I have a sonoff basic I used to control my porch light. Over night I want it to come on when motion is detected then go off after 10 minutes. What happens is the switch on/off is acting more like a toggle, so if motion is detected 2 seconds after the 1st motion, the automation runs, and the switch.turn_on, actually switches it off… Any idea a way to fix this please?
- alias: Front Door Line Crossed light on
initial_state: 'on'
trigger:
- platform: state
entity_id: binary_sensor.front_garden_line_crossing
from: 'off'
to: 'on'
- platform: state
entity_id: binary_sensor.front_garden_field_detection
from: 'off'
to: 'on'
- platform: state
entity_id: binary_sensor.front_garden_motion
from: 'off'
to: 'on'
condition:
condition: time
after: '00:00:01'
before: '06:00:00'
action:
- service: switch.turn_on
entity_id: switch.front_door
- delay: '00:10:00'
- service: switch.turn_off
entity_id: switch.front_door
If the automation re-triggers while waiting in the delay the delay will be cancelled and the action after it executed - leading to the behaviour you are experiencing.
The solution is to not wait in a delay to turn the light off. Instead write a second automation that is triggered by the light being on for 10 minutes that turns the light off.
- alias: Front Door Line Crossed light on
initial_state: 'on'
trigger:
- platform: state
entity_id: binary_sensor.front_garden_line_crossing
from: 'off'
to: 'on'
- platform: state
entity_id: binary_sensor.front_garden_field_detection
from: 'off'
to: 'on'
- platform: state
entity_id: binary_sensor.front_garden_motion
from: 'off'
to: 'on'
condition:
condition: time
after: '00:00:01'
before: '06:00:00'
action:
- service: switch.turn_on
entity_id: switch.front_door
- alias: Front Door light off
initial_state: 'on'
trigger:
- platform: state
entity_id: switch.front_door
to: 'on'
for:
minutes: 10
action:
- service: switch.turn_off
entity_id: switch.front_door