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

Periodically send update dps command #549

Merged
merged 14 commits into from
Jan 2, 2022

Conversation

vmartinv
Copy link

@vmartinv vmartinv commented Aug 27, 2021

Problem

There has been some reports of devices not updating their state unless the app is open (#547, #539).

Solution

Several users found out that periodically sending the update dps command (available in tinytuya) forces the state to be updated. I implemented this feature here.

Update:

Upon request, the feature can now be controlled through the config and is disabled by default:

Screenshot 2021-11-24 at 00 08 31

example using yaml:

- host: 192.168.0.xx
  device_id: xxx
  local_key: xxx
  friendly_name: Robaxo_Heater
  protocol_version: "3.3"
  scan_interval: 15
  entities:
    platform: switch
    friendly_name: Robasxo_Heater
    id: 1
    current_consumption: 19`

Testing

The command is being sent but I don't have any devices that experienced this problem to verify it works.
UPDATE: this has been verified to be working with several devices

example log:

2021-11-26 23:40:00 DEBUG (MainThread) [custom_components.localtuya.pytuya] [536...d2f] Sending command updatedps (device type: type_0a)
2021-11-26 23:40:00 DEBUG (MainThread) [custom_components.localtuya.pytuya] [536...d2f] Send payload: b'{"dpId":[4,5,6,18,19,20]}'
2021-11-26 23:40:00 DEBUG (MainThread) [custom_components.localtuya.pytuya] [536...d2f] Waiting for sequence number -101
2021-11-26 23:40:02 DEBUG (MainThread) [custom_components.localtuya.pytuya] [536...d2f] Dispatching message TuyaMessage(seqno=0, cmd=8, retcode=0, payload=b'xxxxxx', crc=xxxx)
2021-11-26 23:40:02 DEBUG (MainThread) [custom_components.localtuya.pytuya] [536...d2f] Got status type updatedps response
2021-11-26 23:40:02 DEBUG (MainThread) [custom_components.localtuya.pytuya] [536...d2f] Decrypted payload: {"devId":"xxxxxx","dps":{"38":"xxxxxx"},"t":1637970001}

@vmartinv vmartinv force-pushed the update_interval branch 2 times, most recently from 4a9958e to d59ee11 Compare August 27, 2021 16:26
Copy link

@elax46 elax46 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the code works perfectly, personally tested

Copy link

@AdmiralStipe AdmiralStipe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me and my Robaxo plugs perfectly.

Copy link

@Leatherface75 Leatherface75 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here works fine nice work.

@vmartinv
Copy link
Author

vmartinv commented Aug 31, 2021

Code updated, now the storage shouldn't grow that much. (context here)

@AdmiralStipe
Copy link

Code updated, now the storage shouldn't grow that much. (context here)

Some troubles here... after updating the common.py the connection to the plugs gets lost - reload integration doesn't help. THe log gets flooded with messages like this every second:
2021-08-31 15:49:08 ERROR (MainThread) [homeassistant] Error doing job: Exception in callback _SelectorDatagramTransport._read_ready() Traceback (most recent call last): File "/usr/local/lib/python3.9/asyncio/events.py", line 80, in _run self._context.run(self._callback, *self._args) File "/usr/local/lib/python3.9/asyncio/selector_events.py", line 1029, in _read_ready self._protocol.datagram_received(data, addr) File "/config/custom_components/localtuya/discovery.py", line 70, in datagram_received self.device_found(decoded) File "/config/custom_components/localtuya/discovery.py", line 79, in device_found self._callback(device) File "/config/custom_components/localtuya/__init__.py", line 194, in _device_discovered device.connect() AttributeError: 'TuyaDevice' object has no attribute 'connect'

@vmartinv
Copy link
Author

Code updated, now the storage shouldn't grow that much. (context here)

Some troubles here... after updating the common.py the connection to the plugs gets lost - reload integration doesn't help. THe log gets flooded with messages like this every second:
2021-08-31 15:49:08 ERROR (MainThread) [homeassistant] Error doing job: Exception in callback _SelectorDatagramTransport._read_ready() Traceback (most recent call last): File "/usr/local/lib/python3.9/asyncio/events.py", line 80, in _run self._context.run(self._callback, *self._args) File "/usr/local/lib/python3.9/asyncio/selector_events.py", line 1029, in _read_ready self._protocol.datagram_received(data, addr) File "/config/custom_components/localtuya/discovery.py", line 70, in datagram_received self.device_found(decoded) File "/config/custom_components/localtuya/discovery.py", line 79, in device_found self._callback(device) File "/config/custom_components/localtuya/__init__.py", line 194, in _device_discovered device.connect() AttributeError: 'TuyaDevice' object has no attribute 'connect'

Looks like the code was altered... Copy the whole folder again, add the changes and reboot.

@AdmiralStipe
Copy link

AdmiralStipe commented Aug 31, 2021

Looks like the code was altered... Copy the whole folder again, add the changes and reboot.

Did that - completely removed the original component and replaced it by yours - the error mentioned before does not occur anymore, but the integration itself is also not working... the consumption data is still refreshing accordingly, but the state of the plugs (on /off) is not - by clicking on the button in HA, the plugs toggle only once, but the state doesn't change in HA and also further clicking the button has no function. Furthermore, some plugs go offline shortly after. Something must be wrong., although log shows no errors anymore...

@AdmiralStipe
Copy link

AdmiralStipe commented Aug 31, 2021

Might be also related to your previous change in init.py (fix auto-update ip feature), which I was not using until today's try.

Until todays change I was using all files original, the only change was in pytuya/init.py (send update dps command with heartbeat). That was working, but flooding the database.
Then, I just changed the common.py (update state only on changes) and the error from above appeared.
Then I deleted all the localtuya files and used all files from your latest commit (including all the changes, also fix auto-update ip feature). At that point the error was gone, but the integration stopped responding to status change and clicks ( I had to switch the plugs manually with the physical button), yet the consumption was still managed properly - so, dps 18, 19 and 20 were refreshing, but dps 1 was not). Log shows nothing unusual, all dps changes are received, that means 18, 19 and 20, but also 1 - only it seems not to be handled properly.

- - - - - - - - - - - -
2021-08-31 18:57:09 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Dispatching message TuyaMessage(seqno=0, cmd=8, retcode=0, payload=b'3.3\x00\x00\x00\x00\x00\x00H\\\x00\x00\x00\x01\xcaD\xde\xed/O6\xd2\x12[\xac\xcc\xd8|\xa5\x89W>\xe6\xca\x01\xe0\x9c\x1f\xac\xfe\xf46r\xd9rzM\xf5ql)\xb0M+I\x85\xb4\xfb\xbfpR\xa9H\x9d\x05-\x9bN\x15\x90\xc1`\xe1\x12\xe5\x0e\xa5L\x01\x16\x83\x1b>\xb1\x186\xfb\xeb\x98\x89*\x17\xe1\x88', crc=3861284975)
2021-08-31 18:57:09 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Got status update
2021-08-31 18:57:09 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Decrypted payload: {"devId":"630452752462ab289a9b","dps":{"1":true},"t":1630429028}
2021-08-31 18:57:12 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Dispatching message TuyaMessage(seqno=0, cmd=8, retcode=0, payload=b'3.3\x00\x00\x00\x00\x00\x00H]\x00\x00\x00\x01\xcaD\xde\xed/O6\xd2\x12[\xac\xcc\xd8|\xa5\x89W>\xe6\xca\x01\xe0\x9c\x1f\xac\xfe\xf46r\xd9rz\xbc\xa4\xf5\x06\xf7\xbc\xd561\x8e\x11\xa783>\x1c\xcbOKwH\x0f\xac=\xa9O\xb7z=\x99\xb9\x0b\xec\xb2n/G\x16\xee.\xa4\xde\xdc\xd1);\xb7\xffY\x00\x81=\x87\x0b%\x8d\x1a\xdb\xe5\xf6\xfa,\xaa\x00', crc=2233869442)
2021-08-31 18:57:12 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Got status update
2021-08-31 18:57:12 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Decrypted payload: {"devId":"630452752462ab289a9b","dps":{"18":65,"19":46,"20":2361},"t":1630429031}
2021-08-31 18:57:15 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Sending command heartbeat (device type: type_0a)
2021-08-31 18:57:15 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Send payload: b'{}'
2021-08-31 18:57:15 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Waiting for sequence number -100
2021-08-31 18:57:15 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Dispatching message TuyaMessage(seqno=0, cmd=9, retcode=0, payload=b'', crc=2958142211)
2021-08-31 18:57:15 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Got heartbeat response
2021-08-31 18:57:15 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Decrypted payload: {}
2021-08-31 18:57:15 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] updatedps() entry (dev_type is type_0a)
2021-08-31 18:57:15 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Send payload: b'{"dpId":[18,19,20]}'
2021-08-31 18:57:15 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Dispatching message TuyaMessage(seqno=0, cmd=8, retcode=0, payload=b'3.3\x00\x00\x00\x00\x00\x00H^\x00\x00\x00\x01\xcaD\xde\xed/O6\xd2\x12[\xac\xcc\xd8|\xa5\x89W>\xe6\xca\x01\xe0\x9c\x1f\xac\xfe\xf46r\xd9rz\xd8\xe8<G\x12,`\xb6\xd2\xa4n\xa3\xcdL\x98uE~aO\x8a\xcc\xdb\x1e\t#{/\xc6?q\x0e\xe0\xc6om\xe1K\x03\x7f5\x05\x92\x15\x0c\xe6*y', crc=1253622043)
2021-08-31 18:57:15 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Got status update
2021-08-31 18:57:15 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Decrypted payload: {"devId":"630452752462ab289a9b","dps":{"18":61,"19":83},"t":1630429034}
2021-08-31 18:57:25 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Sending command heartbeat (device type: type_0a)
2021-08-31 18:57:25 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Send payload: b'{}'
2021-08-31 18:57:25 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Waiting for sequence number -100
2021-08-31 18:57:25 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Dispatching message TuyaMessage(seqno=0, cmd=9, retcode=0, payload=b'', crc=2958142211)
2021-08-31 18:57:25 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Got heartbeat response
2021-08-31 18:57:25 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Decrypted payload: {}
2021-08-31 18:57:25 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] updatedps() entry (dev_type is type_0a)
2021-08-31 18:57:25 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Send payload: b'{"dpId":[18,19,20]}'
2021-08-31 18:57:25 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Dispatching message TuyaMessage(seqno=0, cmd=8, retcode=0, payload=b'3.3\x00\x00\x00\x00\x00\x00H_\x00\x00\x00\x01\xcaD\xde\xed/O6\xd2\x12[\xac\xcc\xd8|\xa5\x89W>\xe6\xca\x01\xe0\x9c\x1f\xac\xfe\xf46r\xd9rz\xfeC\xcc\xb5\x98#0\xad\nH[\xdb\x1f\x1fY\x9a<\xad\xf2\x192T\xd6\x95\x0b\x96\x05\xd7\xae\xd2"\xaf\xc67Y\xc3N\x91,^\x08\xa1KF)\xf5\x19=Y\x00\x81=\x87\x0b%\x8d\x1a\xdb\xe5\xf6\xfa,\xaa\x00', crc=330124278)
2021-08-31 18:57:25 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Got status update
2021-08-31 18:57:25 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Decrypted payload: {"devId":"630452752462ab289a9b","dps":{"18":60,"19":82,"20":2369},"t":1630429044}
2021-08-31 18:57:35 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Sending command heartbeat (device type: type_0a)
2021-08-31 18:57:35 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Send payload: b'{}'
2021-08-31 18:57:35 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Waiting for sequence number -100
2021-08-31 18:57:35 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Dispatching message TuyaMessage(seqno=0, cmd=9, retcode=0, payload=b'', crc=2958142211)
2021-08-31 18:57:35 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Got heartbeat response
2021-08-31 18:57:35 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Decrypted payload: {}
2021-08-31 18:57:35 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] updatedps() entry (dev_type is type_0a)
2021-08-31 18:57:35 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Send payload: b'{"dpId":[18,19,20]}'
2021-08-31 18:57:35 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Dispatching message TuyaMessage(seqno=0, cmd=8, retcode=0, payload=b'3.3\x00\x00\x00\x00\x00\x00H`\x00\x00\x00\x01\xcaD\xde\xed/O6\xd2\x12[\xac\xcc\xd8|\xa5\x89W>\xe6\xca\x01\xe0\x9c\x1f\xac\xfe\xf46r\xd9rzS\xac\xa0&\x9f6&\xf9\x89AQ\xc4\xe0\xf7Fl\xee\x8bN\x18\rnl\xe0r\x159\x98\xca*<\x06\x12y\x94s{\x97\x17~e\x96\xd9d,}\x8f\xcf', crc=568291823)
2021-08-31 18:57:35 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Got status update
2021-08-31 18:57:35 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Decrypted payload: {"devId":"630452752462ab289a9b","dps":{"19":83,"20":2357},"t":1630429054}
2021-08-31 18:57:40 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Dispatching message TuyaMessage(seqno=0, cmd=8, retcode=0, payload=b'3.3\x00\x00\x00\x00\x00\x00Ha\x00\x00\x00\x01\xcaD\xde\xed/O6\xd2\x12[\xac\xcc\xd8|\xa5\x89W>\xe6\xca\x01\xe0\x9c\x1f\xac\xfe\xf46r\xd9rz\x1a\xa1Eo8\xd3\xef\x9e\xeaJ\x15\xb8p;\x1b\x0f\xa9\xeaL\xaf\xf8:\x15\xb8\xfb\xf7n>\xff6M1Y\x00\x81=\x87\x0b%\x8d\x1a\xdb\xe5\xf6\xfa,\xaa\x00', crc=1988828547)
2021-08-31 18:57:40 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Got status update
2021-08-31 18:57:40 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Decrypted payload: {"devId":"630452752462ab289a9b","dps":{"1":false},"t":1630429059}
2021-08-31 18:57:45 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Sending command heartbeat (device type: type_0a)
2021-08-31 18:57:45 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Send payload: b'{}'
2021-08-31 18:57:45 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Waiting for sequence number -100
2021-08-31 18:57:45 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Dispatching message TuyaMessage(seqno=0, cmd=9, retcode=0, payload=b'', crc=2958142211)
2021-08-31 18:57:45 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Got heartbeat response
2021-08-31 18:57:45 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Decrypted payload: {}
2021-08-31 18:57:45 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] updatedps() entry (dev_type is type_0a)
2021-08-31 18:57:45 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Send payload: b'{"dpId":[18,19,20]}'
2021-08-31 18:57:45 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Dispatching message TuyaMessage(seqno=0, cmd=8, retcode=0, payload=b"3.3\x00\x00\x00\x00\x00\x00Hb\x00\x00\x00\x01\xcaD\xde\xed/O6\xd2\x12[\xac\xcc\xd8|\xa5\x89W>\xe6\xca\x01\xe0\x9c\x1f\xac\xfe\xf46r\xd9rz\xad\x94\x9f?1\x7f\x87\xe7\x9c\x04\xa7\xc7\xcc;\xd8\x1fc\xf1\xb25']\xb5\xa3ti\x02\xb8\x99G\x0f.#\xf0pMc\x82M\xc63\xe5\x0fpm\x7fS\xc6", crc=2494347060)
2021-08-31 18:57:45 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Got status update
2021-08-31 18:57:45 DEBUG (MainThread) [custom_components.localtuya.pytuya] [630...a9b] Decrypted payload: {"devId":"630452752462ab289a9b","dps":{"18":0,"19":0,"20":2361},"t":1630429064}
- - - - - - - - - - - -

If I use all files from your commit and just replace your init.py with the original one again (so eliminating the auto-update ip) I again have the error from above (Exception in callback _SelectorDatagramTransport._read_ready()). Also, nothing is working, plugs are unavailable. Therefore I assume, that there must be some incompatibility with it on my side.

@vmartinv
Copy link
Author

@AdmiralStipe My bad, I updated the code. What about now?

@AdmiralStipe
Copy link

@AdmiralStipe My bad, I updated the code. What about now?

Well, it works without errors now, but the database flooding is the same as before - state_change with {} is still registered every second in average.

db2

@Leatherface75
Copy link

Leatherface75 commented Sep 1, 2021

That's something that is expected with many plugs i guess.
My db with a lot of other things gives me a DB increase with almost 1gb/day.

@AdmiralStipe
Copy link

AdmiralStipe commented Sep 1, 2021

That's something that is expected with many plugs i guess.

Sure thing, but as I understood, Martin is able to alter the code in a way, that useless state_change records (empty ones) are not written to the database. It saves space and prolongues the disk life expectancy (less writing) at the same time.

@Leatherface75
Copy link

That's something that is expected with many plugs i guess.

Sure thing, but as I understood, Martin is able to alter the code in a way, that useless state_change records (empty ones) are not written to the database. It saves space and the prolongues the disk life expectancy (less writing) at the same time.

If you mean when it shows 0W then i agree maybe that could be ignored or atleast an option for that.
But that seems to mostly happen with low values with a few W so that's probably issues with low values that many powermeters have problems with.

@AdmiralStipe
Copy link

If you mean when it shows 0W then i agree maybe that could be ignored or atleast an option for that.
But that seems to mostly happen with low values with a few W so that's probably issues with low values that many powermeters have problems with.

No, not when it shows 0W... in "normal" operation the plug sends the state, when it changes (otherwise not)... when HA receives that, it acts correspondingly and also writes the state change to the database, which is correct.
But when you constantly force poll the state from plugs, they report it even if nothing has changed (for example, when the plug is off or when the consumption is stable for hours, regardless of it's amount). Therefore you get a lot of empty state changes, which don't need to be written to the database as the previously written state is still in force. That's what I meant by saving disk space and life expectancy (and to add processor power :) ).

@Leatherface75
Copy link

Leatherface75 commented Sep 2, 2021

If you mean when it shows 0W then i agree maybe that could be ignored or atleast an option for that.
But that seems to mostly happen with low values with a few W so that's probably issues with low values that many powermeters have problems with.

No, not when it shows 0W... in "normal" operation the plug sends the state, when it changes (otherwise not)... when HA receives that, it acts correspondingly and also writes the state change to the database, which is correct.
But when you constantly force poll the state from plugs, they report it even if nothing has changed (for example, when the plug is off or when the consumption is stable for hours, regardless of it's amount). Therefore you get a lot of empty state changes, which don't need to be written to the database as the previously written state is still in force. That's what I meant by saving disk space and life expectancy (and to add processor power :) ).

Ok then i understand.
But standby values is a problem for me but that's nothing wrong with the plugin but with many plugs.
Can show 3-4W and then 0W and then 3-4W again and in these cases maybe there is room for some improvement.
But it should know if it's real 0W so some check for previous values is needed.

@Leatherface75
Copy link

If you mean when it shows 0W then i agree maybe that could be ignored or atleast an option for that.
But that seems to mostly happen with low values with a few W so that's probably issues with low values that many powermeters have problems with.

No, not when it shows 0W... in "normal" operation the plug sends the state, when it changes (otherwise not)... when HA receives that, it acts correspondingly and also writes the state change to the database, which is correct.
But when you constantly force poll the state from plugs, they report it even if nothing has changed (for example, when the plug is off or when the consumption is stable for hours, regardless of it's amount). Therefore you get a lot of empty state changes, which don't need to be written to the database as the previously written state is still in force. That's what I meant by saving disk space and life expectancy (and to add processor power :) ).

Ok then i understand.
But standby values is a problem for me but that's nothing wrong with the plugin but with many plugs.
Can show 3-4W and then 0W and then 3-4W again and in these cases maybe there is room for some improvement.
But it should know if it's real 0W so some check for previous values is needed.

Fixed this with a workaround.

      htpc_vardagsrum_power:
        friendly_name: "HTPC Vardagsrum Power"
        unit_of_measurement: 'W'
        device_class: energy
        icon_template: mdi:flash
        value_template: >
          {% set val = states('sensor.htpc_vardagsrum_power_tuya') | float %}
          {% if val != 0 %}
             {{ val }}
          {% else %}
             {{ states('sensor.htpc_vardagsrum_power') }}
          {% endif %}

@AdmiralStipe
Copy link

Fixed this with a workaround.

      htpc_vardagsrum_power:
        friendly_name: "HTPC Vardagsrum Power"
        unit_of_measurement: 'W'
        device_class: energy
        icon_template: mdi:flash
        value_template: >
          {% set val = states('sensor.htpc_vardagsrum_power_tuya') | float %}
          {% if val != 0 %}
             {{ val }}
          {% else %}
             {{ states('sensor.htpc_vardagsrum_power') }}
          {% endif %}

Fixed what? I don't think this template reduces number of writings to the database and consequently database size, which was my request...

@Leatherface75
Copy link

Leatherface75 commented Sep 6, 2021

Fixed this with a workaround.

      htpc_vardagsrum_power:
        friendly_name: "HTPC Vardagsrum Power"
        unit_of_measurement: 'W'
        device_class: energy
        icon_template: mdi:flash
        value_template: >
          {% set val = states('sensor.htpc_vardagsrum_power_tuya') | float %}
          {% if val != 0 %}
             {{ val }}
          {% else %}
             {{ states('sensor.htpc_vardagsrum_power') }}
          {% endif %}

Fixed what? I don't think this template reduces number of writings to the database and consequently database size, which was my request...

I talked about my problem there my plugs show 0W for everything below 3W or something not your problem.
This template ignores 0W values.

Ok then i understand.
But standby values is a problem for me but that's nothing wrong with the plugin but with many plugs.
Can show 3-4W and then 0W and then 3-4W again and in these cases maybe there is room for some improvement.
But it should know if it's real 0W so some check for previous values is needed.

@nikogeek
Copy link

nikogeek commented Sep 8, 2021

the code works perfectly, personally tested

Excuse me, I'm new to this. I have localtuya and 2 devices are no longer usable, but I restart homeassistant and they are available again. Would this code work for me? where should i put it? thanks since now.

@jeremysherriff
Copy link

Works perfectly for my Kogan wifi smart plugs.

@rospogrigio @postlund what do we need to get this PR merged?

@elax46
Copy link

elax46 commented Oct 6, 2021

Works perfectly for my Kogan wifi smart plugs.

@rospogrigio @postlund what do we need to get this PR merged?

it is the same thing that I have been wondering for some time @rospogrigio

@rospogrigio
Copy link
Owner

@rospogrigio @postlund what do we need to get this PR merged?

Sorry guys I didn't follow this thread with much attention because I'm not having these issues. I did not merge this because it seemed to me that this fix was working partially and somebody still has issues, but if you confirm that it's working fine I'll merge and release it.
Is there anybody still having problems even with this PR?
Let me know, thanks

@sibowler
Copy link

sibowler commented Oct 8, 2021

@rospogrigio - It's been working fine for me. But I'm only about a week into bringing this change onto my system.

@JurajNyiri
Copy link

Tested and works flawlessly here too.

@jeremysherriff
Copy link

@vmartinv can you please merge in the other commits into your fork? I'm not sure if there's a way for me to do this, my git knowledge is limited.

@jeremysherriff
Copy link

jeremysherriff commented Dec 16, 2021

@rospogrigio the user-facing logic in the PR has not changed. You can see the config flow GUI change here and this is implemented in yaml using scan_interval: as per normal Home Assistant convention. A missing scan_interval or zero value disables the additional polling logic.

There was some discussion about HA database bloat; this has been addressed by only returning values that have changed when calling update_dps().

The stability issues have been resolved; they were caused by polling all known dps values (e.g. [1,9,18,19,20,21]) with the 0x12 refresh command which caused the device to become unresponsive periodically. The resolution is a whitelist of known-good dps values (currently [18,19,20]) - now only the intersecting set of known dps values and the whitelist are refreshed. This whitelist covers all devices mentioned in the comments here and that have had testers provide feedback.

If there are future dps values that need to be added to this whitelist, or values that prove unstable for future devices and need to be removed, it is proposed that it becomes a user-editable list (in the form of a correctly formatted string e.g. "[18,19,22]") added to the config flow GUI and a corresponding value in yaml. This should be a trivial change if needed, however we have not included it for now as there are no indications of other dps values being needed, and we'd like to keep the configuration simple.

@vmartinv
Copy link
Author

merged

@jeremysherriff
Copy link

jeremysherriff commented Dec 16, 2021

@rospogrigio can you open the wiki to allow collaborators and I'll add some doco for this new feature

@jeremysherriff
Copy link

jeremysherriff commented Dec 16, 2021

@vmartinv this needs some supporting edits to README.md and info.md, in the YAML setup instructions (updating to include scan_interval with a comment as to when/why to use) and in the config flow gui instructions (updated screenshot showing refresh interval and comments about when/why to use). Probably needs a brief mention in the Energy monitoring values section too.

Is this something you can do and include in the PR for us? If not, I'll fork the repo and submit another PR to accompany this one some time this weekend.

Edit: 30 seconds seems to be a good value to put in the examples, and matches with the current Xiaomi integration defaults for their power plugs.

Copy link

@AdmiralStipe AdmiralStipe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works perfectly.

@rospogrigio
Copy link
Owner

I am testing this PR and it seems to be working fine. Before I merge it, I'd ask you to update the README.md and info.md documenting the new option and its impact on the DB size, please.
Thank you!

jeremysherriff added a commit to jeremysherriff/localtuya that referenced this pull request Dec 19, 2021
jeremysherriff added a commit to jeremysherriff/localtuya that referenced this pull request Dec 19, 2021
@jeremysherriff
Copy link

@rospogrigio I can't substantiate any significant impact on the database size, since commit update state only on changes. Yes there will be some growth, but no more than any other system that implements energy monitoring. This PR does not implement long-term statistics so any values are only retained for the "normal" length (I see you have a to-do for implementing native energy monitoring capabilities which may trigger long-term statistics, but this is not that). I don't believe any comment is necessary.

If you do not agree, I'll edit PR #667 to include a comment but I really do not think it is warranted.

@jeremysherriff
Copy link

I'd really like to get rid of this:
image
..by getting this PR merged.

@vmartinv
Copy link
Author

@jeremysherriff I think you can configure your PR to be on top of mine (stacked pull requests)

@AdmiralStipe
Copy link

So, is there anything else we need to do to finally get this PR and #667 merged (and #663 at the same time would be nice too)?

Copy link

@styelz styelz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works great thank you ! Really happy now I have the Kogan Smart Plugs working with power monitoring in HA

@rospogrigio rospogrigio merged commit 5427595 into rospogrigio:master Jan 2, 2022
@rospogrigio
Copy link
Owner

OK PR merged, now I'll proceed with the other 2...

@InsanityFlea
Copy link

My entity still only updates when the app is open, time to dig into the logs.

@codingcatgirl
Copy link

This is sadly not working for me either. When i add one of my smart plugs with energy monitoring with todays new update, only two DPS show up. The ones that would contain the energy consumption information are missing. When i tried this a few months ago when i tried localtuya for the first time, a lot more DPS would show up and provide data as long as the app was running.

Am i overlooking something that i should do?

@jeremysherriff
Copy link

@InsanityFlea @codingcatgirl please open new issues, posting your YAML or screenshots of your config as appropriate and we'll try to help.

@codingcatgirl
Copy link

@jeremysherriff
I have created #686, #687 and #688.

@JAKAMI99
Copy link

Thanks, fix works perfectly for me <3

@RaunoVV
Copy link

RaunoVV commented Jan 17, 2023

It should be sent for devices with firmware of 3.4 also as it was not updating for my new Nedis plug, locally updated the code to allow update_dps to be ran for 3.3 and 3.4 and consumptions values started coming.

Also there's somekind of bug with self._dev_config_entry[CONF_SCAN_INTERVAL]) not being INT when configured from UI - there were errors in logs about str being compared to int on common.py line 273 ( self._dev_config_entry[CONF_SCAN_INTERVAL] > 0 )

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

Successfully merging this pull request may close these issues.