TheAgentK / tuya-mqtt

Nodejs-Script to combine tuyaapi and openhab via mqtt
MIT License
173 stars 81 forks source link

TypeError: Cannot set property 'updated' of undefined #59

Closed efflicto closed 3 years ago

efflicto commented 3 years ago

Describe the bug\ Running DEBUG=tuya-mqtt:* node tuya-mqtt.jsends in the following output/exception and the program exits:

tuya-mqtt:info Connection established to MQTT server +0ms
  tuya-mqtt:tuyapi Search for device id xxx +0ms
  tuya-mqtt:tuyapi Found device id xxx +1ms
  tuya-mqtt:tuyapi Received JSON data from device xxx -> {"20":true,"21":"white","22":300,"23":500,"26":0} +19ms
  tuya-mqtt:tuyapi Connected to device Stehlampe (xxx, xxx, xxx) +986ms
  tuya-mqtt:device-detect Attempting to detect light capabilites and DPS values... +0ms
  tuya-mqtt:device-detect Querying DPS 2 for white/color mode setting... +0ms
  tuya-mqtt:tuyapi Received JSON data from device xxx -> {"20":true,"21":"white","22":300,"23":500,"26":0} +52ms
  tuya-mqtt:tuyapi Received JSON data from device xxx -> {"20":true,"21":"white","22":300,"23":500,"26":0} +11ms
  tuya-mqtt:device-detect Detected likely Tuya color bulb at DPS 20-24, checking more details... +62ms
  tuya-mqtt:device-detect Attempting to detect if bulb supports color temperature... +0ms
  tuya-mqtt:tuyapi Received JSON data from device xxx -> {"20":true,"21":"white","22":300,"23":500,"26":0} +10ms
  tuya-mqtt:device-detect Detected likely color temerature support +10ms
  tuya-mqtt:device-detect Attempting to detect Tuya color format used by device... +1ms
  tuya-mqtt:tuyapi Received JSON data from device xxx -> {"20":true,"21":"white","22":300,"23":500,"26":0} +12ms
  tuya-mqtt:device-detect Detected Tuya color format HSBHEX +11ms
  tuya-mqtt:discovery Home Assistant config topic: homeassistant/light/xxx/config +0ms
  tuya-mqtt:discovery {
  tuya-mqtt:discovery   name: 'Stehlampe',
  tuya-mqtt:discovery   state_topic: 'tuya/stehlampe/state',
  tuya-mqtt:discovery   command_topic: 'tuya/stehlampe/command',
  tuya-mqtt:discovery   brightness_state_topic: 'tuya/stehlampe/color_brightness_state',
  tuya-mqtt:discovery   brightness_command_topic: 'tuya/stehlampe/color_brightness_command',
  tuya-mqtt:discovery   brightness_scale: 100,
  tuya-mqtt:discovery   hs_state_topic: 'tuya/stehlampe/hs_state',
  tuya-mqtt:discovery   hs_command_topic: 'tuya/stehlampe/hs_command',
  tuya-mqtt:discovery   white_value_state_topic: 'tuya/stehlampe/white_brightness_state',
  tuya-mqtt:discovery   white_value_command_topic: 'tuya/stehlampe/white_brightness_command',
  tuya-mqtt:discovery   white_value_scale: 100,
  tuya-mqtt:discovery   availability_topic: 'tuya/stehlampe/status',
  tuya-mqtt:discovery   payload_available: 'online',
  tuya-mqtt:discovery   payload_not_available: 'offline',
  tuya-mqtt:discovery   unique_id: 'xxx',
  tuya-mqtt:discovery   device: {
  tuya-mqtt:discovery     ids: [ 'xxx' ],
  tuya-mqtt:discovery     name: 'Stehlampe',
  tuya-mqtt:discovery     mf: 'Tuya',
  tuya-mqtt:discovery     mdl: 'RGBTW Light'
  tuya-mqtt:discovery   },
  tuya-mqtt:discovery   color_temp_state_topic: 'tuya/stehlampe/color_temp_state',
  tuya-mqtt:discovery   color_temp_command_topic: 'tuya/stehlampe/color_temp_command',
  tuya-mqtt:discovery   min_mireds: 154,
  tuya-mqtt:discovery   max_mireds: 400
  tuya-mqtt:discovery } +0ms
  tuya-mqtt:tuyapi Received JSON data from device xxx -> {"20":true,"21":"white","22":300,"23":500,"26":0} +1s
  tuya-mqtt:info Exit code: TypeError: Cannot set property 'updated' of undefined +2s
  tuya-mqtt:tuyapi Disconnected from device Stehlampe (xxx, xxx, xxx) +3ms
  tuya-mqtt:info Exit code: 0 +1s

tuya-cli works fine inside and outside of the docker container:

tuya-cli set --id xxx--key xxx--dps 20 --set false
Set succeeded.

And the lamp turns off.

Software:

Additional context It worked fine a week ago, coming back to this project and now it throws this error.

tsightler commented 3 years ago

Based on the returned DPS values this does not appear to be an RGBTW light, only a white light with color temperature support (TW light). Can you confirm that this is true or not?

efflicto commented 3 years ago

It is a RGBTW light and a week ago I even could set the color via Openhab. It's this lamp: https://www.amazon.de/gp/product/B08BFPGWZ1/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1

led This is the testing sitemap I build. It worked so far but now I ran into this bug.

tsightler commented 3 years ago

OK, maybe it's simply not sending color value because it's in white mode (there's no DPS 24 value being returned by the device), could you try putting it in color mode with the app and then restarting tuya-mqtt to see if that tickles it back to working? If it does, then I think I understand what is happening here, but I've not seen another device behave this way so I'll have to think how to fix it.

efflicto commented 3 years ago

First of all, thank you for your time and help!

Your guess was right. When the lamp is set to a color, the tuya-mqtt is not crashing. I could use the testing sitemap. Here is the log:

tuya-mqtt:info Connection established to MQTT server +0ms
  tuya-mqtt:tuyapi Search for device id xxx +0ms
  tuya-mqtt:tuyapi Found device id xxx +1ms
  tuya-mqtt:tuyapi Received JSON data from device xxx -> {"20":true,"21":"colour","22":970,"23":220,"24":"xxx","26":0} +34ms
  tuya-mqtt:tuyapi Connected to device Stehlampe (xxx, xxx, 9d8fa31a826050a9) +984ms
  tuya-mqtt:device-detect Attempting to detect light capabilites and DPS values... +0ms
  tuya-mqtt:device-detect Querying DPS 2 for white/color mode setting... +0ms
  tuya-mqtt:tuyapi Received JSON data from device xxx -> {"20":true,"21":"colour","22":970,"23":220,"24":"xxx","26":0} +31ms
  tuya-mqtt:tuyapi Received JSON data from device xxx -> {"20":true,"21":"colour","22":970,"23":220,"24":"xxx","26":0} +14ms
  tuya-mqtt:device-detect Detected likely Tuya color bulb at DPS 20-24, checking more details... +44ms
  tuya-mqtt:device-detect Attempting to detect if bulb supports color temperature... +0ms
  tuya-mqtt:tuyapi Received JSON data from device xxx -> {"20":true,"21":"colour","22":970,"23":220,"24":"xxx","26":0} +11ms
  tuya-mqtt:device-detect Detected likely color temerature support +11ms
  tuya-mqtt:device-detect Attempting to detect Tuya color format used by device... +1ms
  tuya-mqtt:tuyapi Received JSON data from device xxx -> {"20":true,"21":"colour","22":970,"23":220,"24":"xxx","26":0} +13ms
  tuya-mqtt:device-detect Detected Tuya color format HSB +12ms
  tuya-mqtt:discovery Home Assistant config topic: homeassistant/light/xxx/config +0ms
  tuya-mqtt:discovery {
  tuya-mqtt:discovery   name: 'Stehlampe',
  tuya-mqtt:discovery   state_topic: 'tuya/stehlampe/state',
  tuya-mqtt:discovery   command_topic: 'tuya/stehlampe/command',
  tuya-mqtt:discovery   brightness_state_topic: 'tuya/stehlampe/color_brightness_state',
  tuya-mqtt:discovery   brightness_command_topic: 'tuya/stehlampe/color_brightness_command',
  tuya-mqtt:discovery   brightness_scale: 100,
  tuya-mqtt:discovery   hs_state_topic: 'tuya/stehlampe/hs_state',
  tuya-mqtt:discovery   hs_command_topic: 'tuya/stehlampe/hs_command',
  tuya-mqtt:discovery   white_value_state_topic: 'tuya/stehlampe/white_brightness_state',
  tuya-mqtt:discovery   white_value_command_topic: 'tuya/stehlampe/white_brightness_command',
  tuya-mqtt:discovery   white_value_scale: 100,
  tuya-mqtt:discovery   availability_topic: 'tuya/stehlampe/status',
  tuya-mqtt:discovery   payload_available: 'online',
  tuya-mqtt:discovery   payload_not_available: 'offline',
  tuya-mqtt:discovery   unique_id: 'xxx',
  tuya-mqtt:discovery   device: {
  tuya-mqtt:discovery     ids: [ 'xxx' ],
  tuya-mqtt:discovery     name: 'Stehlampe',
  tuya-mqtt:discovery     mf: 'Tuya',
  tuya-mqtt:discovery     mdl: 'RGBTW Light'
  tuya-mqtt:discovery   },
  tuya-mqtt:discovery   color_temp_state_topic: 'tuya/stehlampe/color_temp_state',
  tuya-mqtt:discovery   color_temp_command_topic: 'tuya/stehlampe/color_temp_command',
  tuya-mqtt:discovery   min_mireds: 154,
  tuya-mqtt:discovery   max_mireds: 400
  tuya-mqtt:discovery } +1ms
  tuya-mqtt:tuyapi Received JSON data from device xxx -> {"20":true,"21":"colour","22":970,"23":220,"24":"xxx","26":0} +1s
  tuya-mqtt:tuyapi Received JSON data from device xxx -> {"20":true,"21":"colour","22":970,"23":220,"24":"xxx","26":0} +12ms
  tuya-mqtt:tuyapi Received JSON data from device xxx -> {"20":true,"21":"colour","22":970,"23":220,"24":"xxx","26":0} +15ms
  tuya-mqtt:tuyapi Received JSON data from device xxx -> {"20":true,"21":"colour","22":970,"23":220,"24":"xxx","26":0} +11ms
  tuya-mqtt:tuyapi Received JSON data from device xxx -> {"20":true,"21":"colour","22":970,"23":220,"24":"xxx","26":0} +20ms
  tuya-mqtt:tuyapi Received JSON data from device xxx -> {"20":true,"21":"colour","22":970,"23":220,"24":"xxx","26":0} +12ms
  tuya-mqtt:tuyapi Received JSON data from device xxx -> {"20":true,"21":"colour","22":970,"23":220,"24":"xxx","26":0} +12ms
  tuya-mqtt:state tuya/stehlampe/state ON +0ms
  tuya-mqtt:state tuya/stehlampe/white_brightness_state 97 +4ms
  tuya-mqtt:state tuya/stehlampe/hs_state 120,100 +1ms
  tuya-mqtt:state tuya/stehlampe/color_brightness_state 52 +1ms
  tuya-mqtt:state tuya/stehlampe/hsb_state 120,100,52 +0ms
  tuya-mqtt:state tuya/stehlampe/mode_state colour +0ms
  tuya-mqtt:state tuya/stehlampe/color_temp_state 346 +1ms
  tuya-mqtt:state MQTT DPS JSON: tuya/stehlampe/dps/state ->  {"20":true,"21":"colour","22":970,"23":220,"24":"xxx","26":0} +0ms
  tuya-mqtt:state MQTT DPS20: tuya/stehlampe/dps/20/state ->  true +1ms
  tuya-mqtt:state MQTT DPS21: tuya/stehlampe/dps/21/state ->  colour +0ms
  tuya-mqtt:state MQTT DPS22: tuya/stehlampe/dps/22/state ->  970 +0ms
  tuya-mqtt:state MQTT DPS23: tuya/stehlampe/dps/23/state ->  220 +0ms
  tuya-mqtt:state MQTT DPS24: tuya/stehlampe/dps/24/state ->  xxx +0ms
  tuya-mqtt:state MQTT DPS26: tuya/stehlampe/dps/26/state ->  0 +0ms
  tuya-mqtt:command Received MQTT message ->  {"topic":"tuya/stehlampe/command","message":"OFF"} +0ms
  tuya-mqtt:command Received MQTT command message is a text string +0ms
  tuya-mqtt:command Device xxx received command topic: command, message: off +0ms
  tuya-mqtt:tuyapi Set device xxx -> {"dps":20,"set":false} +38s
  tuya-mqtt:tuyapi Received JSON data from device xxx -> {"20":false} +113ms
  tuya-mqtt:state tuya/stehlampe/state OFF +38s
  tuya-mqtt:state MQTT DPS JSON: tuya/stehlampe/dps/state ->  {"20":false} +1ms
  tuya-mqtt:state MQTT DPS20: tuya/stehlampe/dps/20/state ->  false +0ms
  tuya-mqtt:command Received MQTT message ->  {"topic":"tuya/stehlampe/command","message":"ON"} +2s
  tuya-mqtt:command Received MQTT command message is a text string +2s
  tuya-mqtt:command Device xxx received command topic: command, message: on +0ms
  tuya-mqtt:tuyapi Set device xxx -> {"dps":20,"set":true} +2s
  tuya-mqtt:tuyapi Received JSON data from device xxx -> {"20":true} +120ms
  tuya-mqtt:state tuya/stehlampe/state ON +2s
  tuya-mqtt:state MQTT DPS JSON: tuya/stehlampe/dps/state ->  {"20":true} +0ms
  tuya-mqtt:state MQTT DPS20: tuya/stehlampe/dps/20/state ->  true +1ms
tsightler commented 3 years ago

OK, at least that gives me something to go on. I'll have to think of a way to fix this, but the fact that the device just doesn't return any DPS 24 value just because it's in white mode is certainly unexpected. That means that the autodetection of the color type will always fail if the bulb is in white mode when tuya-mqtt starts, so I'll have to implement something in the detection logic so that, if it gets not value here, it just assumes a format based on the mode switch DPS # and then initializes a base DPS 24 value even if no value is returned when queried.

Shouldn't be too hard but, unfortunately, time has not been on my side with this project lately, and I have a number of outstanding issues to address. I'll try to see if I can code up something for this in the next few days.

efflicto commented 3 years ago

Thank you for this. Take your time and let me know if I can help in any way!

tsightler commented 3 years ago

I pushed 3.0.2 with some enhanced logic to attempt to deal with this issue when the light is in white mode and avoid the undefined property. I'd appreciate it if you could give it a spin when you get a chance and let me know if it works for you since I can't easily test this as none of my devices behave this way.

efflicto commented 3 years ago

Hey tsightler, thank you so much for your work. I switched to OH 3 in the meantime, so I had to setup the whole ting again but after some troubles I can control the lamp via OH. Is there a way that I can thank you with a coffee or something?

tsightler commented 3 years ago

Thanks for letting me know, I'll close this now. No need for further thank me but, if you feel like it, I always encourage people to contribute to a charity or some cause which you believe in or offer any simple, kind gesture to others who might need it after what has been a difficult year for many people across the world.