Skip to content

Instantly share code, notes, and snippets.

@stylpen
Created October 7, 2014 19:01
Show Gist options
  • Save stylpen/8364ef6fe71af24e216f to your computer and use it in GitHub Desktop.
Save stylpen/8364ef6fe71af24e216f to your computer and use it in GitHub Desktop.
example usage of mqtt wrapper for homA (sums up smart meter and inverter sensor data)
#! /usr/bin/python
import mqtt, time
MQTT_HOST = "192.168.11.54"
WRs = {"dummy" : 0}
SMs = {"dummy" : 0}
def on_message(mosquitto, message):
device = message.topic.split("/")[4]
if device[0] == "3": # this means it in data from an inverter
WRs[device] = int(message.payload)
elif device[0] == "V": # data is from the smart meter
SMs[device] = float(message.payload)
WRsum = reduce(lambda x, y: x + y, WRs.values())
SMsum = reduce(lambda x, y: x + y, SMs.values())
diff = WRsum - SMsum
mqttPublisher.publish("/devices/RLog/controls/Production", WRsum, 0, True)
mqttPublisher.publish("/devices/RLog/controls/Consumption", SMsum, 0, True)
if diff < 0:
mqttPublisher.publish("/devices/RLog/controls/Purchase", -diff, 0, True)
mqttPublisher.publish("/devices/RLog/controls/Infeed", 0, 0, True)
else:
mqttPublisher.publish("/devices/RLog/controls/Infeed", diff, 0, True)
mqttPublisher.publish("/devices/RLog/controls/Purchase", 0, 0, True)
mqttPublisher = mqtt.mqtt(MQTT_HOST)
mqttPublisher.startMQTT()
mqttPublisher.on_message = on_message
mqttPublisher.subscribe("/devices/RLog/controls/3002IN (1)", 0)
mqttPublisher.subscribe("/devices/RLog/controls/3002IN (2)", 0)
mqttPublisher.subscribe("/devices/RLog/controls/3002IN (3)", 0)
mqttPublisher.subscribe("/devices/RLog/controls/VSM-102 (1)", 0)
mqttPublisher.subscribe("/devices/RLog/controls/VSM-102 (2)", 0)
mqttPublisher.subscribe("/devices/RLog/controls/VSM-102 (3)", 0)
mqttPublisher.publish("/devices/RLog/controls/Production/meta/type", "text", 0, True)
mqttPublisher.publish("/devices/RLog/controls/Consumption/meta/type", "text", 0, True)
mqttPublisher.publish("/devices/RLog/controls/Infeed/meta/type", "text", 0, True)
mqttPublisher.publish("/devices/RLog/controls/Purchase/meta/type", "text", 0, True)
while(True):
time.sleep(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment