Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a5ef5db

Browse files
committedJun 23, 2024·
examples: Improve mqtt_relay to make individual and json topics optional (merbanan#2975)
Add variables to control publishing individual topics and the json topic. Default both to on, so that there is no behavior change. This eases use now, as one's local diff is cleaner. It is also on the path to reading these variables from a config file.
1 parent c187c30 commit a5ef5db

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed
 

‎examples/rtl_433_mqtt_relay.py

+15-12
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
MQTT_PASSWORD = None
3636
MQTT_TLS = False
3737
MQTT_PREFIX = "sensor/rtl_433"
38+
MQTT_INDIVIDUAL_TOPICS = True
39+
MQTT_JSON_TOPIC = True
3840

3941
def mqtt_connect(client, userdata, flags, rc):
4042
"""Handle MQTT connection callback."""
@@ -76,22 +78,23 @@ def publish_sensor_to_mqtt(mqttc, data, line):
7678
elif "id" in data:
7779
path += "/" + str(data["id"])
7880

79-
# Publish some specific items on subtopics.
80-
if "battery_ok" in data:
81-
mqttc.publish(path + "/battery", data["battery_ok"])
81+
if MQTT_INDIVIDUAL_TOPICS:
82+
# Publish some specific items on subtopics.
83+
if "battery_ok" in data:
84+
mqttc.publish(path + "/battery", data["battery_ok"])
8285

83-
if "humidity" in data:
84-
mqttc.publish(path + "/humidity", data["humidity"])
86+
if "humidity" in data:
87+
mqttc.publish(path + "/humidity", data["humidity"])
8588

86-
if "temperature_C" in data:
87-
mqttc.publish(path + "/temperature", data["temperature_C"])
89+
if "temperature_C" in data:
90+
mqttc.publish(path + "/temperature", data["temperature_C"])
8891

89-
if "depth_cm" in data:
90-
mqttc.publish(path + "/depth", data["depth_cm"])
91-
92-
# Publish the entire json string on the main topic.
93-
mqttc.publish(path, line)
92+
if "depth_cm" in data:
93+
mqttc.publish(path + "/depth", data["depth_cm"])
9494

95+
if MQTT_JSON_TOPIC:
96+
# Publish the entire json string on the main topic.
97+
mqttc.publish(path, line)
9598

9699
def parse_syslog(line):
97100
"""Try to extract the payload from a syslog line."""

0 commit comments

Comments
 (0)
Please sign in to comment.