Hi, I have a motion sensor setup at my entrance (Philips Hue Outdoor sensor, I have one at my gate as well). As soon as I add the condition to run the automation between sunset and sunrise the automation doesn’t work.
From the documentation Sunset/Sunrise Condition:
Therefore, to cover time between sunset and sunrise one need to use after: sunset and before: sunriseas 2 separate conditions and combine them using or .
condition:
condition: or
conditions:
- condition: sun
after: sunset
- condition: sun
before: sunrise
alias: deCONZ motion Entré
description: >-
Turns on Entré lights for 2 min (restarting), when motion is detected between
sunset/sunrise and below 10 lux
trigger:
- platform: state
entity_id: binary_sensor.entre_motion
to: 'on'
condition:
- "{{ states('sensor.infart_motion_light_level') | int < 10 }}"
- condition: or
conditions:
- condition: sun
after: sunset
- condition: sun
before: sunrise
action:
- service: light.turn_on
target:
entity_id: light.entre
data:
brightness_pct: 40
- wait_for_trigger:
- platform: state
entity_id: binary_sensor.entre_motion
to: 'off'
for: '00:02'
- service: light.turn_off
target:
entity_id: light.entre
data:
transition: 3
mode: restart
The above code contains wait_for_trigger that isn’t advisable if it spans for a very long time. You can specify the trigger separately like below-
alias: deCONZ motion Entré
description: >-
Turns on Entré lights for 2 min (restarting), when motion is detected between
sunset/sunrise and below 10 lux
trigger:
- platform: state
entity_id: binary_sensor.entre_motion
to: 'on'
- platform: state
entity_id: binary_sensor.entre_motion
to: 'off'
for: '00:02'
condition:
- "{{ states('sensor.infart_motion_light_level') | int < 10 }}"
- condition: or
conditions:
- condition: sun
after: sunset
- condition: sun
before: sunrise
action:
- choose:
- conditions:
- "{{ trigger.to_state.state == 'on' }}"
sequence:
- service: light.turn_on
target:
entity_id: light.entre
data:
brightness_pct: 40
default:
- service: light.turn_off
target:
entity_id: light.entre
data:
transition: 3
Many thanks! I get the “sun” part now when reading the documentation after your example (and that I can use below.horizon as well).
But I don’t get that “wait_for_trigger” isn’t a good choice…? Will your example restart the timer for 2 minutes if motion is detected?
This was a little more complicated than my knowledge, so sorry for asking. But still learning and enjoying HA !
The wait_for_trigger is not a good choice if the area is crowded. The reason simply because the automation will be waiting for the OFF trigger - and if the server is down/restarted while waiting, the automation will no longer work.
However, if the area is not crowded, the use of wait_for_trigger is OK.