Xiaomi Gateway Integration

I’ve just installed my new wall switches which also include the neutral line (called zero line in gearbest site).

I am quite surprised to see that they also show the power consumption, the daily total consumption and monthly consumption on mi app. And the current consumption seems to be true too.

It is found as switch.wall_switch_ln_158xxxxxxxx. Is there a way to get this power values readable too?

Thank you.

Please enable the debug log and provide some events of your device:

logger:
  default: warn
  logs:
    homeassistant.components.xiaomi: debug
    homeassistant.components.binary_sensor.xiaomi: debug
    homeassistant.components.sensor.xiaomi: debug
    homeassistant.components.light.xiaomi: debug
    homeassistant.components.switch.xiaomi: debug

If the events uses the same attributes like the device called “plug” you can just enable the power consumption feature for testing. Go to site-packages/homeassistant/components/switch/xiaomi.py anf flip the third parameter at device.append() from False to True:

diff --git a/homeassistant/components/switch/xiaomi.py b/homeassistant/components/switch/xiaomi.py
index 767043a8b..b21da8439 100644
--- a/homeassistant/components/switch/xiaomi.py
+++ b/homeassistant/components/switch/xiaomi.py
@@ -34,7 +34,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
             elif model == 'ctrl_ln1':
                 devices.append(XiaomiGenericSwitch(device, 'Wall Switch LN',
                                                    'channel_0',
-                                                   False, gateway))
+                                                   True, gateway))
             elif model == 'ctrl_neutral2':
                 devices.append(XiaomiGenericSwitch(device, 'Wall Switch Left',
                                                    'channel_0',
@@ -46,11 +46,11 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
                 devices.append(XiaomiGenericSwitch(device,
                                                    'Wall Switch LN Left',
                                                    'channel_0',
-                                                   False, gateway))
+                                                   True, gateway))
                 devices.append(XiaomiGenericSwitch(device,
                                                    'Wall Switch LN Right',
                                                    'channel_1',
-                                                   False, gateway))
+                                                   True, gateway))
             elif model == '86plug':
                 devices.append(XiaomiGenericSwitch(device, 'Wall Plug',
                                                    'status', True, gateway))
1 Like

:cold_sweat:

All variante are accepted now: CAFEC0FFEE00, CA:FE:C0:FF:EE:00, cafe0ffee00, ca:fe:c0:ff:ee:00

        res_gw_conf = {'sid': gw_conf.get(CONF_MAC)}
        if res_gw_conf['sid'] is not None:
            res_gw_conf['sid'] = res_gw_conf['sid'].replace(":", "").lower()
            if len(res_gw_conf['sid']) != 12:
                raise vol.Invalid('Invalid mac address', gw_conf.get(CONF_MAC))
        key = gw_conf.get('key')

The code retrieves the config parameter “CONF_MAC” (mac), removes the “:” and transforms to lower case. If the result doesn’t have a length of 12 chars it will be reported as invalid.

Thanks. Found out that some of the motions sensors are just stuck in the on position.
Is there any polling option like it was in rave’s code?

The sensor gets polled if there is continuous motion. The interval is 30 seconds. Take look here: Xiaomi Human / Body / Motion Sensor - Timeout - #21 by sjee

Than i’m stuck. Just relaunched the HA. And then in the logs i see sensor reporting motion.
Definetly there is no motion involved and the logs in Mi Home prove that.
What steps should i take to debug further?

2017-08-17 08:49:06 DEBUG (Thread-8) [homeassistant.components.xiaomi] PUSH >> : {‘status’: ‘motion’, ‘voltage’: 3015}
2017-08-17 08:49:37 DEBUG (Thread-8) [homeassistant.components.xiaomi] PUSH >> : {‘status’: ‘motion’, ‘voltage’: 3015}
2017-08-17 08:50:07 DEBUG (Thread-2) [homeassistant.components.xiaomi] PUSH >> : {‘status’: ‘motion’, ‘voltage’: 3015}
2017-08-17 08:50:38 DEBUG (Thread-2) [homeassistant.components.xiaomi] PUSH >> : {‘status’: ‘motion’, ‘voltage’: 3015}
2017-08-17 08:51:09 DEBUG (Thread-3) [homeassistant.components.xiaomi] PUSH >> : {‘status’: ‘motion’, ‘voltage’: 3015}

In the past it was enough to do “real motion” in front of the motion sensor. A few minutes later it will fall back to no_motion and the problem is gone. If this does not help. Remove the battery for a moment.

The motion is correctly reported in the log of MiHome app. So it is hard to blame the hardware issue.
Currently fixed it by removing and re adding to the MiHome.

2017-08-17 09:01:10 DEBUG (Thread-4) [homeassistant.components.xiaomi] PUSH >> : {‘no_motion’: ‘120’}

So is there anyway to dive deeper and gather more information by debugging?

It’s a firmware issue. The communication between your gateway and the chinese servers (mi home app) uses another protocol. Home assistant uses the local procotol which has some flaws. Just monitor (debug log) the events. You will see home assistant just shows what’s reported from the gateway. If the gateway reports a lie we are lost.

If you want to go deeper: Build a zigbee sniffer

and decode the traffic between gateway and motion sensor. Is the issue visible here, too?

So indeed they have changed something in 149. So recommendation is not to update. I believe nobody knows yet how to downgrade on Xiaomi…
Wonder if the all in one zigbee USB stick developed by a french guy and which he states can control xiaomi will make it to the market

The “stuck motion sensor” is not an issue of 149. It pops up from time to time unfortunately and is a pretty old bug. I had the same problem right after the update to 148.

I have enabled logging, edited the xiaomi file and this is from the states page for the switch:

Power consumed: null
Load power: null
friendly_name: Night Light
In use: null
battery_level: 100
icon: mdi:lightbulb-on

Nothing is usable here. Meanwhile as of before the current power value is valid in the mi home app.

And this is the event it creates when switched on:
2017-08-17 16:44:16 DEBUG (Thread-6) [homeassistant.components.xiaomi] PUSH >> <Entity Wall Switch LN_158dxxxxxxxxx: on>: {'channel_0': 'on'}

A pity. The events does not contain the informations at the local network protocol so far.

I now tried this:

  - platform: template
      entity_id: switch.plug_158dXXXXXXXXX
      value_template: '{{ states.switch.plug_158dXXXXXXXXX.attributes["Load power"] < 40 }}'

…and without the “-” before platform:, both gives me errors on automation at startup.

2017-08-17 15:06:56 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: [entity_id] is an invalid option for [automation].

yeah you are right. you are using a platform template for trigger and it doesn’t have the entity_id part.

I finally integrated two of the plugs last night, and noticed the same thing on the Mi app.
I would love to be able to capture daily consumption through HA.

Basically I have a package and for each battery-powered sensor, this is what I add for the package:

sensor:
### Battery level for Bedroom Temperature Sensor ###
  - platform: template
    sensors:
      battery_bedroom_temp_sensor:
        friendly_name: 'Sensor Quarto'
        value_template: "{{ states.sensor.temperature_158dxxxxxxxxxx.attributes.battery_level|default(-1)|int if states.sensor.temperature_158dxxxxxxxxxx is not none }}"
        unit_of_measurement: '%'
        icon_template: '{% if states.sensor.temperature_158dxxxxxxxxxx.attributes.battery_level == 100 %}
                           mdi:battery
                        {% elif states.sensor.temperature_158dxxxxxxxxxx.attributes.battery_level >= 90 %}
                           mdi:battery-90
                        {% elif states.sensor.temperature_158dxxxxxxxxxx.attributes.battery_level >= 80 %}
                           mdi:battery-80
                        {% elif states.sensor.temperature_158dxxxxxxxxxx.attributes.battery_level >= 70 %}
                           mdi:battery-70
                        {% elif states.sensor.temperature_158dxxxxxxxxxx.attributes.battery_level >= 60 %}
                           mdi:battery-60
                        {% elif states.sensor.temperature_158dxxxxxxxxxx.attributes.battery_level >= 50 %}
                           mdi:battery-50
                        {% elif states.sensor.temperature_158dxxxxxxxxxx.attributes.battery_level >= 40 %}
                           mdi:battery-40
                        {% elif states.sensor.temperature_158dxxxxxxxxxx.attributes.battery_level >= 30 %}
                           mdi:battery-30
                        {% elif states.sensor.temperature_158dxxxxxxxxxx.attributes.battery_level >= 20 %}
                           mdi:battery-20
                        {% elif states.sensor.temperature_158dxxxxxxxxxx.attributes.battery_level >= 10 %}
                           mdi:battery-10
                        {% else %}
                           battery-outline
                        {% endif %}'
        entity_id: sensor.temperature_158dxxxxxxxxxx

### Battery level lower than 10% ###
  - platform: template
    sensors:
      bedroom_sensor_battery_low:
        value_template: '{{ states.sensor.temperature_158dxxxxxxxxxx.attributes.battery_level < 10 }}'
        friendly_name: 'Bedroom sensor battery is low'

alert: 
### Alert via iOS notification when Bedroom sensor battery < 10% ###
  bedroom_sensor_battery_low:
    name: Bedroom sensor battery is low
    entity_id: sensor.bedroom_sensor_battery_low
    state: 'True'
# Repeat in minutes (1 day = 1440 minutes)
    repeat: 1440
    skip_first: False
    notifiers:
      - ios

What is shown:
image

Please bare in mind that alert repetition (in my case, once per day), period is back to zero on every HASS restart.

8 Likes

Does the double key wireless switch support double click on each button?

It supports double click on any key: Xiaomi - Home Assistant

Events of the round wireless button: single, double, hold, long_click_press, long_click_release
Events of the squared wireless button: single, double
Events of the wireless wall switch (single-key): single
Events of the wireless wall switch (double-key): single, single, both