#!/usr/bin/env python import requests import json import mosquitto import time, sys # URL to YouLess JSON format URL = "http://192.168.1.9/a?f=j" def energy(url): cnt = '' pwr = 0 status = 0 r = requests.get(url) if r.status_code != 200: return dict(status=1) # {"cnt":"10341,460","pwr":911,"lvl":100,"dev":"","det":"•","con":"*","sts":"","raw":0} js = json.loads(r.text) # Meter counter is inserting decimal comma, either because the meter is # German or because the YouLess is Dutch :) if 'cnt' in js: cnt = js['cnt'].replace(',', '.') if 'pwr' in js: pwr = js['pwr'] return dict(cnt=cnt, pwr=pwr, status=0) if __name__ == '__main__': mqttc = mosquitto.Mosquitto() mqttc.connect("127.0.0.1", 1883, 60) e = energy(URL) while e['status'] == 0: print e['cnt'], e['pwr'] try: string = e['pwr'] mqttc.publish('hw/youless', string , 0) mqttc.loop() time.sleep(15) e = energy(URL) except KeyboardInterrupt: sys.exit(1) except: raise