skull75
(Skull75)
April 12, 2021, 4:15am
1
Hi,
I created an automation using UI, the code is as followed:
alias: Turn WC 1 light on
description: ''
trigger:
- type: motion
platform: device
device_id: 8074edb539e8e478a32df9638e845e30
entity_id: binary_sensor.motion_sensor_3
domain: binary_sensor
for:
hours: 0
minutes: 0
seconds: 0
milliseconds: 0
condition:
- condition: state
entity_id: light.toilet_light_1
state: 'off'
action:
- service: light.turn_on
target:
entity_id: light.toilet_light_1
- type: is_no_motion
condition: device
device_id: 8074edb539e8e478a32df9638e845e30
entity_id: binary_sensor.motion_sensor_3
domain: binary_sensor
for:
hours: 0
minutes: 2
seconds: 0
milliseconds: 0
- service: light.turn_off
target:
entity_id: light.toilet_light_1
mode: single
The automated on working without any issues but the turn off cannot work. I need to turn off the light after no motion is detected during 2 mins, is there any wrong in my code?
tom_l
April 12, 2021, 4:24am
2
Yes. As soon as that device condition in the actions evaluates to false the actions stop being run. Your light will never turn off.
Try this:
alias: Turn WC 1 light on
description: ''
trigger:
- platform: state
entity_id: binary_sensor.motion_sensor_3
to: on
action:
- service: light.turn_on
target:
entity_id: light.toilet_light_1
- delay:
minutes: 2
- service: light.turn_off
target:
entity_id: light.toilet_light_1
mode: restart
123
(Taras)
April 12, 2021, 4:33am
3
The condition is incorrect. If you want to wait until the binary_sensor has been off for 2 minutes, you should consider using wait_for_trigger
alias: Turn WC 1 light on
description: ''
trigger:
- type: motion
platform: device
device_id: 8074edb539e8e478a32df9638e845e30
entity_id: binary_sensor.motion_sensor_3
domain: binary_sensor
action:
- service: light.turn_on
target:
entity_id: light.toilet_light_1
- wait_for_trigger:
- platform: state
entity_id: binary_sensor.motion_sensor_3
to: 'off'
for: '00:02:00'
- service: light.turn_off
target:
entity_id: light.toilet_light_1
mode: restart
EDIT
Replaced:
mode: single
max_exceeded: silent
with:
mode: restart
4 Likes
skull75
(Skull75)
April 12, 2021, 4:49am
4
Ahh. That’s right, while the switch state is on, the automation stopped it self.
skull75
(Skull75)
April 13, 2021, 3:17am
5
Thank you. I need wait_for_trigger when the sensor off after 2 minutes. The mode should be “restart” instead of “single”.
skull75
(Skull75)
April 14, 2021, 7:30am
6
wait_for_trigger is not a good option. You cannot move your body continuously to keep the sensor on. Whenever you are stop moving, this automation start the timer and turn off the light in 2 mins after.
Have you got any other solution for that?
123
(Taras)
April 14, 2021, 10:31am
7
It is doing exactly what you asked for in your first post:
So how do you want it to work now?
skull75
(Skull75)
April 14, 2021, 2:59pm
8
Of cause I need it work. Wait for trigger start the timer when no motion is detected but I need another function to reset the timer if the sensor detect a motion again. Do you have any idea?
Thank you.
123
(Taras)
April 14, 2021, 3:03pm
9
Replace this:
mode: single
max_exceeded: silent
with this:
mode: restart
Whenever motion is detected, the entire automation will be restarted (meaning the previous wait_for_trigger
is cancelled).
NOTE
I updated the example to use restart
.
1 Like
skull75
(Skull75)
April 15, 2021, 1:54am
10
Thank you. I’ll try this!
May i ask why you need restart here? Doesn’t the wait_for_trigger already do everything you need in this automation?