Hello, I am trying to write following automation. However I am not quite sure where I am getting it wrong. I was hoping second look from someone here might help find the problem.
As description mentions, I am looking to turn on the light based of motion between 30 prior to sunset and 10:00 pm and 5:00 am to 30 min past sunrise.
alias: Office Motion Activated Lights - Turn On
description: >-
This automation triggers light on based on motion detected in room / area.
This will automation will work from Sunset (-30 min) until bed time and wake
up time until past an hour after sunrise.
trigger:
- type: motion
platform: device
device_id: b93c7f2f4457139b70823800acb3581e
entity_id: binary_sensor.motion_sensor_office
domain: binary_sensor
condition:
- condition: sun
after: sunset
after_offset: '-00:30:00'
before: sunrise
before_offset: '00:30:00'
- condition: and
conditions:
- condition: time
after: '5:00'
before: '22:00:00'
action:
- type: turn_on
device_id: 7d498ebe74e3caa2cbc359d6cc3e104c
entity_id: light.office
domain: light
brightness_pct: 75
mode: single
This automation does not do anything at this point. However if I remove following, it does turn on lights from 30 min prior to sunset to until midnight.
before: sunrise
before_offset: '00:30:00'
- condition: and
conditions:
- condition: time
after: '5:00'
before: '22:00:00'
I have also tried listing all conditions under one AND condition block as well but that does not seem to work either.
alias: Office Motion Activated Lights - Turn On
description: >-
This automation triggers light on based on motion detected in room / area.
This will automation will work from Sunset (-30 min) until bed time and wake
up time until past an hour after sunrise.
trigger:
- type: motion
platform: device
device_id: b93c7f2f4457139b70823800acb3581e
entity_id: binary_sensor.motion_sensor_office
domain: binary_sensor
for:
hours: 0
minutes: 0
seconds: 5
milliseconds: 0
condition:
- condition: and
conditions:
- condition: time
after: '5:00'
before: '22:30:00'
- condition: sun
after: sunset
after_offset: '-00:30:00'
before: sunrise
before_offset: '00:30:00'
action:
- type: turn_on
device_id: 7d498ebe74e3caa2cbc359d6cc3e104c
entity_id: light.office
domain: light
brightness_pct: 75
mode: single
I’ve probably misunderstood, but I think your conditions will rarely be met. It looks as if you want the light to come on if it is
between 5 am and 10.30 pm
and
between sunset and sunrise
I would be inclined to create separate automation(s) setting an input boolean flag to on when the light should come on and off when it shouldn’t, then using that as the trigger. Much easier to identify problems.
- id: '1614098041320'
alias: Test on
description: Turns on flag at 5 am
trigger:
- platform: time
at: 05:00:00
condition: []
action:
- service: input_boolean.turn_on
data: {}
entity_id: input_boolean.test_flag
mode: single
To turn off the flag:
- id: '1614098618538'
alias: Test off
description: Turns off flag at sunrise + 30 min
trigger:
- platform: sun
event: sunrise
offset: 00:30:00
condition: []
action:
- service: input_boolean.turn_off
data: {}
entity_id: input_boolean.test_flag
mode: single
Then the only condition needed in your main automation is:
condition:
- condition: state
entity_id: input_boolean.test_flag
state: 'on'
A similar pair of automations could turn the flag on and off in the evening (or at other times of the day) with no extra lines needed in the main automation. You might have light sensors turn the flag on and off as well in winter months.
You might be able to use the same flag with several different sets of lights or for other purposes - heating, say?
@finity That makes more sense I assumed, it was OR out of the box. Now it makes sense. Anyhow ended up using @Stiltjack suggestions of flag as it reduces automation yaml size for rest of similar automations.