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.
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…
I progressed a bit… now it turns on with the correct color/Brightness, but it doesn’t want to turn off anymore
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 ?
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