How to udate a device within a Python script in Domoticz

Python and python framework

Moderator: leecollings

Post Reply
Itschi
Posts: 18
Joined: Saturday 16 May 2015 14:58
Target OS: Windows
Domoticz version:
Contact:

How to udate a device within a Python script in Domoticz

Post by Itschi »

Hi,

I used to update thermometer by a python script (see below). Unfortunately the script stooped working after I tried to update Domoticz from Version 2022.1 to 2022.2 or later. It seems that "updateDevice(DE.Devices[device_temp].id, 0, Mydata['Temperatur Istwert'])" is no longer available in "DomoticzEvents". Any idea on how I can update the thermostat device temperature alternately?

Code: Select all

import json
import DomoticzEvents as DE
import requests
import datetime

saunaurl = "http://192.168.100.99/sauna?JSON"   #URL of Vanvex Controller's JSON Output
Update = 60                                     #Update Interval in seconds
device_temp = 'Saunatemperatur'                 #Device Name for Sauna Temperature
device_sauna = 'Sauna'                          #Device Name for Sauna Heating
device_flap = 'Abluftklappe Sauna'
json_sauna = 'Status Heizung'                   #JSON Name for Sauna Heating

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; "
}
mySession = requests.Session()

def readsauna():
    Returnvalue = -1
    saunadata = []
    try:
        #------------------------------------------------------------
        #Start Connection to Sauna 
        #------------------------------------------------------------
        r1 = mySession.get(saunaurl,  headers=headers)
        if (r1.status_code == requests.codes.ok):
            saunadata=json.loads(r1.text)
            if (len(saunadata) >1):                        # We have something in the list
                Returnvalue =0 
#         else:
#         Error_Connecting = -1
#         DE.Log("Unable to connect :"+str(Returnvalue))
    except Exception as Error_ConnecttoSauna:
        pass
#        Error_Connecting = -1
#        DE.Log("Error accessing Sauna: "+str(Error_ConnecttoSauna))      
    #--------------------------------------------------------------
    # Processing data
    #--------------------------------------------------------------
    return (Returnvalue, saunadata)    

try:
    d = datetime.datetime.now()
    l = datetime.datetime.strptime(DE.Devices[device_temp].last_update_string, "%Y-%m-%d %H:%M:%S")
    if (d-l).seconds >= Update:
        Myreturnvalue, Mydata = readsauna();
        if (Myreturnvalue == 0):
            params = Mydata.keys()
            if (Mydata['Temperatur Istwert'] != '0.0'):
                updateDevice(DE.Devices[device_temp].id, 0, Mydata['Temperatur Istwert'])
                for p in sorted(params):
                    theline = "{:{width}}: {}".format(p, Mydata[p], width=len(max(params, key=len)))
                    DE.Log("(Sauna) "+theline)
            if (Mydata['Status Heizung'][0:2] == 'AN'):
                if (DE.Devices[device_sauna].n_value_string == 'Off'):
                    DE.Command(device_sauna, 'On')
            else:
                if (DE.Devices[device_sauna].n_value_string == 'On'):
                    DE.Command(device_sauna, 'Off')
            if (Mydata['Abluft Sauna'][0:3] == 'AUF'):
                if (DE.Devices[device_flap].n_value_string == 'Off'):
                    DE.Command(device_flap, 'On')
            else:
                if (DE.Devices[device_flap].n_value_string == 'On'):
                    DE.Command(device_flap, 'Off')
        else:
            DE.Log("(Sauna) ERROR: I was unable to query Sauna")
except Exception as ex:
    DE.Log("(Sauna) ERROR: Issues querying Sauna ("+str(ex)+")")
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest