Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

device.status() gives sometimes a weird reply #565

Closed
grosjo opened this issue Nov 27, 2024 · 3 comments
Closed

device.status() gives sometimes a weird reply #565

grosjo opened this issue Nov 27, 2024 · 3 comments

Comments

@grosjo
Copy link

grosjo commented Nov 27, 2024

calling d.status() returns sometimes some weird answer (protocol 3.4)

image

my code:

    d=tinytuya.Device(dev[1],dev[2],dev[3])        // dev[] include the ID/key/ip of the device                     
    d.set_socketRetryLimit(2)                                   
    d.set_socketTimeout(2)                                       
    d.set_sendWait(1)                                                                                             
    d.set_dpsUsed({"1": None})                                                                                    
    d.set_version(3.4)                                                                                            
    data = d.status()                                                                                             

@jasonacox
Copy link
Owner

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?).

@grosjo
Copy link
Author

grosjo commented Nov 28, 2024

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")  

@jasonacox
Copy link
Owner

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")

@grosjo grosjo closed this as completed Dec 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants