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

Fix power measurement for Aqara MAEU01 plugs with newer firmware #1656

Merged
merged 22 commits into from
Oct 17, 2022

Conversation

TheJulianJES
Copy link
Collaborator

@TheJulianJES TheJulianJES commented Jul 17, 2022

News

The quirk is now included with Home Assistant Core 2022.10.5 (and later). Please upgrade to that version.
If you have issues with the Aqara MAEU01 / MMEU01 plugs, then delete the plug from ZHA and re-pair it.

If you installed the custom quirk previously, please uninstall it now.

AGAIN: THE QUIRK IS INCLUDED WITH Home Assistant Core 2022.10.5 (and later).
Just pair the plug normally.

If there any issues, create an issue here: https://github.com/zigpy/zha-device-handlers/issues/new/choose
(Conbee users might need to update the coordinators firmware)

An alternative is downgrading the plug if there are still issues: Koenkk/zigbee2mqtt#13903 (comment)

The quirk is included with Home Assistant Core 2022.10.5 and later


Notes

This quirk will allow for the power outage memory to be enabled with a config entity in HA with:

This quirk will probably only work if:

  • the MAEU01 plug has app_version = 41 (couldn't confirm with older firmware versions) AND (should work with all firmware versions now)
  • the coordinator sends out Xiaomi manufacturer codes when pairing (likely only an issue for maeu01 plugs with new firmware and as the sensors of those weren't working at all before, it's not really an issue. Older plugs seem to use OppleMode instead of needing Xiaomi manufacturer codes at pairing to determine whether or not to send the special Xiaomi messages)

This quirk (as of right now) will break power measuring in other MAEU01 plugs if:

  • the plug is running older firmware OR
  • the coordinator doesn't send out Xiaomi manufacturer codes when pairing (likely fixed for older plugs by turning on OppleMode)

If anyone needs the quirk right now, you can install this as a "custom quirk". See the instructions at the bottom.

Would fix #1655

As of right now, the newest firmware for the MAEU01 can be found here: Koenkk/zigbee-OTA#140 (41)
Older firmware is available here: Koenkk/zigbee-OTA#159 (32)
For downgrading your firmware, refer to the "Plugs randomly toggling on/off when using IKEA remotes" section of this post.

It's possible that this also fixes an issue with MAUS01 plugs (#1654) where sensors aren't created because there's no default value.

Changes

  • add new signatures (yes, even if the plug isn't reporting an endpoint with an AnalogInput cluster, it's there)
  • Improve custom Xiaomi ElectricalMeasurementCluster:
    • add lumi.plug.maeu01
    • put default values for sensors
    • switch to report voltage on rms_voltage instead of wrongly used instantaneous_voltage attribute (sensor isn't created for instantaneous voltage (and it's wrong, as we deal with AC voltage here and the sensor always reports the "max" of the sin wave)
    • set ac_power_divisor to 10 and multiple power_reported value to workaround a rounding error in ZHA (initial attribute report gets correctly rounded: 100.9 => 101 but during "polling" of the fake DataCluster, it only reads 100 which causes a weird jump in graphs. Another reason we want this: it also adds one decimal point. See: for the rounding issue, see: Possible rounding issue in update_attribute zigpy#1041)
  • use the custom Xiaomi ElectricalMeasurementCluster for MAEU01 plugs (previously used for MMEU01 and MAUS01 plugs)
  • renamed some things
  • now set OppleMode to 1 (enabled), as this sends the custom Xiaomi attribute reports on plugs with older firmware when enabled. Newer firmware versions don't have this attribute and always have this "enabled" (will require re-pairing to change "automatically" for older plugs)
  • add power outage memory
  • ...

TODO

  • make this work without breaking power measurement on older plugs paired without Xiaomi manufacturer codes/older firmware (or force everyone to update firmware?) (how to determine in which state we are?)
  • provide custom quirk download links
  • also get the alternative signatures of the older MMEU01 plug
  • search ZHA for where that rounding/cutoff issue happens (currently tracking in Possible rounding issue in update_attribute zigpy#1041))
  • figure out if AnalogInput cluster on endpoint 31 is used for anything (at least US plugs use it, maybe EU too. Do the US plugs, like the EU plugs, also send total consumption in the special Xiaomi message?)
  • It's possible that current_summ_delivered needs to be polled, as it doesn't seem to update (Reference: Update lumi.plug to report total energy #1211)
  • check if we still need to write OppleMode to 0. 1 might be better? (default for MMEU01 plugs with older firmware. For those, this quirk seems to work fine with both. OppleMode seems to be removed on newer firmware versions). Edit: We want OppleMode set to 1 if it exists, so special Xiaomi attribute reports are sent
  • figure out if Xiaomi Aqara Plug (SP-EUC01/lumi.plug.maeu01) Toggles without reason Koenkk/zigbee2mqtt#13903 can be solved. (IKEA remotes routing through these plugs trigger toggle) Edit: by downgrading firmware (refer to the "Plugs randomly toggling on/off when using IKEA remotes" section below)
  • ...?

Testing

New instructions:

  • update to Home Assistant Core 2022.10.5 (or later)
  • possibly remove the plug from ZHA and re-pair it

Old instructions:

  1. DO NOT DO THIS IF YOU'RE RUNNING Home Assistant Core 2022.10.5 OR LATER (the quirk is already installed by default then)
  2. Completely remove the device from HA
  3. Do the following:

In the configuration.yaml, add this:

zha:
  custom_quirks_path: /config/zha_custom_quirks

In the /config directory, create a folder called zha_custom_quirks, put the tjj_custom_aqara_plugs folder from this ZIP file in it: zha_custom_quirks.zip

In the end, the folder structure should look like this:
/config/zha_custom_quirks/tjj_custom_aqara_plugs
The last folder should contain four files:
__init__.py, plug.py, plug_eu.py, plug_maus01.py

  1. Restart Home Assistant
  2. Re-pair the device and see if the power measurement sensor works (it's likely others (like power_factor get created which can be disabled, as they'll never work))
  3. Also see if "total power consumption" works now (only EU models atm).
  4. Even if the power measurement sensor works now, please restart Home Assistant and let me know if any additional useless sensors are created.

Plugs randomly toggling on/off when using IKEA remotes

There's an issue in the v41 firmware on the plug where IKEA remotes toggle the plug (because the plugs always listens to messages sent for group 0). This is somewhat fixed in current versions of zhaquirks and this custom quirk by making the plug explicitly leave group 0. However, the plug still reacts when messages from those remotes are routed through it.
See the following comment for further explanation and a link for how to downgrade your plug to a "non-broken version": #1656 (comment)

@codecov-commenter
Copy link

codecov-commenter commented Jul 17, 2022

Codecov Report

Base: 80.06% // Head: 80.37% // Increases project coverage by +0.31% 🎉

Coverage data is based on head (bac7e4a) compared to base (fceacf1).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #1656      +/-   ##
==========================================
+ Coverage   80.06%   80.37%   +0.31%     
==========================================
  Files         240      240              
  Lines        7455     7482      +27     
==========================================
+ Hits         5969     6014      +45     
+ Misses       1486     1468      -18     
Impacted Files Coverage Δ
zhaquirks/xiaomi/__init__.py 83.79% <100.00%> (+3.38%) ⬆️
zhaquirks/xiaomi/aqara/plug_eu.py 90.56% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@coveralls
Copy link

coveralls commented Jul 17, 2022

Pull Request Test Coverage Report for Build 3267161994

  • 43 of 43 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.4%) to 80.444%

Totals Coverage Status
Change from base Build 3259514820: 0.4%
Covered Lines: 6018
Relevant Lines: 7481

💛 - Coveralls

def power_reported(self, value):
"""Power reported."""
self._update_attribute(self.POWER_ID, value)
self._update_attribute(self.POWER_ID, value * 10)
Copy link
Contributor

Choose a reason for hiding this comment

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

This change made my another aqara outlet (mmeu01 square one) report 10* more usage than normal

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That should be fixed by either re-pairing the device or reading the "ac_power_divisor" attribute on the ElectricalMeasurement cluster through ZHA -> Devices -> Plug -> Manage Clusters -> ElectricalMeasurement -> ac_power_divisor -> read attribute.

Copy link
Contributor

Choose a reason for hiding this comment

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

Reparing is workaround, I don't think this is good user experience to ask user to re-pair working device,
I think the solution is to remove this multiplication and change ac_power_divisor back to 1, I've changed it locally and both devices shows power consumption correctly for me

Copy link
Collaborator Author

@TheJulianJES TheJulianJES Aug 4, 2022

Choose a reason for hiding this comment

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

I agree. If possible, the "fake" attribute should be re-read automatically. There is another issue with it being left on 1 (different rounding when values like 1.9W are reported which causes a jump from 2W to 1W after some time with unchanged consumption).

If consumption varies a lot when low, it's also nice to see values in between 0W and 1W (and it not constantly jumping around).

Copy link

Choose a reason for hiding this comment

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

The power measurement on LLKZMK11LM (lumi.relay.c2acn01) is also wrong after this change. It's showing 100W for a 10W device.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@hakong No, check the AC power divisor: 0x0605. It's set to 10 instead of 1 now. Both of these changes were made so that there's one decimal.

@TheJulianJES
Copy link
Collaborator Author

TheJulianJES commented Sep 1, 2022

I've updated the custom quirk and total power consumption might work after some time now.
(EU models only atm: maeu01 should work. mmeu01 could potentially also work

Before testing anything, it's recommended to reset and re-pair the plug! Some values might be off otherwise.

Edit: Updated again. MAEU01 and MMEU01 total power consumption should work now (re-pair device!)

Note: When restarting or manually polling active_power or current_summ_delivered, the respective attribute might display a lower value than it previously was. This will also affect the HA sensor. It should resolve itself when the value updates again. (This seems to be because of another zigpy/zhaquirks/HA issue currently tracked here: zigpy/zigpy#1041)

@fmiermont
Copy link

I've updated the quirks and let you know if the total power consumption is now working !

One quick question, the plug has the hability to resume power on power loss. This is not the default behaviour (does not resume power by default). With Z2M, we had access to this settings directly. It's also described on the device page :
Power_outage_memory (binary)

Enable/disable the power outage memory, this recovers the on/off mode after power failure. Value can be found in the published state on the power_outage_memory property. To read (/get) the value publish a message to topic zigbee2mqtt/FRIENDLY_NAME/get with payload {"power_outage_memory": ""}. To write (/set) a value publish a message to topic zigbee2mqtt/FRIENDLY_NAME/set with payload {"power_outage_memory": NEW_VALUE}. If value equals true power_outage_memory is ON, if false OFF.
#
Voltage (numeric)

With ZHA, I cannot find it, nor using "Set Cluster" options. Do you have any idea how should I do that ?

Regards,

@TheJulianJES
Copy link
Collaborator Author

TheJulianJES commented Sep 5, 2022

I updated the quirks. (No need to re-pair when coming from the previous version).

Under Manage Clusters -> OppleCluster, you'll now find power_outage_memory.
When writing the attribute, use 0 for default off or 1 for restore.

@fmiermont
Copy link

Thanks !
But it does not seems to do anything. Getting the value returns "Bool.false". If I set this to 1 and then try to get value, it returns "none".
If I close/ reopen Manage Cluster and get the value, returns "Bool.false" again ?

Smartenergysummation has started to returns value !

@TheJulianJES
Copy link
Collaborator Author

Mhm, that's weird. It works on the plugs I tested it with. Reading the attribute before re-opening the window also shouldn't be able to get another value (None) at all. Can you turn on ZHA debug logs and see if there's anything in the logs when reading or writing the attribute?

Also, make sure you're not using the lower text field with "manufacturer code override". That should always stay empty.

So only use the upper one with writing 0 or 1. When reading the attribute, it will show in the same text field (but Bool.false/Bool.true).

@fmiermont
Copy link

Ok, now I feel dumb, i've never used custer attributes to write attributes so I was using the "Manufacturer code Override" because the "value" was filed with 'Bool.false'.

Works like a charm using the correct textfield !

@Jojonintendo
Copy link

Looking good here too on my 2 lumi.plug.maeu01. Only one useless sensor is created, but I think you already mentioned that (sensor.lumi_lumi_plug_maeu01_electricalmeasurementpowerfactor_2). The rest seems to work fine and update as well, many thanks!

@TheJulianJES
Copy link
Collaborator Author

@fmiermont It's not really a user-friendly way. If/when this quirk gets merged officially, an option could be added to HA itself, so the clusters menu doesn't even need to be used.

@oskarannas
Copy link

My two maeu01 plugs also works with "Electricalmeasurement", "Smartenergysummation" and the new "power_outage_memory" setting. Thanks! :)

@fmiermont
Copy link

@TheJulianJES yes I know this is not the user friendly way to set this parameter but this is also the basics of "advanced" zwave settings and I'm usually familiar with this (but i'm not using HA for a long time).
Anyway that works flawlessly for me now, perfect !

@xam-ps
Copy link

xam-ps commented Sep 12, 2022

Thanks a lot for your work! It's working for me as well 👍 (MAEU01, app_version = 41)

@TheJulianJES
Copy link
Collaborator Author

FYI for anyone that is using IKEA remotes and firmware v41 on the MAEU01/SP-EUC01 plug:
There's an issue in the v41 firmware version where the plug toggles on/off when IKEA remotes are used with their messages routed through the plug.
Before #1706 (changes are also included in this modified quirk), the plug on v41 always toggled if an IKEA remote was used anywhere on the network. With those changes and v41 plugs, it'll now only happen if the messages are routed through that plug. (Again, an issue in Aqara's v41 firmware).

The plug includes a rudimentary downgrade check that can be bypassed. I've successfully downgraded one of the plugs I'm testing with from v41 to v32. Be aware that you could brick your plug!
Instructions for downgrading are here: Koenkk/zigbee2mqtt#13903 (comment)
I'd only recommend trying the downgrade if you're running into this issue. (v32 doesn't seem to have the same issue as v41)

The quirk is now also updated to support maeu01 plugs with older v32 firmware. This is done by explicitly enabling "OppleMode" now. Newer firmware versions don't have this attribute anymore and it's basically always enabled. So, the plugs send special Xiaomi attribute reports. Since this modified quirk properly works with those now, we want to enable "OppleMode" if that attribute exists.
(Note: re-pairing and/or updating the custom quirk might be required in some cases when the sensors don't update)

@mvaessen
Copy link

I tested the latest quirk with 3x v41 plugs, all work great. Thanks for the quirk!

@oSourceSE
Copy link

oSourceSE commented Oct 1, 2022

Hi

Just wanted to give my input on this, i have a total of 5 Aqara Smartplugs and i have mixed luck with this and the original quirk shipped in 2022.9.7 of Home assistant, in the original one the connection to my plugs with .41 firmware is stable, but no power readings what so ever, in this this one i get power readings but the plugs constantly goes unavailable but i get power readings from them.

i have 3 with .41 firmare and 2 with .32 and the old ones work with either quirks.

I have removed one of the plugs after i added this and reinstalled it, same problem.

Just wanted you to know that it does not work for every .41 plug sadly.

If there is something specific you want me to post about my plugs i will gladly do so.

Regards / Marcus

@TheJulianJES
Copy link
Collaborator Author

@muddenhed The quirk can't affect the connection stability. It only changes how the messages are parsed when coming from the plug. With these plugs, I haven't encountered any stability issues so far. What coordinator/stick are you using?

(Also FYI, you can upgrade or downgrade all plugs to whatever firmware version you want (32 or 41))

@oSourceSE
Copy link

Hmm, intresting, wieard that it starts acting like that then.
I'm using ConBee II on a Raspebby PI, I will try to downgrade one of them if i can.

Sorry for wasting your time on this, thought it containted some connection polling code in this, but know better now.

Thanks.
/Marcus

@TheJulianJES
Copy link
Collaborator Author

TheJulianJES commented Oct 2, 2022

Sorry for wasting your time on this, thought it containted some connection polling code in this, but know better now.

You didn't! Thanks for all the feedback.

I'm using ConBee II on a Raspebby PI

Mhm, I've heard of quite a few issues with ConBee sticks now. You could try to upgrade the firmware on the stick and see if that helps.
Also make sure that the ConBee is on a USB extension and not near any USB 3 devices (like a USB-SSD plugged into the Pi, as that will cause massive interference in the 2,4 GHz range).

downgrade one of them if i can.

Process should basically be to download the modified firmware image, put the image in a zha_ota folder that's registered in the configuration.yaml, send a cluster command to notify the device of the OTA, wait until the device updates, delete the image from the zha_ota folder and restart HA (so the device doesn't "upgrade"(downgrade) again).
Let me know if you need help with this.

@dmulcahey dmulcahey merged commit 8149efd into zigpy:dev Oct 17, 2022
@TheJulianJES
Copy link
Collaborator Author

The quirk is now included starting with Home Assistant Core 2022.10.5 (and later).
It's recommended to uninstall the custom quirk before you upgrade to that version.
If you've already installed 2022.10.5, then just delete the custom quirk, reboot HA, and the "official quirk" should be used by ZHA.

If you didn't install the custom quirk and have issues with your Aqara MAEU01 or MMEU01 plugs now, please remove the device from ZHA and re-pair it. If you still have issues then, create a new issue in this repo and tag me.

@FSHelgeland
Copy link

@TheJulianJES

I just got three of these plugs, first tried to install one of them this evening. It did not work so I got searching for why, and found this thread. Now I have updated to:
Home Assistant 2022.10.5
Supervisor 2022.10.0
Operating System 9.3
Grensesnitt 20221010.0 – latest

But it still do not function, I have tried to install one of the other ones I got but the result is still the same. I got ZHA and the Raspbee II, where do I go wrong?

Best regards Freddy.

@Maggi0r
Copy link

Maggi0r commented Oct 28, 2022

I'm facing the same problem. I have 2 switches of this type. One is recognized, the other is not recognized or already recognized but I don't get any values displayed. The other connector works with no problems. My HA is 2202.10.5, everything is up to date. Greetings, Maggi

@FSHelgeland
Copy link

This is actually really frustrating. Have spend hours searching and I do have to conclude that my knowledge isnt good enough. Hope this is posible to solve then.

@FSHelgeland
Copy link

@TheJulianJES any idea how I can solve this? Today I updated the firmware on my Raspbee II, but still none of my plugs reporting any measuring, only function is on off.
Home Assistant 2022.11.1
Supervisor 2022.10.2
Operating System 9.3
Grensesnitt 20221102.1 – latest

@TheJulianJES
Copy link
Collaborator Author

If you didn't install the custom quirk and have issues with your Aqara MAEU01 or MMEU01 plugs now, please remove the device from ZHA and re-pair it. If you still have issues then, create a new issue in this repo and tag me.

@FSHelgeland So, you re-paired the plug after updating your Raspbee firmware and it still doesn't work?
If so, then please create another issue: https://github.com/zigpy/zha-device-handlers/issues/new/choose
Make sure to fill out the template and tag me (@TheJulianJES) there.

@sipy
Copy link

sipy commented Nov 3, 2022

Hello @TheJulianJES , I have Home Assistant 2022.11.1 as well and only the switch is working. Then I tried installing the quirk but it does not work anyway...

@TheJulianJES
Copy link
Collaborator Author

So, you re-paired the plug after updating your Raspbee firmware and it still doesn't work?
If so, then please create another issue: https://github.com/zigpy/zha-device-handlers/issues/new/choose
Make sure to fill out the template and tag me (@TheJulianJES) there.

@sipy Did you do the above?

Also, remove any custom quirks for the plug. They're all in Home Assistant 2022.10.5 or later (so also in 2022.11.1).
After having removed all custom quirks, remove the device from ZHA and then pair it again.

@sipy
Copy link

sipy commented Nov 3, 2022

Yep @TheJulianJES , tried them all and still not working. I am now trying to downgrade the Aqara firmware from V41 to V32. I'll let you know if it works.

@TheJulianJES
Copy link
Collaborator Author

Also, like I mentioned, please create a new issue here and fill-out the template: https://github.com/zigpy/zha-device-handlers/issues/new/choose

@FSHelgeland
Copy link

FSHelgeland commented Nov 3, 2022

@TheJulianJES Shame on me... Like my students I told you I had done something that I havent. But now I have updated my Raspbee II firmware to the latest one, and now I am able to read at least the active power, and also the correct device temperature. Cant read the voltage, but I dont need it, as long as I can read the active power I am satisfied. Thank you for solving this.

EDIT: And suddenly the voltage appeared, now it works like a charm :-)

@TheJulianJES
Copy link
Collaborator Author

@FSHelgeland Nice, glad it works for you now!

(Voltage always takes some time to appear after initial pairing due to how Xiaomi's messages work and I'm also not sure how accurate it is lol)

@sipy
Copy link

sipy commented Nov 4, 2022

@TheJulianJES , downgrading the Aqara firmware from V41 to V32 following this guide Koenkk/zigbee2mqtt#13903 (comment) solved the issue. Thank you.

@TheJulianJES
Copy link
Collaborator Author

@sipy Yeah, that's works around the issue you were having. It's the recommended firmware anyway, but, so far, it looks like your Raspbee firmware didn't correctly update if the older plug firmware works and the newer one doesn't. The newer firmware versions requires Xiaomi manufacturing codes to be sent using pairing. That's implemented in deCONZ firmwares since 1 or 2 years.

I'm glad that it finally works for you.

@sipy
Copy link

sipy commented Nov 4, 2022

Thanks @TheJulianJES , in order to be sure that I will face no issue in the future I should disable any updates of the Aqara plug firmware, right? How can I ensure that this will not happen?

Thanks

@TheJulianJES
Copy link
Collaborator Author

@sipy Just remove all Xiaomi plug related OTA files from your ZHA OTA directory and don't put any new ones in there and you're set.

@MrGlad8
Copy link

MrGlad8 commented Dec 11, 2022

I just bought a new Aqara Smartplug and it didnt show up any values! Tried the quirk here but it didnt work. Any workaround?

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.

[Device Support Request] Aqara MAEU01 Plug