Skip to content

Commit

Permalink
examples: Improve mqtt_relay to make individual and json topics optional
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gdt committed Jun 20, 2024
1 parent 321f231 commit 68c0299
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions examples/rtl_433_mqtt_relay.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
MQTT_PASSWORD = None
MQTT_TLS = False
MQTT_PREFIX = "sensor/rtl_433"
MQTT_INDIVIDUAL_TOPICS = True
MQTT_JSON_TOPIC = True

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

# Publish some specific items on subtopics.
if "battery_ok" in data:
mqttc.publish(path + "/battery", data["battery_ok"])
if MQTT_INDIVIDUAL_TOPICS:
# Publish some specific items on subtopics.
if "battery_ok" in data:
mqttc.publish(path + "/battery", data["battery_ok"])

if "humidity" in data:
mqttc.publish(path + "/humidity", data["humidity"])
if "humidity" in data:
mqttc.publish(path + "/humidity", data["humidity"])

if "temperature_C" in data:
mqttc.publish(path + "/temperature", data["temperature_C"])
if "temperature_C" in data:
mqttc.publish(path + "/temperature", data["temperature_C"])

if "depth_cm" in data:
mqttc.publish(path + "/depth", data["depth_cm"])

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

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

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

0 comments on commit 68c0299

Please sign in to comment.