Xiaomi zigbee smart plug

Hello.

Try the following instead:

sensors:
  - platform: template
    sensors:
      mydevice_power:
        value_template: '{{ states.switch.plug_158d0001xxxxxx.attributes.load_power|int }}'
  - platform: template
    sensors:
      mydevice_inuse:
        value_template: '{{ states.switch.plug_158d0001xxxxxx.attributes.in_use|default(0) }}'

That should do the trick.

3 Likes

Thanks. That does the job perfectly!

Hi @syssi, I’ve been using the Xiaomi gateway for a month or two and generally it works fine.

I’m running a few movement sensors etc off it and it was pretty much plug and play.

My only issue is with the Plug aka Socket (Zigbee version). When I try to turn it on/off from Homeassistant, it doesn’t work.

Basically, I just turn the switch on/ff via the interface, and the switch moves and after a few seconds reverts back to the original position.
If I use the MiHome app to turn on/off the switch, I see the switch mode within HA changes accordingly. I also get to see the power utilisation etc as it should. See below.

Here’s the log;

2017-10-28 06:05:41 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/lib/python3.5/asyncio/tasks.py", line 241, in _step
    result = coro.throw(exc)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/core.py", line 1026, in _event_to_service_call
    yield from service_handler.func(service_call)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/switch/__init__.py", line 117, in async_handle_switch_service
    yield from switch.async_turn_off()
  File "/usr/lib/python3.5/asyncio/futures.py", line 380, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/lib/python3.5/asyncio/tasks.py", line 304, in _wakeup
    future.result()
  File "/usr/lib/python3.5/asyncio/futures.py", line 293, in result
    raise self._exception
  File "/usr/lib/python3.5/concurrent/futures/thread.py", line 55, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/switch/xiaomi_aqara.py", line 106, in turn_off
    if self._write_to_hub(self._sid, **{self._data_key: 'off'}):
  File "/srv/homeassistant/lib/python3.5/site-packages/PyXiaomiGateway/__init__.py", line 290, in write_to_hub
    data['key'] = self._get_key()
  File "/srv/homeassistant/lib/python3.5/site-packages/PyXiaomiGateway/__init__.py", line 320, in _get_key
    encryptor = AES.new(self.key.encode(), AES.MODE_CBC, IV=init_vector)
  File "/srv/homeassistant/lib/python3.5/site-packages/Crypto/Cipher/AES.py", line 95, in new
    return AESCipher(key, *args, **kwargs)
  File "/srv/homeassistant/lib/python3.5/site-packages/Crypto/Cipher/AES.py", line 59, in __init__
    blockalgo.BlockAlgo.__init__(self, _AES, key, *args, **kwargs)
  File "/srv/homeassistant/lib/python3.5/site-packages/Crypto/Cipher/blockalgo.py", line 141, in __init__
    self._cipher = factory.new(key, *args, **kwargs)
ValueError: Key cannot be the null string 

Any thoughts?

Thx
JP

Please post you configuration, too. Do you use a proper key? How did you obtain the key (iOS or android app)? Your backtrace shows the component was unable to encrypt the “turn_off” command because of missing requirements.

Hi @syssi

Here it is. I’ve commented out the key just today. Until then it was running with the key. Either way the other sensors work, and the Plug behaves the same.

xiaomi_aqara:
  gateways:
  #  - mac:
  #    key: 6050E5E0E9594A31

I use the iOS instructions in the docs to obtain the key.

Thx
James

A key is needed to control the device. I think you should regenerate the key with the android app. Take a look here: https://community.home-assistant.io/t/pyxiaomigateway-problem-error-invalid-key/

I’m a rare breed with no access to any android devices - only iOS, MACOS or Windows. Any other thoughts?
Thx
JP

hi, did you manage to solve ir?i have the same isue, im using hass.io, and the discovery found the gateway and plug, but the plug doesnt work just like your problem, besides i cant control the light of the gateway

I had the same issue as you both, managed to fix it by generating a new key in the MiHome app, putting the new key in the config yaml, then it worked

1 Like

slightly off topic, but after the recent update to the firmware, load_power is always displaying ‘0’. Any thoughts on how to fix this?
I also moved from aio ha installer to hassio by the way. Not sure if this is relevant to the issue.

This works when i enable internet access to the gateway. However, doesnt seem to work on the local network only.

Hey @Mukund,

I’ve been using my xiaomi smart plugs to turn on my kettle, but I am also encountering that the load_power doesn’t update for around 4 minutes. I need this load_power information so I can automatically turn the switch off at the wall when the kettle is boiled, and to send me my notification.

Did you ever get it sorted?

1 Like

Yep. It works when I allow the switch to communicate to the internet. I generally disable internet access to all of my Xiaomi products.
I use it to turn on lights when motion is detected by the Xiaomi pir sensor.

What’s the exact name and type of your device? Which HA component do you use?

And does it update in real time? Mine take 4-5 minutes to update the load_power.
I have never disabled internet access for my Xiaomi gear. I am also using Hassio, if that makes a difference.

1 Like

Is there any way to report plug power usage per day or cumulative month usage in a chart ?

You’re not alone here, mine are also very delayed reporting power usage which makes it horrible for automations. Around the 4-5 minute mark most of the time, but once it starts to see some changes it’s faster. I can’t tell if this is a HA or Xiaomi bottleneck…

In my case always show 0 as load power. :sweat: 5 minutes could be great for me…

I noticed the same thing. Was trying to base some automations based on how many watts is going through the plug.

I’m using home assistant, xiaomi gateway, xiaomi zigbee power plug.

The load power (watts) updates instantly inside the mi home application, but takes a very long to update the state attributes within home assistant.

Someone mentioned that if you give the gateway internet access it fixes the slow update problem, i haven’t tried this yet. The other method that has been mentioned is to change the update frequency of pooling for the switch, but I’m not sure how to do this, or if it is even supported.

Another method might be to use a sonoff plug with tasmota and mqtt, some (but not all) of the sonoff devices can monitor watts/current usage.

Apparently with the zigbee2mqtt component / hardware the update of the zigbee power plug is a bit faster (? 1 minute).

See discussion here:

See github issue asking if the update frequency can be increased for zigbee2mqtt:

1 Like