Xiaomi Gateway Integration

Hey guys,

This is my first post so first of all thanks to all contributors for all the hard work!
I’m using danielhiverson repo and trying to do something which sounds very simple, but have been struggling to get it working… I suspect other people may be interested in having the same configuration I am seeking…

I’m trying to set-up a template sensor to show when motion was last detected rather than ‘on’ or ‘off’.
Here’s the code I have for the template sensor;

- platform: template
  sensors:
    nursery_last_motion:
      friendly_name: 'Nursery'
      value_template: '{{as_timestamp(states.binary_sensor.motion_sensor_158d00010cxxxx.last_updated) |  timestamp_custom("%-d/%-m %H:%M")}}'
      icon_template: mdi:walk

This is now showing time last detected in the format I want, however for some reason the ‘last_updated’ attribute keeps triggering until the ‘no motion since’ attribute of the sensor reaches 1800, causing the value I’m using to be wrong.

Is there another way anyone is aware of that I should be doing this?
I’d like to have all my motion sensors showing the time motion was last detected on my main screen without having to click on the sensor to view more detail.

Thanks in advance!

p.s. I also have the battery level of all my sensors showing in a different group using template sensors…

nursery_motion_battery:
  friendly_name: 'Nursery Motion Sensor'
  value_template: '{{ states.binary_sensor.motion_sensor_158d00010cxxxx.attributes.battery_level }}'
  icon_template: mdi:battery
  unit_of_measurement: "%"
entrance_motion_battery:
  friendly_name: 'Entrance Motion Sensor'
  value_template: '{{ states.binary_sensor.motion_sensor_158d000153xxxx.attributes.battery_level }}'
  icon_template: mdi:battery
  unit_of_measurement: "%"
front_door_motion_battery:
  friendly_name: 'Front Door Sensor'
  value_template: '{{ states.binary_sensor.door_window_sensor_158d000113xxxx.attributes.battery_level }}'
  icon_template: mdi:battery
  unit_of_measurement: "%"
temperature_sensor_battery:
  friendly_name: 'Kitchen Temperature Sensor'
  value_template: '{{ states.sensor.temperature_158d000110xxxx.attributes.battery_level }}'
  icon_template: mdi:battery
  unit_of_measurement: "%"
studio_speaker_switch_battery:
  friendly_name: 'Studio Speaker Switch'
  value_template: '{{ states.binary_sensor.switch_158d00013fxxxx.attributes.battery_level }}'
  icon_template: mdi:battery
  unit_of_measurement: "%"

2 Likes
  1. I’m still on Rave’s February release and it’s fine but as above Daniel’s is further down the development path.

  2. You can run either Xiaomi’s or Home Assistant’s automations exclusively, a mixture of both or even both for the same sensor.
    eg. You can have a switch turn on a light via Xiaomi’s app and also display a message on a media player via Home Assistant.
    Neither takes preference over the other but only Home Assistant’s will work offline. I currently have two switches and one I use for Xiaomi App automations and the other Home Assistant.

  3. As above, yes. Two ways I can with my router (Tenda), though I don’t bother. Both will however mean you can’t use Xiaomi App automations (unless they’ve changed the software to allow it to operate offline).
    First would be bandwidth control for the device itself. Set it to zero to stop it communicating outside the local network.
    Second would be via static routing (or similar) and just redirect traffic to a blackhole IP such as one on your local network outside your dhcp range. I’m unsure what IP they use for external communication so you may need to log or packet sniff to find out.

Hi

I have a little trouble making the friendly names working, I am new to this, hope someone can point point me in the right direction…

This is what I have added to homeassistant:, but when reloading I do not see any changes in the front end

homeassistant:
customize:
binary_sensor.humidity_158d00012xxxxe5:
friendly_name: Outside temp
binary_sensor.temperature_158d00012xxxxe5:
friendly_name: Outside hum

I hope your temperature is not binary. :wink: Just copy the entity name from the web ui (developer tools -> states). Something like this should work:

homeassistant:
  customize:
    sensor.humidity_158d00012xxxxe5:
      friendly_name: Outside temp
    sensor.temperature_158d00012xxxxe5:
      friendly_name: Outside hum

I’d like to get some feedback from anyone who has a “Xiaomi Aqara in-wall switch”. Specifically, does double press and long press work or will these only work as only ON\OFF switches. The README on github isn’t entirely clear because no examples are given.

Also if the in wall switches does support double press and long press events, if I buy a double inwall switch does it means I can track these events for left ON, left OFF, right ON & right OFF.

Also is the above behaviour any different between the Wired Aqara wall switch and the Wireless Aqara switch?

Can someone please advise?

Been away too long and i can’t even remember my password! Had been busy with traveling and work so i have not been active in the development. And i think i need to change my pace, i have been doing a lot of fix and debug for the community which have taken most of my time and that wasn’t something interesting to do all the time.

Going forward, I’ll rewritten some of the code and simplify it to make it work for me so i can spend more time implementing new devices.

By the way, aqara is crowdsourcing curtain motor and track. I bought one of those, gonna be more integrated than the dooya i had right now

In wall switch is only an on and off switch used to physically toggle a light or something connected to it. Long press and hold does nothing.

Thanks @rave, is this is the same for the Xiaomi Aqara Wireless Wall Switch? i.e. no double press or long press either? Or does the wireless one act as a bunch of sensors rather than a switch?

Welcome back!

1 Like

Yes! It’s the same. There is just a single press. The device pops up as two binary_sensors:

    binary_sensor.wall_switch_right_158dxxxxxxxxad
    binary_sensor.wall_switch_left_158dxxxxxxxxad

I tried something like this:

  - alias: Increase brightness of bedroom light
    trigger:
      platform: event
      event_type: click
      event_data:
        entity_id: binary_sensor.wall_switch_right_158xxxxxxxxxxd
        click_type: single
    action:
      service: light.turn_on
      entity_id: light.bedroom
      data_template:
        brightness: '{{states.light.bedroom.attributes.brightness + 20}}'

  - alias: Decrease brightness of bedroom light
    trigger:
      platform: event
      event_type: click
      event_data:
        entity_id: binary_sensor.wall_switch_left_158xxxxxxxxxxd
        click_type: single
    action:
      service: light.turn_on
      entity_id: light.bedroom
      data_template:
        brightness: '{{states.light.bedroom.attributes.brightness - 20}}'

It only works if the light is already on.

1 Like

Wow, the functionality of the Wireless Aqara Wall Switch is a real shame. I’d hoped getting a double wireless wall switch would be the same as getting 4 Xiaomi buttons. If I read your code correctly you can’t separate click types between

  1. left ON
  2. left OFF
  3. right ON
  4. right OFF

It is simply left CLICK or right CLICK (I’d still hope you could use double click and long press events also). It’s a shame it behaves this way in HASS in that it can’t seperate between an ON press and an OFF press.

One would think that Xiaomi Hardware has the ability to know between an off press and an on press so that your can correctly pair a wireless unit with a wired one and have it behave in a consistent manner. That’s assuming Xiaomi have not simply made the left and right switches as single toggle buttons.

Thanks, that’s what happens when you copy past code blindly :wink:

Guys, after update to new update i get “Invalid config”
also the developer mode did not open, any one get this issue?

wird, if i back to the old IP that fine, but this is not the IP on the device!!

looks like the 'both_click" event can be used in home assistant. i spotted this in the logs

17-05-10 20:08:58 DEBUG (Thread-5) [custom_components.xiaomi] PUSH >> <Entity Wall Switch (Left)_158d000183f688: off>: {'dual_channel': 'both_click'}
17-05-10 20:08:58 DEBUG (Thread-5) [custom_components.xiaomi] PUSH >> <Entity Wall Switch (Right)_158d000183f688: off>: {'dual_channel': 'both_click'}

How is the reliability with these switches? My seems to be hit or miss, I press it and nothing happens then the next 5 or 6 times it works find.

Wireless switch is different, it is essentially the same as xiaomi button

May I suggest you create a pull request with including this component into core. This will definitely spread out the work to more people who can fix bugs and implement devices. You’ve done outstanding work and I and I’m sure the rest of the community are very grateful for your work and I hope you’d agree it’ll be a waste if the work we’ve done would go to waste if you’re too busy accepting patches and implementing new devices.

I really hope you’d reconsider and prepare a pull request to submit this code in core. I believe @Danielhiversen would agree that we can later create a pull request with his changes on his fork. In my defense I’d say that the API has been stable and didn’t break the component even though it added new features.

15 Likes

+++++++++1

Hi there,
I’m trying to automate a Plug with a Motion Sensor but it doesn’t work :frowning: I tried platform state and platform event, Thanks in advance :slight_smile:
This is my automation:

automation:
- alias: Accendi neon garage se rilevi movimento
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d00010f7fda
    state: 'on'
  action:
    service: homeassistant.turn_on
    entity_id: switch.plug_158d00010ad640

- alias: Spegni neon garage se non c'è movimento dopo 2 minuti
  trigger:                                      
    platform: state                             
    entity_id: binary_sensor.motion_sensor_$
    state: 'off'
    for:
      minutes: 2
  action:
    service: homeassistant.turn_off
    entity_id: switch.plug_158d00010ad640

Another two strange things, I’m not able to see binary_sensor in services:

And under sensor there is only speedtest:

I’m doing the same. Your code looks good in my opinion.

  - alias: Motion by night at bathroom
    trigger:
      platform: state
      entity_id: binary_sensor.motion_sensor_158xxxxxxxxxxx
      from: 'off'
      to: 'on'
    condition:
      - condition: state
        entity_id: sun.sun
        state: 'below_horizon'
    action:
      service: light.turn_on
      entity_id: group.bathroom_light
      data:
        brightness: 10
        transition: 0

  - alias: Turn off light at bathroom 5 minutes after last movement
    trigger:
      platform: state
      entity_id: binary_sensor.motion_sensor_158xxxxxxxxxxx
      to: 'off'
      for:
        minutes: 3
    action:
      service: light.turn_off
      entity_id: group.bathroom_light
1 Like

Try service switch.turn_on with the same entity id

Under the services tab in developer tools it will tell you how to refer to it.

And you can’t see the sensors there as they only provide information.