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

Xiaomi Zigbee Power Plug Update Frequency #201

Closed
kiwijunglist opened this issue Jul 18, 2018 · 21 comments
Closed

Xiaomi Zigbee Power Plug Update Frequency #201

kiwijunglist opened this issue Jul 18, 2018 · 21 comments
Labels
feature request Feature request stale Stale issues

Comments

@kiwijunglist
Copy link

kiwijunglist commented Jul 18, 2018

The xiaomi power plug reports power (wattage) used. In the mi home app changes are updated almost instantly. In the zigbee2mqtt the update is 1 minute or so.

Can the update frequency be increased or manually set?

Discussion here: https://community.home-assistant.io/t/zigbee2mqtt-getting-rid-of-your-proprietary-zigbee-bridges-xiaomi-hue-tradfri/52108/363

The reason for more rapid changes is that the power usage can be used to:

  1. Monitor if a device is on / off
  2. Monitor what state a device is in, eg standby power / low / medium / high - all by using a template sensor with various wattage used.
@ciotlosm ciotlosm added the feature request Feature request label Jul 21, 2018
@Koenkk
Copy link
Owner

Koenkk commented Jul 29, 2018

Unfortunately I've lost my EU plug adapter, just ordered a new one and will check when it arrives.

@subzero79
Copy link

How would you work on this feature? Do you have to capture xiaomi gw and plug zigbee traffic? Using the same usb dongle?

I ask this because was of the features of the app was also to restore the power state of the plug after power outage. Don’t know if this was controlled by the gw or is a setting of the actual plug.

@Koenkk
Copy link
Owner

Koenkk commented Aug 1, 2018

@subzero79 first I'm going to check if any of the configure report functions works like: https://github.com/Koenkk/zigbee-shepherd-converters/blob/master/devices.js#L557

@subzero79
Copy link

Is it as simply as adding the lines to the definition in converters for the xiaomi plug? or needs additional manual configuration in the yaml file?

If that method doesn't work will you then have to start sniffing traffic?

@Koenkk
Copy link
Owner

Koenkk commented Aug 8, 2018

A configure method has to be added in devices.js. Similar to: https://github.com/Koenkk/zigbee-shepherd-converters/blob/master/devices.js#L559

@subzero79
Copy link

Sure, i've added the same lines and works as expected, energy report is instantaneous, but my impression is that only reports instantly if going up on power maybe more than 15W, less than that takes about 10 seconds which is still good. What i see also it doesn't report fast when power goes down it takes the normal time like a minute or so.

@subzero79
Copy link

I think i spoke too soon. I see no difference with the function there. I just moved to the dev branch to test this, and comparing this to the stable branch i am pretty sure i see faster report times, but report function makes no difference.
In my case the plug is attached to a fan. So basically turn on the fan light (20W) makes the report almost instant. Any speed setting and report comes back after 8-10 seconds. Must be due to current increasing slowly until reaches speed.
The slowest reports comes when consumption goes down. Takes something like 50-60 seconds.

@subzero79
Copy link

subzero79 commented Aug 23, 2018

Here is a wireshark capture of the zigbee traffic being captured while i interact with the plug via the phone app.

https://www.dropbox.com/s/v1st24npnos1mdv/xiaomi_gw_capture.tar.bz2?dl=0

The file readPowerfromState has the info in line 322. It requests read attribute to the endpoint

The file setRestorePowerState is when the restore power boolean is toggled in the app in line 354, it writes attributes these are the values i saw

Off restore state aa:80:05:d1:47:2d:01:10:00
On restore state aa:80:05:d1:47:2c:01:10:01

As for the file that turns the led on/off, is in the setLedStatusOnOff file, line 255 (led off), line 354 (led on).

The master key is the same as listed everywhere Zigbee Trust Center Key. You should see the transport key in the capture to decrypt traffic below the joining.

Hope it helps

@Koenkk
Copy link
Owner

Koenkk commented Aug 24, 2018

@subzero79 thanks!

From the logs, does the gateway constantly poll the plug or does the plug send its state on a change?

@subzero79
Copy link

It does not poll constantly. The request come from when I click the device within the app.
From within the device in the app if power changes, the report is not immediately unless I go back and then click the device again.

@subzero79
Copy link

So IMO zigbee2mqtt will have to implement to send read/write topic commands to the endpoints, frequency apparently cannot be set, unless the report functions you mentioned earlier work.

If someone wants to have the energy report every 15 seconds, will have to publish a payload to the read device topic and wait for the reply. I believe zigbee2mqtt already implements this, when a payload is set then zigbee2mqtt sends a requests to read the attributes to confirm.

* After publishing a command to a zigbee device we want to monitor the changed attribute(s) so that
* everything stays in sync.
*/
published.forEach((p) => {
let counter = 0;
let secondsToMonitor = 1;
// In case of a transition we need to monitor for the whole transition time.
if (p.message.zclData.hasOwnProperty('transtime')) {
// Note that: transtime 10 = 0.1 seconds, 100 = 1 seconds, etc.
secondsToMonitor = (p.message.zclData.transtime / 10) + 1;
}
const timer = setInterval(() => {
counter++;
// Doing a 'read' will result in the device sending a zigbee message with the current attribute value.
// which will be handled by this.handleZigbeeMessage.
p.converter.attr.forEach((attribute) => {
this.zigbee.read(deviceID, p.message.cid, attribute, ep, () => null);
});
if (counter >= secondsToMonitor) {
clearTimeout(timer);
}
}, 1000);
});
}

@Koenkk
Copy link
Owner

Koenkk commented Aug 25, 2018

Ok, so does this mean:

  • When in app the Xiaomi gateway polls -> changes are almost instantly
  • When not in app behaviour is the same as of zigbee2mqtt.

@subzero79
Copy link

From what I saw changes are not instant using the app. You have to force (sort of a refresh) go back to the devices list and click the plug again to get the power consumption, if it has changed. If you’re within the device window and there is power change it doesn’t show up in the app unless you do what I described before.

I didn’t monitor the zig bee traffic without the app. I can test again to see how the energy reports when consumption changes. But my guess is probably similar to what we have in zigbee2mqtt atm. Big increments are instant, smaller ones take ~10 seconds, and go back to zero or reductions take around 50-60 seconds. I’ll test this again with the gw

I’ll do some more testing.

@tb-killa
Copy link
Contributor

@subzero79 Do you capture like mentioned here: https://blog.jimmo.id.au/zigbee-packet-capture-724cf9346b47 ??
Do i have to decrypt your capture ?
Because i doesn´t see something like this: https://cdn-images-1.medium.com/max/2000/1*PYOaHRrHLnCKOczBRov-vg.png
Maybe we could see the unencrypted write command ?

@subzero79
Copy link

Do you capture like mentioned here: https://blog.jimmo.id.au/zigbee-packet-capture-724cf9346b47 ??

Yes, difference i did not build the contiki fw (good luck trying that), i use the sniffer_fw_cc2531.hex(which is shipped by TI), and whisniff (in linux). So basically use the trust center key, then pair a device to obtain a transport key.

As i mentioned before, you have to add the well known trust center key to the zigbee protocol, then the join process will appear. In the lines of the join process grab the transport key and add it again to the protocol, that should have the rest of the traffic de-crypted.

@subzero79
Copy link

I did not find a way with wireshark to save as decrypted traffic

@subzero79
Copy link

@tb-killa Did you manage to read the pcap file or still having trouble de-crypting it?

@stale
Copy link

stale bot commented Dec 15, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale Stale issues label Dec 15, 2018
@mattkerrison
Copy link

This would be really useful to update more than once a min

@stale stale bot removed the stale Stale issues label Dec 17, 2018
@subzero79
Copy link

#518

@stale
Copy link

stale bot commented Feb 15, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Feature request stale Stale issues
Projects
None yet
Development

No branches or pull requests

6 participants