Home Assistant – Zigbee device updates : ZHA & ZHA_TOOLKIT OTA

Introduction

I quickly put together this article to provide guidance on how I flashed my cheap AliExpress ZigBee Smart plug. These are also known as Tuya TS011F.

Consider this as a work in progress article.

The purpose is to fix a firmware flaw causing the plugs to turn off when reaching 251 volts. Even if this sounds like a good idea, it’s actually a problem since spikes up to 263 volts are allowed as per European quality standard (fixme, get link to specs).

Requirements

This post deals with a bunch of requirements, adapt for your own setup (spoiler : it might work, or not) :

What’s your firmware?

Before flashing your plugs, you might want to know where to find the firmware version.

Navigate to Settings > Integrations and click the “xx devices” on the card.

This opens the list of all your (paired) Zigbee devices.

Filter on some keyword (mine are all named “Prise Xz”).

Note this view provides overview of Manufacturer and Model. If your plug doesn’t update, this might be related to an unknown manufacturer ID.

Firmware version is shown under Device info. If the “Firmware” line is missing, your plug hasn’t been updated..

After what, the TS011F plugs started reporting below Firmware versions:

  • _TZ3000_zloso4jk : 0x0000004a
  • _TZ3000_cphmq0q7 : 0x00000043
  • _TZ3000_typdpbpg : 0x0000004d

Update 2023-02-03, here are updated firmware versions:

  • _TZ3000_zloso4jk : 0x000000c0 -> All 5 updated fine but one had to be disconnected and re-plugged
  • _TZ3000_cphmq0q7 : 0x00000043 -> None have been updated (out of 8)
  • _TZ3000_typdpbpg : 0x000000c0 -> All 6 updated fine

Flashing a Zigbee plug

Send OTA notify

If you reach down to this, you are pretty well done with this.

Go to your HA instance under Developer Tools > Services (HA doc page).

From this page, you will be able to run an OTA notification using ZHA, causing the device to update.

If find it easier in YAML mode, so you can copy/paste below code. Don’t forget to change “ieee” to match the device name you want to flash.

service: zha_toolkit.ota_notify
data:
  ieee: sensor.prise_7z_boiler_electrical_measurement
  download: true
  path: /config/zb_ota

More about OTA process

The OTA process automatically checks up-to-date firmware from a JSON file compiled by contributors with URLs from vendors’ OTA (see https://github.com/Koenkk/zigbee-OTA/blob/master/index.json ).

However after update, the OTA is not triggered automatically, you must go through the process again to keep your devices up-to-date (if any firmware is available and added to the list).

Here is example for one of my devices as per Home Assistant ZHA diagnostic output, note the “manufacturer_code”.

...
  "data": {
    "ieee": "**REDACTED**",
    "nwk": 2706,
    "manufacturer": "_TZ3000_typdpbpg",
    "model": "TS011F",
    "name": "_TZ3000_typdpbpg TS011F",
    "quirk_applied": true,
    "quirk_class": "zhaquirks.tuya.ts011f_plug.Plug",
    "manufacturer_code": 4417,
...

The correct firmware is detected from manufacturerCode (dec 4417 = 0x1141).

    {
        "fileVersion": 192,
        "fileSize": 307682,
        "manufacturerCode": 4417,
        "imageType": 54179,
        "sha512": "01939ca4fc790432d2c233e19b2440c1e0248d2ce85c9299e0b88928cb2341de675350ac7b78187a25f06a2768f93db0a17c4ba950b60c82c072e0c0833cfcfb",
        "url": "https://images.tuyaeu.com/smart/firmware/upgrade/20220907/1662545193-oem_zg_tl8258_plug_OTA_3.0.0.bin"
    }

As seen in the actual firmware binary file.

For full header specs, see https://github.com/Koenkk/zigbee-OTA/blob/da29b97e10d4ec674fb6a5a6c6df311497a1a2d9/lib/ota.js#L15

Re-pair device

After update, I highly recommend you to remove and re-pair the device.

This helps having inconsistent entities. A simple “Reconfigure” doesn’t seem to be good enough.

Tip : When playing around with many devices, I always go to Developer Tools > States and search for “_2”, to prevent suffixes being added to my entities.

Is it fixed?

First, be aware some plugs seem to have a hardware / firmware issue related to power monitoring. You can clearly see on below graphs that some plugs on the left graph are reporting around 10V higher than plugs on the right.

See this comment at Tuya TS011F (TZ3000_typdpbpg) powers off #13343.

Second, I want plugs not turning off themselves (even with flashed firmware!).

I haven’t dug into this yet, however since I mostly want to use these plugs in 2 cases:

  1. Control lights : So spikes during sunny day don’t matter;
  2. Monitor power consumptions of major appliances : I mainly care on having plugs always on and correctly reporting power.

My choice is pretty clear: Filter on “always on”-able plugs, use the reliable power monitoring ones.

I’m left with a subset of 7 plugs out of 13.

Update 2022-12-05

Since I’ve received a third batch of the ZigBee Smart plug. It is clear they are not made equal, making them somehow unreliable, I’d have to use another sensor to compare results.

Filtering invalid values using Node-RED and SQL

Here is my “maintenance” flow. It consists of a sqlite connection running a “delete” statement every 5 minutes.

[{"id":"6cc3b15e287542b9","type":"sqlite","z":"3e3378fe315fc5da","mydb":"023d9d2edf07540e","sqlquery":"fixed","sql":"delete\nFROM \"states\"\nwhere \n  (\n    (entity_id like 'sensor.eau_en%' AND state < 0)\n    or\n    ((entity_id like 'sensor.%_super_95_e10' or entity_id like 'sensor.%_diesel_b7') and state < 0.1)\n    or\n    (\n      entity_id like 'sensor.%rms%voltage%'\n      and ((CAST(state as INT) < 190 and CAST(state as FLOAT) > 0.1) or state = 0 or state > 290)\n      and state != 'unavailable'\n    )\n    or\n    (entity_id = 'sensor.average_voltage' and state <= 190)\n  )\n;","name":"Clean invalid states","x":470,"y":120,"wires":[["63ec92685cd2573e"]]},{"id":"a905c3cbb0a04dc7","type":"inject","z":"3e3378fe315fc5da","name":"Repeat every 5 minutes","props":[],"repeat":"300","crontab":"","once":false,"onceDelay":"10","topic":"","x":230,"y":120,"wires":[["6cc3b15e287542b9"]]},{"id":"63ec92685cd2573e","type":"debug","z":"3e3378fe315fc5da","name":"SQLite Maintenance","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":700,"y":120,"wires":[]},{"id":"023d9d2edf07540e","type":"sqlitedb","db":"/config/home-assistant_v2.db","mode":"RW"}]

Conclusion

Long story short : No, flashing your plugs might not fix your issue.

This is not an easy action, even if trivial, because it requires a lof of prerequisites that not everyone might be confortable with.

However once you unleash the power HACS, it is well worth the extra effort since that will be useful for a lot of other scenarios.

 

Post Author: Shut

8 thoughts on “Home Assistant – Zigbee device updates : ZHA & ZHA_TOOLKIT OTA

    Freek

    (15th December 2022 - 19:01)

    Thanks, this was very helpful. I always wondered how OTA updates worked for Zigbee devices.
    It’s interesting that your plugs show up as TS011F. According to the Blakadder ZHA wiki, TS011F does not support power measurement. My Lidl/SilverCrest Smart Plugs also identify as TS011F but they do not report any power usage.

    Theo

    (3rd February 2023 - 13:19)

    Hello Shut.

    Thanks a lot for the above post.

    I also despair of the tuya devices. I have the TS011F by _TZ3000_typdpbpg.

    I bought them first a half year ago. And with the “zha.issue_zigbee_cluster_command” Update with the version 1654157434-oem_zg_tl8258_plug_OTA_1.0.13.bin it worked fine for me and all the 16 plugs i have bought went from nothing to 0x0000004a and then to 0x0000004d. And they are working more or less fine nowadays.

    But a month ago, i bought further four TS011F by _TZ3000_typdpbpg plugs.

    And they messed up. Not with the OTA_1.0.13.bin nor the OTA_3.0.0.bin they do not went from 0x0000004a to 0x0000004d.

    And in the current state 0x0000004a they didnt work and plug odd randomly.

    I tried your method above (zha_toolkit.ota_notify), but that didnt work.

    So my question is: what have you done, to get them in state 0x0000004d?!?

    Thx a lot, Theo

      Shut

      (3rd February 2023 - 13:26)

      Hi Theo,

      Thanks for your kind words!
      The firmware version depends on the mapping provided at https://github.com/Koenkk/zigbee-OTA/blob/master/index.json
      Since your plug has the same manufacturer as mine, they should behave in the same way.

      Have you tried removing / pairing again the plugs?

      Update 1 : I’m experiencing a similar issue for one of my TS011F by _TZ3000_zloso4jk.
      My plugs (as in the post at time of writing) had firmware 0x0000004a, they all updated to 0x000000c0 but one. I’ll have to get a closer look.

      Update 2 : The plug updated fine, I had to disconnect it from wall and put it back. It kind of rebooted it and started accepting the OTA again.

      Regards,
      Rudy

    Theo

    (12th February 2023 - 00:10)

    Hello Rudy.

    Thanks for your reply. Yes, they are the same as yours with the same firmware (TZ3000_typdpbpg).

    Several times, I reboot them, plug them from the wall, reconfigure them but nothing happend. It does not change from 0x0000004a to 0x0000004d or when I remove them from nothing to 0x0000004a but not to 0x0000004d. And so, they plug off randomly and are not usable for things like refridgerators etc.

    Its kind of annoying because the plugs from last year (same product) are in state 0x0000004d.

    So, with the new ones I have e-waste @home.

    Regards, Theo

      Shut

      (12th February 2023 - 11:47)

      I’m afraid you’re right, the plugs do not seem to be made equal 🙁

    Theo

    (19th March 2023 - 23:32)

    Hey Rudy. Short update.

    I bought a second conbee 2 and set up a zigbee 2 mqtt environment. And voila. The three “broken” plugs are working very fine. The gui looks fresher than ZHA and it shows me directly updates for the plugs.

    Regards, Theo

      Shut

      (20th March 2023 - 08:42)

      Hi Theo,
      Thanks for the update! That actually sounds scary as the coordinator shouldn’t affect that.
      I might git it a try!
      Regards

Leave a Reply

Your email address will not be published. Required fields are marked *