Presence sensor using Xiaomi MiHome app and Gateway

From options I tried for presence sensor like NMAP/Ping/Bluetooth/IOS component/Owntracks I find that Xiaomi MiHome’s app presence detection works better and more accurate.

The idea using it with HA is making automation that flashes Gateway light, HA see the changes in “rgb_color” values, and automation should define that it is a signal from MiHome app, but I’m stuck at the automation part

This is what I made:

in MiHome app made automation that flashes light 3 times:

turn gateway light off
delay 3s
turn gateway light on
delay 3s
turn gateway light off
delay 3s
turn gateway light on
delay 3s
turn gateway light off
delay 3s
turn gateway light on
delay 3s
turn gateway light off

in HA made variables, automation and scripts that store light changes:

input_number:
  code_r:
    min: 0
    max: 255
  code_g:
    min: 0
    max: 255
  code_b:
    min: 0
    max: 255

automation:
  - alias: light change
    trigger:
      platform: state
      entity_id: light.gateway_light_7811dcb22ca0
    action:
    - service: script.resetcode
    - service: script.savecode

script:
  savecode:
    sequence:
      - service: input_number.set_value
        data_template:
          entity_id: input_number.code_r
          value: '{{ states.light.gateway_light_7811dcb22ca0.attributes.rgb_color[0] | int }}'
      - service: input_number.set_value
        data_template:
          entity_id: input_number.code_g
          value: '{{ states.light.gateway_light_7811dcb22ca0.attributes.rgb_color[1] | int }}'
      - service: input_number.set_value
        data_template:
          entity_id: input_number.code_b
          value: '{{ states.light.gateway_light_7811dcb22ca0.attributes.rgb_color[2] | int }}'
  resetcode:
    sequence:
      - service: input_number.set_value
        data_template:
          entity_id: input_number.code_r
          value: 0
      - service: input_number.set_value
        data_template:
          entity_id: input_number.code_g
          value: 0
      - service: input_number.set_value
        data_template:
          entity_id: input_number.code_b
          value: 0

the result is HA see that light flashed 3 times:
1

Now I can’t figure how to make an automation that translate this signal to binary sensor, pleas advice

Wouldn’t it be easier to just directly monitor the on/off state of the light? Maybe something like this:

counter:
  toggle_count:
    initial: 0

input_boolean:
  flash_completed:
    initial: off

automation:
  - alias: Count light changes
    trigger:
      - platform: state
        entity_id: light.gateway_light_7811dcb22ca0
        to: 'on'
        for:
          seconds: 2
      - platform: state
        entity_id: light.gateway_light_7811dcb22ca0
        to: 'off'
        for:
          seconds: 2
    condition:
      condition: template
      value_template: >
        {% set to_on = trigger.to_state.state == 'on' %}
        {% set cnt = states('counter.toggle_count')|int %}
        {{ to_on and cnt in [0,2,4] or not to_on and cnt in [1,3,5] }}
    action:
      service: counter.increment
      entity_id: counter.toggle_count

  - alias: Light sequence finished
    trigger:
      platform: numeric_state
      entity_id: counter.toggle_count
      above: 5
    action:
      service: input_boolean.turn_on
      entity_id: input_boolean.flash_completed

  - alias: Reset counter
    trigger:
      - platform: state
        entity_id: light.gateway_light_7811dcb22ca0
        to: 'on'
        for:
          seconds: 4
      - platform: state
        entity_id: light.gateway_light_7811dcb22ca0
        to: 'off'
        for:
          seconds: 4
      - platform: state
        entity_id: input_boolean.flash_completed
        to: 'on'
    action:
      service: counter.reset
      entity_id: counter.toggle_count

  - alias: Now do something...
    trigger:
      platform: state
      entity_id: input_boolean.flash_completed
      to: 'on'
    action:
      - service: input_boolean.turn_off
        entity_id: input_boolean.flash_completed
      # Your stuff goes here...
1 Like

If done the same in the past by just switching on a light to a brightness of 1 when leaving and 2 when coming home. If HA detects a brightness of 1 we’re not at home and HA switches of the light. No need to flash or anything like that. Works pretty well.

1 Like

Thanks tried your code just needed to change “trigger.to_state.state” to “states.light.gateway.state” and the code is working

But I find this method is not reliable, sometimes HA not registering states and “toggle_count” gets only to 2 or 4 and not triggering “flash_completed”, even when I added another “on/off” cycle to lamp still some times it misses, maybe extending time period of states will help but its already taking about 20 seconds I want to look for better trigger

Xiaomi Gateway brightness can’t be set with MiHome automations but I tried your method with my Yeelight lamp, it is working but to make this trigger reliable it should stay at least 30 seconds in same brightness cause otherwise it’s triggered by just playing with the lights

So my conclusion about light as a trigger is that it is slow and not 100% reliable, need to think about better trigger, something that can be automated in MiHome app, be instant rather than sequence and have some definitive value by which we can say it’s 100% intended trigger and not something that can be set by mistake

If you had a newer zwave switch with double-tap feature, that would be perfect.

Sorry I don’t understand zwave?

Now I’m working on different approach with Xiaomi IR Remote, attached IR LED to RPi, installed LIRC now figuring how to make it listen to Xiaomi IR Remote and control boolean in HA

I used two levels of (very low) brigthness to detect home/away, after that the automation sets the brightness back to 100% (for example) and switches off the light. This way there is no confusion with normal switching on/off the lights and there is no need to keep the light on for a specific time. It workes very(!) reliable but you have to make sure you trigger on specific brightness not just on/off.

When Yeelight state/brightness/color changed not from HA, it takes time to update maybe 10-15 second or more, so for the safe side automation should keep low brightness for 15-20 seconds, it is too long and felt inconsistent, sometimes it takes 5 seconds sometimes 15 seconds

Eventually I made it with Xiaomi IR Remote, it’s instant and precise

Installed LIRC on my RPi, attached IR led, added random remote in MiHome app, learned 2 commands with LIRC, made 2 “.sh” scripts with “curl -X POST” to turn on and off input_boolean in HA