Motion Activated Light Automation Help

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

What does it actually do?

Updated the question.

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.

PS…

Does your motion sensor detect light levels as well? (Mine do.) That might make a better trigger, rather than the multiple conditions.

Yes, thats exactly what I want to do but when you AND those two conditions. I expect it should work like below.

Light should turn on
5:00 am to (Sunrise + 30 min)
(Sunset - 30 min) to 10:00 pm

About using Luminance Value - I do have Luminance sensor still the problem of turning it on will persist because of my fixed time requirement.

Can you share some more details about Boolean Flag solution?

To turn on the flag:

- 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?

I’ll fix your post above to make it more obvious what the problem is…

in your automation you were using everything in an and block so those two conditions can never both be true at the same time.

that condition block is pretty wrong…

basically it is saying:

before (sunrise +30) and after (sunset - 30) and after 0500 and before 2200 and ...

try this as the condition:

condition:
  condition: or
  conditions:
    - condition: and
      conditions:
        - condition: sun
          after: sunset
          after_offset: '-00:30:00'
        - condition: time
          after: '22:00:00'
    - condition: and
      conditions:
        - condition: sun
          before: sunrise
          before_offset: '00:30:00'
        - condition: time
          after: '05:00:00'

if you read that out logically it says:

(after 0500 and before (sunrise + 30)) or (before 2200 and after (sunset - 30))

At least I’m pretty sure that’s correct… :wink:

@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.

Thank you for your help and support.