smarthomej / addons

SmartHome/J addons for openHAB
Eclipse Public License 2.0
59 stars 23 forks source link

Tuya energy metering polling interval #397

Closed obrador closed 1 year ago

obrador commented 1 year ago

OpenHab version 3.2.0 running on a Debian Buster VM SmartHomeJ version 3.2.14

Hi, I recently got a DIN format wifi circuit breaker with power metering, I used smarthomej Tuya Plugin with great results (great job with this plugin, by the way!) I can switch on / off and also I get power readings (Voltage, current, etc...) but this readings are refreshing at a very irregular interval, sometimes minutes, but sometime hours. If I open SmartLife App and select the device, the values are refreshed instantly, so there should be a way to force the device to refresh those values. I haven't tested on other devices (I have ordered 4 wifi socket plugs, also with power monitor, and I will try when they arrive).

I found this in localtuya plugin for HA, which looks like the same issue, and if you read localtuya Readme, it seems they have solved the issue because you can set a "scan interval" down to 10 seconds.

I think it would be a very interesting to have an option like this in smarthomej Tuya binding, in my case is mandatory, because I relay in those readings to be able to run a water pump.

Do you see it possible to integrate in this fabulous plugin?

Many thanks in avance, Jaume Obrador.

J-N-K commented 1 year ago

Can you uninstall the binding, change the JSON 3rd Party Marketplace configuration to https://download.smarthomej.org/addons.json|https://download.smarthomej.org/addons-beta.json, enable "unstable Add-ons" and then install "Smarthome/J Tuya (develop)" and check if that works for you? You don't need to re-create the things. Thanks.

obrador commented 1 year ago

Thanks a lot J. I tried the new developer version, now it let's me enter a polling interval, but the value the sensor doesn't get updated at the desired interval (in this case it's a voltage sensor).

Running some tests I've noticed that when the voltage changes substantialy, the value of the sensor automatically gets updated (for example, from 230V to 220V) but for small changes it doesn't, but If I open SmartLife App and access the sensor it gets updated instantly and shows minor changes (230.1V, 230.0V, 228V) so it seems that this polling interval does not really have any effect. I get updates in OH only when voltage changes substantially.

Please let me know if there's any debug information I can look at to check if the binding works as it should.

Many thanks for your work! Jaume.

J-N-K commented 1 year ago

Please set the binding to TRACE debugging (log:set TRACE org.smarthomej.binding.tuya) and check if you see something similar to this

0:56:43.630 [DEBUG] [a.internal.local.handlers.TuyaEncoder] - bf3122fba025738ffc9pqa/192.168.0.10:6668: Sending DP_QUERY, payload {devId=bf3122fba025738ffc9pqa, uid=bf3122fba025738ffc9pqa, t=1661590603, dps=null, gwId=bf3122fba025738ffc9pqa}
10:56:43.631 [TRACE] [a.internal.local.handlers.TuyaEncoder] - bf3122fba025738ffc9pqa/192.168.0.10:6668: Sending encoded '000055AA000000070000000A00000088185193D0E2352FAC944F465A37C591B6D3759671C64744EF7D12996D659DE3054E7545364B810217406CDF27276CDBB74F5D865936443EFC852C56334074B3512A1A126FDCE1FB8EC33C1333754B9BE01C1B28D7FAA3DE2B814F2BDBF12E5749F5A3347CC7F5746CDA7E0A2AC644C9B5F371FF0E34A3695B9AC78A60DEB109A7CFC457CC0000AA55'
10:56:43.875 [TRACE] [a.internal.local.handlers.TuyaDecoder] - bf3122fba025738ffc9pqa/192.168.0.10:6668: Received encoded '000055AA000000070000000A0000008C00000000A8229EA0203F1DD86EF4F780B82924ADA19609AA7EEB6F68DD6FF0A5E8F8C78E480DCCC42E7A80235036B2E41E03BE1F1655EFF9E0DCD73AA8AAB2CE5CB62DC26B4130DCAAABAE4B55EE6720A3FE0E40C9ABBA0D275655589AC4D881457D08AC98A34EDC742FB99280F562D2FCEDD5A72CE35EC4505C8235C360231E0C19AE724587F3830000AA55'
10:56:43.876 [TRACE] [a.internal.local.handlers.TuyaDecoder] - bf3122fba025738ffc9pqa//192.168.0.10:6668: Decoded raw payload: {"dps":{"20":false,"21":"colour","22":30,"23":332,"24":"007702aa029a","25":"030e0d0000000000000001f401f4","26":0}}
10:56:43.877 [DEBUG] [a.internal.local.handlers.TuyaDecoder] - bf3122fba025738ffc9pqa//192.168.0.10:6668: Received MessageWrapper{commandType=DP_QUERY, content='{20=false, 21=colour, 22=30.0, 23=332.0, 24=007702aa029a, 25=030e0d0000000000000001f401f4, 26=0.0}'}
10:56:43.877 [TRACE] [ya.internal.handler.TuyaDeviceHandler] - 'tuya:tuyaDevice:bf3122fba025738ffc9pqa' received status message '{20=false, 21=colour, 22=30.0, 23=332.0, 24=007702aa029a, 25=030e0d0000000000000001f401f4, 26=0.0}'

of course with your device-id instead of bf3122fba025738ffc9pqa. You can disable the race logging with log:set DEFAULT org.smarthomej.binding.tuya.

obrador commented 1 year ago

Hi, I set up the TRACE log and I can see those HEART_BEAT messages, but there's an error at the end "Could not find channel..." I haven't defined all available channels in the Thing definition. BUT the value of the voltage (which is dp 20) is not updated in Openhab frontend, it still gets updated only when a signicative change in Voltage happens.

I can see that the log, in the JSON Decoded raw payload, dps 20 is always the same: 2215 in this case. It doesn't get updated with DP_QUERY

2022-08-27 18:50:47.585 [TRACE] [.internal.local.handlers.TuyaEncoder] - bfb3bcbf9d37765069l8fq/192.168.2.131:6668: Sending encoded '000055AA0000000F0000000A00000088C57940AF727E98ECDE70E802FC204E30C6219D51FE339E224D5AD3AB77C7541712AEAB5263ACCE3F74F9CC7902F87B24FE07200B7FFE451AB730A156DF2037FFB4232B94F05B0BECF252160897F40EE57BCAA20B071312E564E262EE8C32975835AA357C51B39D7D3FB4943AE7B620D85F9F4F95791D9B72C47670843E5F6C0E526973970000AA55'
2022-08-27 18:50:47.999 [TRACE] [.internal.local.handlers.TuyaDecoder] - bfb3bcbf9d37765069l8fq/192.168.2.131:6668: Received encoded '000055AA0000000F0000000A000000AC00000000DDECBA8288FA02DEBA8D6DD7DA2442AF4E04834F1A7BAF2FA3C7B56B29DC29B866D0E297F0E9A7241AFDAC3C0EF41D1742D36B8B8B15E2DDBC6BF32B12B5553F693671577D679DD078005602540D216F57A1620E5F3E46B9C72456DAC440DF08BAAE47A147541BE27F3B755527854D36E8D0891A38B5710B3F71D9A3FEB95E453EBB135127602F60AA58A8079B5D525C20AA294EF2E2A7A5C9D4C1AD00C2357A5975A4950000AA55'
2022-08-27 18:50:47.999 [TRACE] [.internal.local.handlers.TuyaDecoder] - bfb3bcbf9d37765069l8fq//192.168.2.131:6668: Decoded raw payload: {"dps":{"1":false,"9":0,"17":1,"18":0,"19":0,"20":2215,"21":1,"22":0,"23":0,"24":0,"25":0,"26":0,"38":"memory","40":"relay","41":false,"42":""}}
2022-08-27 18:50:48.000 [DEBUG] [.internal.local.handlers.TuyaDecoder] - bfb3bcbf9d37765069l8fq//192.168.2.131:6668: Received MessageWrapper{commandType=DP_QUERY, content='{1=false, 9=0.0, 17=1.0, 18=0.0, 19=0.0, 20=2215.0, 21=1.0, 22=0.0, 23=0.0, 24=0.0, 25=0.0, 26=0.0, 38=memory, 40=relay, 41=false, 42=}'}
2022-08-27 18:50:48.000 [TRACE] [a.internal.handler.TuyaDeviceHandler] - 'tuya:tuyaDevice:poolSwitch' received status message '{1=false, 9=0.0, 17=1.0, 18=0.0, 19=0.0, 20=2215.0, 21=1.0, 22=0.0, 23=0.0, 24=0.0, 25=0.0, 26=0.0, 38=memory, 40=relay, 41=false, 42=}'
2022-08-27 18:50:48.000 [DEBUG] [a.internal.handler.TuyaDeviceHandler] - Could not find channel for dp '9' in thing 'tuya:tuyaDevice:poolSwitch'
2022-08-27 18:50:48.001 [DEBUG] [a.internal.handler.TuyaDeviceHandler] - Could not find channel for dp '17' in thing 'tuya:tuyaDevice:poolSwitch'
2022-08-27 18:50:48.001 [DEBUG] [a.internal.handler.TuyaDeviceHandler] - Could not find channel for dp '18' in thing 'tuya:tuyaDevice:poolSwitch'
2022-08-27 18:50:48.001 [DEBUG] [a.internal.handler.TuyaDeviceHandler] - Could not find channel for dp '19' in thing 'tuya:tuyaDevice:poolSwitch'
2022-08-27 18:50:48.002 [DEBUG] [a.internal.handler.TuyaDeviceHandler] - Could not find channel for dp '21' in thing 'tuya:tuyaDevice:poolSwitch'
2022-08-27 18:50:48.002 [DEBUG] [a.internal.handler.TuyaDeviceHandler] - Could not find channel for dp '22' in thing 'tuya:tuyaDevice:poolSwitch'
2022-08-27 18:50:48.002 [DEBUG] [a.internal.handler.TuyaDeviceHandler] - Could not find channel for dp '23' in thing 'tuya:tuyaDevice:poolSwitch'
2022-08-27 18:50:48.002 [DEBUG] [a.internal.handler.TuyaDeviceHandler] - Could not find channel for dp '24' in thing 'tuya:tuyaDevice:poolSwitch'
2022-08-27 18:50:48.002 [DEBUG] [a.internal.handler.TuyaDeviceHandler] - Could not find channel for dp '25' in thing 'tuya:tuyaDevice:poolSwitch'
2022-08-27 18:50:48.003 [DEBUG] [a.internal.handler.TuyaDeviceHandler] - Could not find channel for dp '26' in thing 'tuya:tuyaDevice:poolSwitch'
2022-08-27 18:50:48.003 [DEBUG] [a.internal.handler.TuyaDeviceHandler] - Could not find channel for dp '38' in thing 'tuya:tuyaDevice:poolSwitch'
2022-08-27 18:50:48.003 [DEBUG] [a.internal.handler.TuyaDeviceHandler] - Could not find channel for dp '40' in thing 'tuya:tuyaDevice:poolSwitch'
2022-08-27 18:50:48.003 [DEBUG] [a.internal.handler.TuyaDeviceHandler] - Could not find channel for dp '41' in thing 'tuya:tuyaDevice:poolSwitch'
2022-08-27 18:50:48.004 [DEBUG] [a.internal.handler.TuyaDeviceHandler] - Could not find channel for dp '42' in thing 'tuya:tuyaDevice:poolSwitch'

Another thing that troubles me is that yesterday night I set poll_interval to 10, today it was automatically set to 0.

Thanks.

J-N-K commented 1 year ago

It looks like the polling is correct, a DP_QUERY is send and the response contains the values. What I do not understand what is missing here: the channel for the voltage is not updated with the value from the response? It seems it is found (because there is no "Could not find channel for dp '20'" message) and I can't imagine what might go wrong here.

Or is the issue that always the same value is in the response to the DP_QUERY message even if it changed in reality? In that case it seems that no read is triggered when the status is queried.

obrador commented 1 year ago

That's it! DP_QUERY runs fine, dp 20 is read, but the value of the sensor (voltage in my case) is always the same value, but, if I query it with SmartLife App it changes every time I open the device. It seems that the device does not update the "real voltage" when it's polled, only when a significant change in voltage exists (better explained in the first post). Do you think there's a way to trigger the device to update the values to the real ones?

J-N-K commented 1 year ago

No idea. Maybe sending a command to one of the other channels would do the trick. What are they named? Do you use the smartLife or the TuyaSmart app?

Edit: I have an EP2 (https://www.amazon.de/Usmart-Steckdose-Smart-WLAN-Sprachsteuerung/dp/B09ND8PRH8) which also measures voltage and current and even without polling the value is updated very often (I believe this is the last digit of the A/D-converter flipping from 0 to 1):

20:32:14.848 [INFO ] [openhab.event.ItemStateChangedEvent  ] - Item 'EP2AR_curvoltage' changed from 2200.0 to 2186.0
20:32:19.868 [INFO ] [openhab.event.ItemStateChangedEvent  ] - Item 'EP2AR_curvoltage' changed from 2186.0 to 2200.0
20:32:29.904 [INFO ] [openhab.event.ItemStateChangedEvent  ] - Item 'EP2AR_curvoltage' changed from 2200.0 to 2186.0
20:32:44.953 [INFO ] [openhab.event.ItemStateChangedEvent  ] - Item 'EP2AR_curvoltage' changed from 2186.0 to 2200.0
20:33:04.943 [INFO ] [openhab.event.ItemStateChangedEvent  ] - Item 'EP2AR_curvoltage' changed from 2200.0 to 2186.0
20:33:20.044 [INFO ] [openhab.event.ItemStateChangedEvent  ] - Item 'EP2AR_curvoltage' changed from 2186.0 to 2200.0
20:33:30.120 [INFO ] [openhab.event.ItemStateChangedEvent  ] - Item 'EP2AR_curvoltage' changed from 2200.0 to 2186.0
20:33:45.077 [INFO ] [openhab.event.ItemStateChangedEvent  ] - Item 'EP2AR_curvoltage' changed from 2186.0 to 2200.0
20:34:05.135 [INFO ] [openhab.event.ItemStateChangedEvent  ] - Item 'EP2AR_curvoltage' changed from 2200.0 to 2186.0
20:34:20.192 [INFO ] [openhab.event.ItemStateChangedEvent  ] - Item 'EP2AR_curvoltage' changed from 2186.0 to 2200.0
20:34:30.139 [INFO ] [openhab.event.ItemStateChangedEvent  ] - Item 'EP2AR_curvoltage' changed from 2200.0 to 2186.0
20:34:35.244 [INFO ] [openhab.event.ItemStateChangedEvent  ] - Item 'EP2AR_curvoltage' changed from 2186.0 to 2173.0
obrador commented 1 year ago

Hi J! I see... what do you mean "Sending a command to one of the other channels"?

Here's the device attributes got from Tuya Developer API:

{
  "result": {
    "category": "dlq",
    "functions": [
      {
        "code": "switch",
        "dp_id": 1,
        "type": "Boolean",
        "values": "{}"
      },
      {
        "code": "countdown_1",
        "dp_id": 9,
        "type": "Integer",
        "values": "{\"unit\":\"s\",\"min\":0,\"max\":86400,\"scale\":0,\"step\":1}"
      },
      {
        "code": "relay_status",
        "dp_id": 38,
        "type": "Enum",
        "values": "{\"range\":[\"power_off\",\"power_on\",\"last\"]}"
      },
      {
        "code": "light_mode",
        "dp_id": 40,
        "type": "Enum",
        "values": "{\"range\":[\"relay\",\"pos\",\"none\",\"on\"]}"
      }
    ],
    "status": [
      {
        "code": "switch",
        "dp_id": 1,
        "type": "Boolean",
        "values": "{}"
      },
      {
        "code": "countdown_1",
        "dp_id": 9,
        "type": "Integer",
        "values": "{\"unit\":\"s\",\"min\":0,\"max\":86400,\"scale\":0,\"step\":1}"
      },
      {
        "code": "add_ele",
        "dp_id": 17,
        "type": "Integer",
        "values": "{\"min\":0,\"max\":50000,\"scale\":3,\"step\":100}"
      },
      {
        "code": "cur_current",
        "dp_id": 18,
        "type": "Integer",
        "values": "{\"unit\":\"mA\",\"min\":0,\"max\":100000,\"scale\":0,\"step\":1}"
      },
      {
        "code": "cur_power",
        "dp_id": 19,
        "type": "Integer",
        "values": "{\"unit\":\"W\",\"min\":0,\"max\":99999,\"scale\":1,\"step\":1}"
      },
      {
        "code": "cur_voltage",
        "dp_id": 20,
        "type": "Integer",
        "values": "{\"unit\":\"V\",\"min\":0,\"max\":5000,\"scale\":1,\"step\":1}"
      },
      {
        "code": "test_bit",
        "dp_id": 21,
        "type": "Integer",
        "values": "{\"min\":0,\"max\":5,\"scale\":0,\"step\":1}"
      },
      {
        "code": "voltage_coe",
        "dp_id": 22,
        "type": "Integer",
        "values": "{\"min\":0,\"max\":1000000,\"scale\":0,\"step\":1}"
      },
      {
        "code": "electric_coe",
        "dp_id": 23,
        "type": "Integer",
        "values": "{\"min\":0,\"max\":1000000,\"scale\":0,\"step\":1}"
      },
      {
        "code": "power_coe",
        "dp_id": 24,
        "type": "Integer",
        "values": "{\"min\":0,\"max\":1000000,\"scale\":0,\"step\":1}"
      },
      {
        "code": "electricity_coe",
        "dp_id": 25,
        "type": "Integer",
        "values": "{\"min\":0,\"max\":1000000,\"scale\":0,\"step\":1}"
      },
      {
        "code": "fault",
        "dp_id": 26,
        "type": "Bitmap",
        "values": "{\"label\":[\"ov_cr\",\"ov_vol\",\"ov_pwr\",\"ls_cr\",\"ls_vol\",\"ls_pow\"]}"
      },
      {
        "code": "relay_status",
        "dp_id": 38,
        "type": "Enum",
        "values": "{\"range\":[\"power_off\",\"power_on\",\"last\"]}"
      },
      {
        "code": "light_mode",
        "dp_id": 40,
        "type": "Enum",
        "values": "{\"range\":[\"relay\",\"pos\",\"none\",\"on\"]}"
      },
      {
        "code": "child_lock",
        "dp_id": 41,
        "type": "Boolean",
        "values": "{}"
      }
    ]
  },
  "success": true,
  "t": 1661683526942,
  "tid": "86fbbb5f26be11ed86c5bed3cc205f4d"
}

I use SmartLife. Every time I check the App voltage is updated, and when it's updated in the App it's also updated in OH, but without using the App, the updates only happen when there's a substantial change in real voltage. Maybe it's my device (DIN Wifi Switch.

What do you think SmartLife App does when I access the device in order to trigger an update of the the values? do you think there's a way to trigger this update locally using your binding? Any ideas where to explore?

Many thanks for your time! Jaume

J-N-K commented 1 year ago

If you cut off internet access, can you still update the measured voltage in the app? If this is the case, you can see what is sent to the device in the cloud later with "Debug Device". Maybe we can copy that. If you can't update the value without Internet access, then I think it's not possible to do it locally.

obrador commented 1 year ago

Thanks J. At the time I don't have much time to look at the issue, sorry, but I will look into it when I have more time. I've ordered some different power outlets with energy meter and I can see if they updates the state more often and compare. By now I will go on with my setup, which is not perfect but half-works.

Thank you again. Jaume.

J-N-K commented 1 year ago

I have changed the code after a discussion with the ioBroker guys. Can you uninstall/re-install and try again?

obrador commented 1 year ago

Awsome! it works like a charm now, sensor values updated every 10 secons (the parameter I wrote)

I hope this would be useful to other users as well.

Many many thanks! Jaume.

andreground commented 5 months ago

Hi!

Similar issue here with another Tuya Power Meter. Every time I hit refresh from the Tuya app I can see the consumption value (19) updated both in the app and in Openhab.

Tried to set polling to 10 seconds without any luck.

This is the message I receive when I trigger a refresh from the app:

2024-01-30 21:33:57.077 [DEBUG] [.internal.local.handlers.TuyaDecoder] - xyz1234bogo/192.168.50.196:6668: Received MessageWrapper{commandType=STATUS, content='TcpStatusPayload{protocol=4, devId='', gwId='', uid='', t=1706650436, dps=null, data=Data{dps={20=2162.0, 18=11399.0, 19=24185.0}}}'}

Is there anything I could do to help diagnose the problem / fix it?

Thanks in advance!