Open grosjo opened 1 week ago
That's correct, especially if you are just monitoring for status changes. The device will not always send back the full DPS payload so you need to expect that you may not get every DPS every time. You need to add error handling to your python script to handle that. The code snip you sent doesn't seem to be the full code (monitor.py
?).
This is the complete code
ct = datetime.now()
print("Current time = ",ct.strftime("%H %M"))
ch = int(ct.strftime("%H"))
cm = ct.strftime("%M")
chh = int(ct.strftime("%H")) + float(ct.strftime("%M"))/60.0
print("Current hour = ",chh)
for dev in (devices):
print("\nTesting ",dev[0]," Day time = ",dev[4]," Night time = ",dev[5])
tobe = False
if((chh > dev[5]) or (chh < dev[4])):
tobe = True
try:
d=tinytuya.Device(dev[1],dev[2],dev[3])
d.set_socketRetryLimit(2) # set retry count limit [default 5]
d.set_socketTimeout(2) # set connection timeout in seconds [default 5]
d.set_sendWait(1)
d.set_dpsUsed({"1": None})
d.set_version(3.4)
data = d.status()
print(data)
asis = data['dps'][dev[6]]
print("To be = ",tobe," As is =",asis)
if(asis != tobe):
print("Switching ",dev[0])
d.set_value(dev[6],tobe)
except:
print("Error connecting ",dev[0])
print("Done")
Possibly try something like this:
from datetime import datetime
import tinytuya
ct = datetime.now()
print("Current time = ", ct.strftime("%H %M"))
ch = int(ct.strftime("%H"))
cm = ct.strftime("%M")
chh = int(ct.strftime("%H")) + float(ct.strftime("%M")) / 60.0
print("Current hour = ", chh)
for dev in devices:
print("\nTesting ", dev[0], " Day time = ", dev[4], " Night time = ", dev[5])
tobe = False
if (chh > dev[5]) or (chh < dev[4]):
tobe = True
try:
d = tinytuya.Device(dev[1], dev[2], dev[3])
d.set_socketRetryLimit(2) # set retry count limit [default 5]
d.set_socketTimeout(2) # set connection timeout in seconds [default 5]
d.set_sendWait(1)
d.set_dpsUsed({"1": None})
d.set_version(3.4)
data = d.status()
print(data)
# Check if 'dps' exists and contains the required key
if 'dps' in data and dev[6] in data['dps']:
asis = data['dps'][dev[6]]
print("To be = ", tobe, " As is =", asis)
if asis != tobe:
print("Switching ", dev[0])
d.set_value(dev[6], tobe)
else:
print(f"DPS data missing or key {dev[6]} not found for device {dev[0]}")
except Exception as e:
print(f"Error connecting to {dev[0]}: {e}")
print("Done")
calling d.status() returns sometimes some weird answer (protocol 3.4)
my code: