def resetmaxeinschaltdauerfunc(): global resetmaxeinschaltdauer global numberOfSupportedDevices global mydevices mqtt_reset = {} hour = time.strftime("%H") if (int(hour) == 0): if (int(resetmaxeinschaltdauer) == 0): for i in range(1, (numberOfSupportedDevices + 1)): for mydevice in mydevices: if (str(i) == str(mydevice.device_nummer)): pref = 'openWB/SmartHome/Devices/' + str(i) + '/' mydevice.runningtime = 0 mqtt_reset[pref + 'RunningTimeToday'] = '0' log.info("(" + str(i) + ") RunningTime auf 0 gesetzt") mydevice.oncountnor = '0' mqtt_reset[pref + 'oncountnor'] = '0' # Sofern Anlauferkennung laueft counter nicht zuruecksetzen if (mydevice.devstatus != 20): mydevice.oncntstandby = '0' mqtt_reset[pref + 'OnCntStandby'] = '0' mydevice.c_oldstampeinschaltdauer = 0 mydevice.c_oldstampeinschaltdauer_f = 'N' resetmaxeinschaltdauer = 1 # Nur einschaltgruppe in Sekunden für neuen Tag zurücksetzten Sbase.nureinschaltinsec = 0 sendmq(mqtt_reset) if (int(hour) == 1): resetmaxeinschaltdauer = 0
def updatepar(self, input_param): self._smart_param = input_param.copy() self.device_nummer = int(self._smart_param.get('device_nummer', '0')) for key, value in self._smart_param.items(): if (key == 'device_pbip'): self._device_pbip = value else: log.info("(" + str(self.device_nummer) + ") " + __class__.__name__ + " überlesen " + key + " " + value)
def updatepar(self, input_param): super().updatepar(input_param) if (self._old_measuretype0 == 'none'): self._mydevicemeasure0 = Sltasmota() self._old_measuretype0 = 'tasmota' log.info("(" + str(self.device_nummer) + ") Integrierte Leistungsmessung. Neues Measure" + " device erzeugt " + self.device_type) else: log.info("(" + str(self.device_nummer) + ") Integrierte Leistungsmessung. Nur Parameter " + " update " + self.device_type) self._mydevicemeasure0.updatepar(input_param)
def pbon(self): if (self.led == 1): return try: urllib.request.urlopen("http://" + str(self._device_pbip) + "/settings?led_status_disable=false", timeout=3) log.info("Shelly button led on (%d) %s" % (self.device_nummer, self._device_pbip)) except Exception as e1: log.warning("Shelly button on (%d) %s Fehlermeldung: %s " % (self.device_nummer, self._device_pbip, str(e1))) self.led = 1
def updatepar(self, input_param): super().updatepar(input_param) self._smart_paramadd = input_param.copy() self.device_nummer = int(self._smart_paramadd.get( 'device_nummer', '0')) for key, value in self._smart_paramadd.items(): if (key == 'device_nummer'): pass elif (key == 'device_idmnav'): self._device_idmnav = value else: log.info("(" + str(self.device_nummer) + ") " + __class__.__name__ + " überlesen " + key + " " + value)
def sendmq(mqtt_input): global mqtt_cache client = mqtt.Client("openWB-SmartHome-bulkpublisher-" + str(os.getpid())) client.connect("localhost") for key, value in mqtt_input.items(): valueold = mqtt_cache.get(key, 'not in cache') if (valueold == value): pass else: log.info("Mq pub " + str(key) + "=" + str(value) + " old " + str(valueold)) mqtt_cache[key] = value client.publish(key, payload=value, qos=0, retain=True) client.loop(timeout=2.0) client.disconnect()
def checkbut(self, manual, relais, manual_control): newmanual = manual newmanual_control = manual_control try: at = str(urllib.request.urlopen("http://" + str(self._device_pbip) + "/status", timeout=3).read().decode("utf-8")) except Exception as e1: log.warning("Shelly button ch (%d) %s Fehlermeldung: %s " % (self.device_nummer, self._device_pbip, str(e1))) return newmanual, newmanual_control a = json.loads(at) with open(self._basePath+'/ramdisk/smarthome_device_ret' + str(self.device_nummer) + '_shelly_bp', 'w') as f: f.write(str(a)) self.oldevent_cnt = self.event_cnt self.oldevent = self.event self.event_cnt = int(a['inputs'][0]['event_cnt']) self.event = str(a['inputs'][0]['event']) if (self.oldevent == 'none'): return newmanual, newmanual_control if ((self.event == self.oldevent) and (self.event_cnt == self.oldevent_cnt)): return newmanual, newmanual_control log.info("Shelly button pressed (%d) %s %s" % (self.device_nummer, self._device_pbip, self.event)) # im automatic modus -> ein mal Drücken wechselen auf manual if (manual == 0): newmanual = 1 return newmanual, newmanual_control # im manual modus -> ein mal Drücken wechselen zwischen on und off if (self.event == 'S'): if (manual_control == 1): newmanual_control = 0 else: newmanual_control = 1 return newmanual, newmanual_control # im manual modus -> mehrmals drücken wechselen auf automat newmanual = 0 return newmanual, newmanual_control
def readmq(): global parammqtt global mydevices log_config.info("Config reRead start") log.info("Config reRead start") parammqtt = [] client = mqtt.Client("openWB-mqttsmarthome") client.on_connect = on_connect client.on_message = on_message startTime = time.time() waitTime = 5 client.connect("localhost") while True: client.loop() elapsedTime = time.time() - startTime if elapsedTime > waitTime: client.disconnect() break update_devices() log_config.info("Config reRead done") log.info("Config reRead done")
def updatepar(self, input_param): super().updatepar(input_param) self._smart_paramadd = input_param.copy() self.device_nummer = int(self._smart_paramadd.get( 'device_nummer', '0')) for key, value in self._smart_paramadd.items(): if (key == 'device_actor'): self._device_actor = value elif (key == 'device_username'): self._device_username = value elif (key == 'device_password'): self._device_password = value else: log.info("(" + str(self.device_nummer) + ") " + __class__.__name__ + " überlesen " + key + " " + value) if (self._old_measuretype0 == 'none'): self._mydevicemeasure0 = Slavm() self._old_measuretype0 = 'avm' log.info("(" + str(self.device_nummer) + ") Integrierte Leistungsmessung. Neues Measure" + " device erzeugt " + self.device_type) else: log.info("(" + str(self.device_nummer) + ") Integrierte Leistungsmessung. Nur Parameter" + " update " + self.device_type) self._mydevicemeasure0.updatepar(input_param)
def updatepar(self, input_param): super().updatepar(input_param) self._smart_paramadd = input_param.copy() self.device_nummer = int(self._smart_paramadd.get( 'device_nummer', '0')) for key, value in self._smart_paramadd.items(): # try: # valueint = int(value) # except Exception: # valueint = 0 if (key == 'device_nummer'): pass elif (key == 'device_einschalturl'): self._device_einschalturl = value elif (key == 'device_ausschalturl'): self._device_ausschalturl = value else: log.info("(" + str(self.device_nummer) + ") " + __class__.__name__ + " überlesen " + key + " " + value) if (self._old_measuretype0 == 'none'): self._mydevicemeasure0 = Slhttp() self._old_measuretype0 = 'http' log.info("(" + str(self.device_nummer) + ") Integrierte Leistungsmessung. Neues Measure" + " device erzeugt " + self.device_type) else: log.info("(" + str(self.device_nummer) + ") Integrierte Leistungsmessung. Nur Parameter" + " update " + self.device_type) self._mydevicemeasure0.updatepar(input_param)
def updatepar(self, input_param): self._smart_param = input_param.copy() self.device_nummer = int(self._smart_param.get('device_nummer', '0')) for key, value in self._smart_param.items(): try: valueint = int(value) except Exception: valueint = 0 # params known to be used in sbase, to avoid logging if (key in [ 'device_nummer', 'device_mineinschaltdauer', 'device_finishTime', 'device_ausschaltschwelle', 'device_manual_control', 'device_canSwitch', 'device_standbyDuration', 'device_startTime', 'device_onuntilTime', 'device_einschaltverzoegerung', 'device_standbyPower', 'device_einschaltschwelle', 'device_ausschaltverzoegerung', 'device_speichersocbeforestop', 'device_homeConsumtion', 'device_deactivateWhileEvCharging', 'device_startupMulDetection', 'device_onTime', 'device_speichersocbeforestart', 'device_endTime', 'device_maxeinschaltdauer', 'mode', 'WHImported_temp', 'RunningTimeToday', 'oncountnor', 'OnCntStandby', 'device_deactivateper', 'device_startupDetection' ]): pass elif (key == 'device_differentMeasurement'): self._device_differentmeasurement = valueint elif (key == 'device_type'): self.device_type = value elif (key == 'device_configured'): self._device_configured = value elif (key == 'device_name'): self.device_name = value elif (key == 'device_temperatur_configured'): self.device_temperatur_configured = valueint elif (key == 'device_ip'): self._device_ip = value elif (key == 'device_measureType'): self._device_measuretype = value elif (key == 'device_measureip'): self._device_measureip = value elif (key == 'device_measurePortSdm'): self._device_measureportsdm = value elif (key == 'device_measuresmaage'): self._device_measuresmaage = valueint elif (key == 'device_measchan'): self._device_measchan = valueint elif (key == 'device_chan'): self._device_chan = valueint elif (key == 'device_measuresmaser'): self._device_measuresmaser = value elif (key == 'device_measureid'): self._device_measureid = value elif (key == 'device_leistungurl'): self._device_leistungurl = value elif (key == 'device_measureurl'): self._device_measureurl = value elif (key == 'device_measureurlc'): self._device_measureurlc = value elif (key == 'device_measurejsonurl'): self._device_measurejsonurl = value elif (key == 'device_measurejsonpower'): self._device_measurejsonpower = value elif (key == 'device_measurejsoncounter'): self._device_measurejsoncounter = value elif (key == 'device_measureavmactor'): self._device_measureavmactor = value elif (key == 'device_measureavmusername'): self._device_measureavmusername = value elif (key == 'device_measureavmpassword'): self._device_measureavmpassword = value elif (key == 'device_actor'): self._device_actor = value elif (key == 'device_username'): self._device_username = value elif (key == 'device_password'): self._device_password = value elif (key == 'device_stateurl'): self._device_stateurl = value else: log.info("(" + str(self.device_nummer) + ") " + __class__.__name__ + " überlesen " + key + " " + value)
def update_devices(): global parammqtt global mydevices global mqtt_cache client = mqtt.Client("openWB-SmartHome-bulkpublisher-" + str(os.getpid())) client.connect("localhost") # statische daten einschaltgruppe Sbase.ausdevices = 0 Sbase.eindevices = 0 Sbase.einverz = 0 Sbase.einschwelle = 0 # Nur einschaltgruppe in Sekunden Sbase.nureinschaltinsec = 0 for i in range(1, numberOfSupportedDevices + 1): device_configured = 0 device_type = 'none' input_param = {} input_param['device_nummer'] = str(i) for devicenumb, keyword, value in parammqtt: if (str(i) == str(devicenumb)): if (keyword == 'device_configured'): device_configured = value if (keyword == 'device_type'): device_type = value input_param[keyword] = value if (device_configured == "1"): createnew = 1 for mydevice in mydevices: if (str(i) == str(mydevice.device_nummer)): log.info("(" + str(i) + ") " + "Device bereits erzeugt") if (device_type == mydevice.device_type): log.info("(" + str(i) + ") " + "Typ gleich, nur Parameter update") createnew = 0 mydevice.updatepar(input_param) else: log.info("(" + str(i) + ") " + "Typ ungleich " + mydevice.device_type) mydevice.device_nummer = 0 mydevice._device_configured = '9' # del mydevice mydevices.remove(mydevice) log.info("(" + str(i) + ") " + "Device gelöscht") break if (createnew == 1): log.info("(" + str(i) + ") Neues Devices oder Typänderung: " + str(device_type)) if (device_type == 'shelly'): mydevice = Sshelly() elif (device_type == 'stiebel'): mydevice = Sstiebel() elif (device_type == 'vampair'): mydevice = Svampair() elif (device_type == 'tasmota'): mydevice = Stasmota() elif (device_type == 'avm'): mydevice = Savm() elif (device_type == 'viessmann'): mydevice = Sviessmann() elif (device_type == 'acthor'): mydevice = Sacthor() elif (device_type == 'elwa'): mydevice = Selwa() elif (device_type == 'idm'): mydevice = Sidm() elif (device_type == 'mqtt'): mydevice = Smqtt() elif (device_type == 'http'): mydevice = Shttp() elif (device_type == 'mystrom'): mydevice = Smystrom() else: mydevice = Sbase() mydevice.updatepar(input_param) mydevices.append(mydevice) else: log.info("(" + str(i) + ") " + "Device nicht (länger) definiert") for mydevice in mydevices: if (str(i) == str(mydevice.device_nummer)): # cleant up mqtt for key, value in mydevice.mqtt_param_del.items(): valueold = mqtt_cache.pop(key, 'not in cache') log.info("Mq pub " + str(key) + "=" + str(value) + " old " + str(valueold)) client.publish(key, payload=value, qos=0, retain=True) client.loop(timeout=2.0) mydevice.device_nummer = 0 mydevice._device_configured = '9' # del mydevice mydevices.remove(mydevice) log.info("(" + str(i) + ") " + "Device gelöscht") client.disconnect()
def getdevicevalues(): global mydevices global uberschuss global uberschussoffset totalwatt = 0 totalwattot = 0 totalminhaus = 0 # dyn daten einschaltgruppe Sbase.ausschaltwatt = 0 Sbase.einrelais = 0 Sbase.eindevstatus = 0 mqtt_all = {} for mydevice in mydevices: mydevice.getwatt(uberschuss, uberschussoffset) watt = mydevice.newwatt wattk = mydevice.newwattk relais = mydevice.relais # temp0 = mydevice.temp0 # temp1 = mydevice.temp1 # temp2 = mydevice.temp2 if ((mydevice.abschalt == 1) and (relais == 1) and (mydevice.device_manual != 1)): totalwatt = totalwatt + watt else: totalwattot = totalwattot + watt if (mydevice.device_homeconsumtion == 0): totalminhaus = totalminhaus + watt log.info("(" + str(mydevice.device_nummer) + ") " + str(mydevice.device_name) + " rel: " + str(relais) + " oncnt/onstandby/time: " + str(mydevice.oncountnor) + "/" + str(mydevice.oncntstandby) + "/" + str(mydevice.runningtime) + " Status/Üeb: " + str(mydevice.devstatus) + "/" + str(mydevice.ueberschussberechnung) + " akt: " + str(watt) + " Z: " + str(wattk)) mqtt_all.update(mydevice.mqtt_param) # device_total_watt is needed for calculation the proper überschuss # (including switchable smarthomedevices) with open(bp + '/ramdisk/devicetotal_watt', 'w') as f: f.write(str(totalwatt)) with open(bp + '/ramdisk/devicetotal_watt_other', 'w') as f: f.write(str(totalwattot)) with open(bp + '/ramdisk/devicetotal_watt_hausmin', 'w') as f: f.write(str(totalminhaus)) log.info("Total Watt abschaltbarer smarthomedevices: " + str(totalwatt)) log.info("Total Watt nichtabschaltbarer smarthomedevices: " + str(totalwattot)) log.info("Total Watt nicht im Hausverbrauch: " + str(totalminhaus)) log.info("Anzahl devices in Auschaltgruppe: " + str(Sbase.ausdevices) + " akt: " + str(Sbase.ausschaltwatt) + " Anzahl devices in Einschaltgruppe: " + str(Sbase.eindevices)) nurhh = math.floor(Sbase.nureinschaltinsec / 3600) nurmm = math.floor((Sbase.nureinschaltinsec - (nurhh * 3600)) / 60) nurss = (Sbase.nureinschaltinsec - (nurhh * 3600) - (nurmm * 60)) log.info("Einschaltgruppe rel: " + str(Sbase.einrelais) + " Summe Einschaltschwelle: " + str(Sbase.einschwelle) + " max Einschaltverzögerung " + str(Sbase.einverz) + " nur Einschaltgruppe prüfen bis: " + str('%.2d' % nurhh) + ":" + str('%.2d' % nurmm) + ":" + str('%.2d' % nurss) + " in Total sec " + str(Sbase.nureinschaltinsec)) mqtt_all['openWB/SmartHome/Status/maxspeicherladung'] = maxspeicher mqtt_all['openWB/SmartHome/Status/wattschalt'] = totalwatt mqtt_all['openWB/SmartHome/Status/wattnichtschalt'] = totalwattot mqtt_all['openWB/SmartHome/Status/wattnichtHaus'] = totalminhaus mqtt_all['openWB/SmartHome/Status/uberschuss'] = uberschuss mqtt_all['openWB/SmartHome/Status/uberschussoffset'] = uberschussoffset sendmq(mqtt_all)
def loadregelvars(): global uberschuss global uberschussoffset global speicherleistung global speichersoc global speichervorhanden global wattbezug global numberOfSupportedDevices global maxspeicher global mydevices try: with open(bp + '/ramdisk/speichervorhanden', 'r') as value: speichervorhanden = int(value.read()) if (speichervorhanden == 1): with open(bp + '/ramdisk/speicherleistung', 'r') as value: speicherleistung = int(float(value.read())) with open(bp + '/ramdisk/speichersoc', 'r') as value: speichersoc = int(float(value.read())) else: speicherleistung = 0 speichersoc = 100 except Exception as e: log.warning("Fehler beim Auslesen der Ramdisk " + "(speichervorhanden,speicherleistung,speichersoc): " + str(e)) speichervorhanden = 0 speicherleistung = 0 speichersoc = 100 try: with open(bp + '/ramdisk/wattbezug', 'r') as value: wattbezug = int(float(value.read())) * -1 except Exception as e: log.warning("Fehler beim Auslesen der Ramdisk (wattbezug):" + str(e)) wattbezug = 0 uberschuss = wattbezug + speicherleistung try: with open(bp + '/ramdisk/smarthomehandlermaxbatterypower', 'r') as value: maxspeicher = int(value.read()) except Exception as e: log.warning("Fehler beim Auslesen der Ramdisk " + "(smarthomehandlermaxbatterypower): " + str(e)) maxspeicher = 0 uberschussoffset = wattbezug + speicherleistung - maxspeicher log.info("EVU Bezug(-)/Einspeisung(+): " + str(wattbezug) + " max Speicherladung: " + str(maxspeicher)) log.info("Uberschuss: " + str(uberschuss) + " Uberschuss mit Offset: " + str(uberschussoffset)) log.info("Speicher Entladung(-)/Ladung(+): " + str(speicherleistung) + " SpeicherSoC: " + str(speichersoc)) reread = 0 try: with open(bp + '/ramdisk/rereadsmarthomedevices', 'r') as value: reread = int(value.read()) except Exception: reread = 1 if (reread == 1): with open(bp + '/ramdisk/rereadsmarthomedevices', 'w') as f: f.write(str(0)) readmq() for i in range(1, (numberOfSupportedDevices + 1)): try: with open(bp + '/ramdisk/smarthome_device_manual_' + str(i), 'r') as value: for mydevice in mydevices: if (str(i) == str(mydevice.device_nummer)): mydevice.device_manual = int(value.read()) except Exception: pass try: with open( bp + '/ramdisk/smarthome_device_manual_control_' + str(i), 'r') as value: for mydevice in mydevices: if (str(i) == str(mydevice.device_nummer)): mydevice.device_manual_control = int(value.read()) except Exception: pass
if (mydevice.devstatus != 20): mydevice.oncntstandby = '0' mqtt_reset[pref + 'OnCntStandby'] = '0' mydevice.c_oldstampeinschaltdauer = 0 mydevice.c_oldstampeinschaltdauer_f = 'N' resetmaxeinschaltdauer = 1 # Nur einschaltgruppe in Sekunden für neuen Tag zurücksetzten Sbase.nureinschaltinsec = 0 sendmq(mqtt_reset) if (int(hour) == 1): resetmaxeinschaltdauer = 0 if __name__ == "__main__": initlog() log.info("*** Smarthome mq Start ***") log_config.info("*** Smarthome mq Start ***") while True: if (checkbootdone() == 1): break time.sleep(5) readmq() time.sleep(5) while True: # update_devices() mqtt_man = {} sendmess = 0 loadregelvars() resetmaxeinschaltdauerfunc() getdevicevalues() conditions()