Xiaomi motion sensor automation problem

Hello everyone.

I own a couple of motion sensors from Xiaomi and created an automation to turn on a light when motion is detected and then turn it of after the “no motion” attribute is higher than 180 seconds.

Here it is:

- alias: Luzes da casa - Suíte
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d000120af26
    from: 'off'
    to: 'on'
  action:
    - condition: state
      entity_id: light.suite_tv
      state: 'off'
    - service: light.turn_on
      entity_id: light.suite_tv
    - wait_template: "{% if states.binary_sensor.motion_sensor_158d000120af26.attributes['No motion since'] | int >= 180 and states.binary_sensor.motion_sensor_158d000120af26.state == 'off' %}True{% else %}False{% endif %}"
    - condition: state
      entity_id: light.suite_tv
      state: 'on'
    - service: light.turn_off
      entity_id: light.suite_tv

The automation works ok but there is a problem: if I walk in the room and the automation starts, I exit the room quickly, after a minute, no motion is detected (state = off) and then after another minute “no motion” is 120 seconds (not 180 that triggers the turn_off command), if I walk on the room again (state of the sensor = on again), the light immediately turns off… I have no idea why.

My ideia is that the “no motion” should reset to zero and the “wait_template” should still result “false” (since if statement requires both the sensor state and “no motion” attributes to result “true”), then after a couple of minutes out of the room again, state should be off and after “no motion” reaches 180, light goes off.

Can someone help me here?

Thanks!!!

Create two separate automation for light.turn_on and light.turn_off.

I use this code to automate in a similar way lights during the night:

- alias: Gateway light on/off based on motion
  trigger:
    - platform: state
      entity_id: binary_sensor.motion_sensor_id
      to: 'on'
    - platform: state
      entity_id: binary_sensor.motion_sensor_id
      to: 'off'
      for:
        minutes: 1
        seconds: 30
  condition:
    condition: time
    after: '22:30'
    before: '06:25'
  action:
    service_template: >
      {% if trigger.to_state.state == "on" %}
      homeassistant.turn_on
      {% elif trigger.to_state.state == "off" %}
      homeassistant.turn_off
      {% endif %}
    entity_id: light.gateway_light_id
3 Likes

Thanks! I will try. I have 6 rooms with that automation, now it is time to separate my automation files in to a directory :slight_smile:

EDIT: well, if I do that, there is still a big problem, described on my next post…

Hello @substa! Thanks for your reply.

I really like your approach, very simple and effective.

I was about to give it a try but there is something I really need to make it work for me: the way I wrote mine, If I turn on the light manually, since it has a condition on the very beginning, I can override this automation and keep my light on as long as I turn it off again. A very nice solution when I take a bath, for example, the sensor does not detect me on the shower and the lights turns of by the automation, very inconvenient hehe

Any ideas on how to overcome that?

I tought maybe this could work, but the light would still be turned off after one and a half minute:

- alias: Luzes da casa - Suíte
  trigger:
    - platform: state
      entity_id: binary_sensor.motion_sensor_158d000120af26
      to: 'on'
    - platform: state
      entity_id: binary_sensor.motion_sensor_158d000120af26
      to: 'off'
      for:
        minutes: 1
        seconds: 30
  action:
    service_template: >
      {% if trigger.to_state.state == "on" and states.light.suite_tv.state == "off" %}
      homeassistant.turn_on
      {% elif trigger.to_state.state == "off" and states.light.suite_tv.state == "on" %}
      homeassistant.turn_off
      {% endif %}
    entity_id: light.suite_tv

I took out your condition since I have a couple of automations that runs on sunset/sunrise to turn on and off these lights automations itself…

Thanks!

Hello,

I think you can try to use the state in the condition, to start the automation only if the light is off

1 Like

Hello! But how can I do that since the on and off automations are on the same trigger? Can I set a condition between the on and off triggers? Thanks.

Hello guys!

Just a heads up: I found a solution by setting the first condition to the triggering data:

- alias: Luzes da casa - Suíte
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d000120af26
    from: 'off'
    to: 'on'
  condition:
    condition: state
    entity_id: light.suite_tv
    state: 'off'
  action:
    - service: light.turn_on
      entity_id: light.suite_tv
    - wait_template: "{% if states.binary_sensor.motion_sensor_158d000120af26.attributes['No motion since'] | int >= 180 and states.binary_sensor.motion_sensor_158d000120af26.state == 'off' %}True{% else %}False{% endif %}"
    - condition: state
      entity_id: light.suite_tv
      state: 'on'
    - service: light.turn_off
      entity_id: light.suite_tv

It is working great! Now the automation does not start at all if the lights are on.

Thanks.

4 Likes

Smart approach :slight_smile:

1 Like

Now I get it! It was your suggestion at first hahaha

Sorry mate! I have not understood your idea at first glance…

Thanks!

Hello,

new HA user here.

after some research, I have found your example which is what I was looking for.

I’m trying to write a simple modification but cannot find how to do it.
I’d like to turn on the light with a specific color/brightness.

I changed your code, but obviously it doesn’t work :frowning:

  action:
    service_template: >
      {% if trigger.to_state.state == "on" %}
      light.turn_on
      {% elif trigger.to_state.state == "off" %}
      light.turn_off
      {% endif %}
    data_template:
      entity_id: light.hallway
      brightness: 1
      rgb_color: 255,0,255 

would you have any advice how to do it properly ?

Thank you

I progressed a bit… now it turns on with the correct color/Brightness, but it doesn’t want to turn off anymore :frowning:

Apparently, the rgb_color needed some brackets… I don’t know if it is normal or not…
When I check the device state, I don’t see any brackets though, so how can you tell ?

  action:
    service_template: >
      {% if trigger.to_state.state == "on" %}
      light.turn_on
      {% elif trigger.to_state.state == "off" %}
      light.turn_off
      {% endif %}
    # entity_id: light.pilar
    data_template:
      entity_id: light.pilar
      brightness: 1
      rgb_color: [255,0,255] 

The error I get is:
Invalid service data for light.turn_off: extra keys not allowed @ data[‘brightness’]. Got ‘1’
extra keys not allowed @ data[‘rgb_color’]. Got [‘255’, ‘0’, ‘255’]

what is the trick to keep it simple and have both On and Off action working ?

I have a iris ZigBee motion sensor. It looks like the only attributes are on and off. I’m new so I could be wrong. This is my 1st attempt at a useful automation.

I just want the lights to turn off as soon as the sensor goes to off, but like the initial automation, I don’t want anything to happen if the lights were already on.

Does this look good?

  trigger:
  - platform: state
    entity_id: binary_sensor.centralite_3326l_05989ed5_1_1280
    from: 'off'
    to: 'on'
  condition:
  - condition: state
    entity_id: group.office_lights
    state: 'off'
 - condition: state
    entity_id: sun.sun
    state: 'below_horizon'
  action:
  - service: homeassistant.turn_on
    entity_id: group.office_lights
  - wait_template: "{% if states.binary_sensor.motion_sensor_158d000120af26.state == 'off' %}True{% else %}False{% endif %}"
  - condition: state
    entity_id: group.office_lights
    state: 'on'
  - service: homeassistant.turn_off
    entity_id: group.office_lights