Xiaomi mi wifi plug & air purifier

Thanks.

And you’re right: the plug does not provide power consumption as it’s the “basic” version (non-Zigbee). But the power strip should provide it, as you can see it in “Mi Home” app. I’ve read miio device documentation and teoretically it should provide 2 additional capabilities: power-load and power-usage, but I could’nt retrieve them via the miio command line.

I’ll keep trying to get them later.

EDIT:

I found a new attribute that’s present in power strip (but not in power plug): current. It’s a decimal number and it’s proportional to instant consumption showed in Mi Home, but I don’t know yet the proportions.

1 Like

Can you post the miio call and the output?

miio --control MY_STRIP_IP --method get_prop --params '["power","temperature", "current"]'

What does respond the plug to this request?

Here you are:

Call:

miio --control 192.168.1.218 --method get_prop --params ‘[“power”,“temperature”, “current”]’

Output:

INFO Attempting to control 192.168.1.218

INFO Got result:
[
“on”,
48.11,
0.06
]

I’ve been thinking that the current is expressed as Amps (0.06A), but Xiaomi erroneously calculates the Watts, as in Mi Home app, this is equivalent to about 6.6W. It seems like it thinks that the input voltage is 110V:

W = V x A ==> 110V x 0.06A = 6.6W

But in Europe we use 220V, so it should be:

220V x 0.06A = 13.2W

What’s the output if you request the plug? I want to make sure I can send the same request to both devices.

1 Like

It simply returns null as the value for current:

INFO Attempting to control 192.168.1.57

INFO Got result:
[
“on”,
51,
null
]

I have updated the component. The state attributes “temperature” and “current” should be available now.

3 Likes

@syssi Your an absolute legend. Thanks so much for getting this up and running! Everything is running perfectly! :smile:

Being able to see the temperature is a cheeky bonus!

1 Like

Can you give me a hint where to buy the smart strip? I just found a Xiaomi Yeelight Smart Light Strip.

Perfect!

It works nicely for me.
Now I’ll define a couple of template sensors based on these attributes.

Thanks for your work!

I bought my smart strip at Aliexpress for about $17 with EU adapter.

I’m not sure if I can post links…

You can also set modes by calling:

miio --control 192.168.0.15 --method set_mode --params '["favorite"]'
miio --control 192.168.0.15 --method set_mode --params '["auto"]'

Took me 3 hours to find that out, because I’m a complete and utter noob.

I think that’s a pretty big thing, because this way you can override Xiaomi’s target Aqi of 30 to the Aqi of your liking using automations and much more in Home Assistant.

A very simple switch would look like this:

- platform: command_line
  switches:
    air_purifier:
      command_on: miio --control 192.168.0.15 --method set_mode --params '["favorite"]'
      command_off: miio --control 192.168.0.15 --method set_mode --params '["auto"]'
2 Likes

I have another question about your devices. Can you tell me the reported types (“devtype”) of your devices? At discovery there should be some output which looks like this:

 _LOGGER.debug("Discovered %s %s with ts: %s" % (self._devtype, self._serial, self._device_ts))

Thanks in advance!

How do I enable debug level logging? I think my max level is INFO.

EDIT: I’ve got it:

2017-07-17 01:50:45 DEBUG (Thread-7) [xiaomiplug.device] Discovered 842 20054 with ts: 1970-01-08 09:32:45
2017-07-17 01:50:46 DEBUG (Thread-13) [xiaomiplug.device] Discovered 743 9302 with ts: 1970-01-02 06:35:23

I already knew it and used with input slider as for 3 modes.
If you need it, refer to the below

-configuration.yaml-
input_select:
air_purifier_mode:
name: Air Purifier Mode
options:
- N/A
- Auto
- Sleep
- Favorite
initial: N/A
icon: mdi:target

shell_command:
miair_auto: miio --control 192.168.0.15 --method set_mode --params ‘[“auto”]’
miair_sleep: miio --control 192.168.0.15 --method set_mode --params ‘[“silent”]’
miair_favorite: miio --control 192.168.0.15 --method set_mode --params ‘[“favorite”]’

-automations.yaml-

  • alias: Mi Air Mode
    trigger:
    platform: state
    entity_id: input_select.air_purifier_mode
    action:
    service_template: shell_command.miair_{{ states.input_select.air_purifier_mode.state.lower() }}

-switch.yaml-

  • platform: command_line
    switches:
    air_purifier:
    command_on: miio --control 192.168.0.15 --method set_power --params ‘[“on”]’
    command_off: miio --control 192.168.0.15 --method set_power --params ‘[“off”]’
    friendly_name: ‘Air Purifier’

-group.yaml-
mi_air_pro:
name: “Xiaomi Mi Air”
control: hidden
entities:

  • switch.air_purifier
  • input_select.air_purifier_mode
1 Like

Hi again.

I do not know why when my Home Assistant is working for some time, I cannot switch the xiaomi sockets any more:

2017-07-17 17:16:59 ERROR (Thread-18) [xiaomiplug.device] got error when receiving: timed out
2017-07-17 17:16:59 ERROR (Thread-18) [custom_components.switch.xiaomiplug] Got exception while fetching the state: 
2017-07-17 17:17:09 ERROR (Thread-12) [xiaomiplug.device] got error when receiving: timed out
2017-07-17 17:17:09 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/var/lib/home-assistant/xiaomiplug/device.py", line 129, in send
    data, addr = s.recvfrom(1024)
socket.timeout: timed out

The above exception was the direct cause of the following exception:

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 1023, 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 109, in async_handle_switch_service
    yield from switch.async_turn_off()
  File "/usr/lib/python3.5/asyncio/futures.py", line 361, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/lib/python3.5/asyncio/tasks.py", line 296, in _wakeup
    future.result()
  File "/usr/lib/python3.5/asyncio/futures.py", line 274, 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 "/var/lib/home-assistant/custom_components/switch/xiaomiplug.py", line 86, in turn_off
    self.switch.stop()
  File "/var/lib/home-assistant/xiaomiplug/plug.py", line 20, in stop
    return self.send("set_power", ["off"])
  File "/var/lib/home-assistant/xiaomiplug/device.py", line 143, in send
    raise DeviceException from ex
xiaomiplug.device.DeviceException

From Mi Home they work perfectly…

I added a reconnect on connection failure. Please update the custom_component. Next step will be a rewrite of the component and use this 3rd party library: https://github.com/SchumyHao/python-miio

1 Like

Updated. Now I’ll test through the day.

Got two wifi switches here, and both now works without any issues.

1 Like

Mine too. I didn’t have to restart HA at all.

Good job!

@syssi & @xavV, do you have plans try to get this included into the upstream at some point? It is open for PRs and I have even refactored it to be more easily extendable and not so vacuum-centric, but maybe it should be renamed to be less bound to the vacuum part?