tinytuya: Different / Varied Failure Cases

Hi,

Really liking tinytuya - appreciate it! But I admit, seeing some odd results here. I think that’s my specialty though … 🤣.

I am polling my devices on a regular basis, using them for power monitoring (of some switches providing energy monitoring capability). But … relatively often (i.e. 10+ times a day), they cause my code to “yell”, for a few different reasons. So attaching my code below, as well as some of the error messages. And a few thoughts,

  1. I was going to add code to check for the recently added Error status … but nothing is there in the case of passing results? Thinking there should always be a return status? Just to avoid try … catch loops (they just seem nasty, LOL!)
  2. I seem to be seeing different outcomes and failures, some of them not caught (reported) by the Error status. Or am I missing it?

My code, called inside the loop, with except to let me know what kinds of failure I am seeing,

        try:
            device = tinytuya.OutletDevice(currdevice['id'], currdevice['hostname'], currdevice['key'])
            device.set_version(currdevice['ver'])
            device.set_socketPersistent(True)
            device.updatedps()
            currData = device.status()
            if currdevice['ver'] == 3.1:
                time.sleep(0.5)
            dps = currData["dps"]
            # Check for power data - DP 19 on some 3.1/3.3 devices
            W = A = V = 0
            if "19" in dps.keys():
                W = float(dps['19']) / 10.0
                A = float(dps['18']) / 1000.0
                V = float(dps['20']) / 10.0
            # Check for power data - DP 5 for some 3.1 devices
            elif "5" in dps.keys():
                W = float(dps["5"]) / 10.0
                A = float(dps["4"]) / 1000.0
                V = float(dps["6"]) / 10.0
            # Return power measurement results
            return {'Current': A, 'Power': W, 'Voltage': V}
        except OSError:
            print("[OSError] Error reading Tuya power meter, ", currdevice['hostname'])
            return None
        except ValueError:
            print("[ValueError] Error reading Tuya power meter, ", currdevice['hostname'])
            return None
        except KeyError:
            print("[KeyError] Error reading Tuya power meter, ", currdevice['hostname'])
            print("[KeyError] Data, ", currData)
            return None
        except TypeError:
            print("[TypeError] Error reading Tuya power meter, ", currdevice['hostname'])
            print("[TypeError] Data, ", currData)
            return None

And, some of the exceptions (some normal / caught, others not,

[KeyError] Error reading Tuya power meter,  emFrontSwitch [KeyError] Data,  {'Error': 'Network Error: Unable to Connect', 'Err': '901', 'Payload': None}
[TypeError] Error reading Tuya power meter,  emRussell [TypeError] Data,  None
[KeyError] Error reading Tuya power meter,  emWinServer [KeyError] Data,  {'dps': {'18': 530, '19': 642}, 't': 1617010003}
[KeyError] Error reading Tuya power meter,  emRussell [KeyError] Data,  {'Error': 'Network Error: Device Unreachable', 'Err': '905', 'Payload': None}

And this one, when using the data (somehow wrong data type?),

influxdb.exceptions.InfluxDBClientError: 400: {"error":"partial write: field type conflict: input field \"value\" on measurement \"Current\" is type integer, already exists as type float dropped=3"}

Thanks!

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

Hmmm … nothing so far 😆. And with v1.2.4, my failure rates are down quite a bit? Did you change anything else?

hostname=emFrontSwitch       : Failure Rate = 0.7%, OK Count = 1428, Total Count = 1438
hostname=emHackintosh        : Failure Rate = 0.0%, OK Count = 1439, Total Count = 1439
hostname=emLinuxServer       : Failure Rate = 0.0%, OK Count = 1438, Total Count = 1438
hostname=emPoolTuya          : Failure Rate = 0.3%, OK Count = 1433, Total Count = 1438
hostname=emRussell           : Failure Rate = 0.0%, OK Count = 1438, Total Count = 1438
hostname=emWinServer         : Failure Rate = 0.3%, OK Count = 1434, Total Count = 1439

Thanks!

You are correct. Let me know how it goes! Thanks, Russell.

Thanks @arrmo - I appreciate the updates. I added some additional error handling for unpack_message() calls to see if we can catch it. It will be available in v1.2.4.