Outdoor light - several conditions doesn't seem to work

Hi all,

I have a small problem with some of my automations, regarding my outdoor light.
Currently my Outdoor Light turns on at sunset which seems to work as it should.
The problem occurs when I want to setup conditions with my Xiaomi Motion Sensors and the behaviour of the light when it gets triggered.

The main goal here is:

  • At sunset my spotlight gets turned on and set to 25%
  • When motion gets triggered, the light should boost to 100% and some extra light turns on.
  • The light should stay this way until motion detection changes state from on --> off (+5 minutes)

The above function is only valid during the weekdays from sunset to ‘00:15:00’.
Because at 00:15:00, the light will be switched off (That automation also works)
From ‘00:15:00’ to sunrise, the light will be OFF, but if the motion sensors get triggered, then the light should behave like this:

  • Turn all light on and set brightness to 100%
  • When Motion trigger changes state from ON --> OFF (add 5 minutes) turn off all light.

Here are some of the automations I’m working with:

- id: Backyard Motion Trigger ON (WORKING)
  alias: '[Outdoor] Backyard Motion'
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_1xxxxxxxxxxxxx
    from: 'off'
    to: 'on'
  condition:
    condition: state
    entity_id: sun.sun
    state: 'below_horizon'
  action:
  - service: light.turn_on
    entity_id: 
    - light.backyard_spot
    data:
      brightness_pct: 100
  - service: light.turn_on
    entity_id: 
    - light.backyard_bed_v
    - light.backyard_bed_h
  - service: light.turn_on
    entity_id: 
    - light.led_path
    - light.led_terrace
    data:
      brightness_pct: 100
      color_name: orange

Then I have this one which should turn it off that doesn’t work when I use the parameter before:

- id: Backyard Motion Trigger OFF    
  alias: '[Outdoor] Backyard Motion Trigger OFF  - Evening'
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_1xxxxxxxxxxxxxxxxx
    from: 'on'
    to: 'off'
    for:
      minutes: 5
  condition:
    condition: state
    entity_id: sun.sun
    state: 'below_horizon'
  condition:
    condition: time
    before: '00:15:00'
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
  action:
  - service: light.turn_off
    entity_id: 
    - light.backyard_bed_v
    - light.backyardbaghave_bed_h
    - light.led_path
    - light.led_terrace
  - service: light.turn_on
    entity_id: 
    - light.backyard_spot
    data:
      brightness_pct: 25

And finally I have this one that works:

- id: Turn Light OFF in Garden when Motion is OFF at night    
  alias: '[Outdoor] Baghave Motion Trigger OFF - Night'
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_1xxxxxxxxxxxxxxxxxxx
    from: 'on'
    to: 'off'
    for:
      minutes: 5
  condition:
    condition: time
    after: '00:15:00'
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
  condition:
    condition: sun
    before: sunrise
  action:
  - service: light.turn_off
    entity_id: 
    - light.led_path
    - light.led_terrace
    - light.baghave_bed_v
    - light.backyard_bed_h
    - light.backyard_spot

What am I doing wrong?
I need to have duplicate automations for the weekends, because the timing will be different.
Can I somehow do this even smarter or am I doing it correctly with different set of automations?

Your Backyard Motion Trigger OFF has two conditions with out the - to show it’s a list.

  condition:
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'
    - condition: time
      before: '00:15:00'
      weekday:
        - mon
        - tue
        - wed
        - thu
        - fri

Please note this is the morning after a Polish wedding so, I could be rather wrong with the spacing and dash.

Indeed.

And the one you’re saying is working isn’t valid yaml as you have a duplicate condition key there too :confused:

I’m sorry, but when I validate the files I get no errors and somehow it works :slight_smile:.
I will try to put in the - in front of the conditions then.

So you agree the below should work then?

- id: Turn Light OFF in Garden when Motion is OFF at night  
  alias: '[Outdoor] Baghave Motion Trigger OFF - Night'
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_1xxxxxxxxxxxxxxxxxx
    from: 'on'
    to: 'off'
    for:
      minutes: 5
  condition:
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'
    - condition:
      condition: time
      before: '00:15:00'
      weekday:
        - mon
        - tue
        - wed
        - thu
        - fri
  action:
    - service: light.turn_off
      entity_id: 
        - light.backyard_bed_v
        - light.backyard_bed_h
        - light.led_path
        - light.led_terrace
    - service: light.turn_on
      entity_id: 
        - light.backyard_spot
      data:
        brightness_pct: 25

That’s valid yaml, (except on your second condition you have 2 condition lines, the top one needs to go) but there you have 2 conditions that both must be true for the automation to trigger, and it’s only before 0015 but between midnight and 0015 so that will only fire from midnight to 0015.

You need to cover before midnight aswell :slight_smile:

Can you try to modify it and show me. I want it to work after sunset and until 0015.

- id: Turn Light OFF in Garden when Motion is OFF at night  
  alias: '[Outdoor] Baghave Motion Trigger OFF - Night'
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_1xxxxxxxxxxxxxxxxxx
    from: 'on'
    to: 'off'
    for:
      minutes: 5
  condition:
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon' 
    - condition: or
      conditions:
        - condition: sun
          after: sunset
        - condition: time
          before: '00:15:00'
          weekday:
            - mon
            - tue
            - wed
            - thu
            - fri
  action:
    - service: light.turn_off
      entity_id: 
        - light.backyard_bed_v
        - light.backyard_bed_h
        - light.led_path
        - light.led_terrace
    - service: light.turn_on
      entity_id: 
        - light.backyard_spot
      data:
        brightness_pct: 25

Do I need to use the or - if I want both to be true?

You don’t want them both to be true.

You want

Sun is down and between sunset-midnight

Or

Sun is down and between midnight-0015

So - still a few issues here, so lets start with this one.
Can anyone see why this fails? The lights didn’t turn on, but if I manually run the rule the light do turn on:

- id: Outdoor light ON when sunset
  alias: '[Ourdoor] Sunset'
  trigger:
    platform: sun
    event: sunset
  action:
  - service: light.turn_on
    entity_id: 
    - light.front_spot
    - light.driveway_spot
    data:
      brightness_pct: 25
  - service: light.turn_on
    entity_id: 
    - light.backyard_spot
    data:
      brightness_pct: 100

Hey, it seems to be working when using your rule:

- id: Backyard Motion Sensor Dim Light
  alias: '[Udelys] Baghave Motion Sensor Dim Light'
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_xxxxxxxxxxxxxx
    from: 'on'
    to: 'off'
    for:
      minutes: 5
  condition:
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon' 
    - condition: or
      conditions:
        - condition: sun
          after: sunset
        - condition: time
          before: '01:30:00'
  action:
    - service: light.turn_off
      entity_id:
      - light.baghave_bed_v
      - light.baghave_bed_h
      - light.led_legehussti
      - light.led_terrasse

… but when I add what should happens after ‘01:30:00’ and nutil sunrise it kinda fails:

- id: Backyard Motion Sensor Dim Light - Night
  alias: '[Udelys] Baghave Motion Sensor Dim Light - Night'
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d00016d5aae
    from: 'on'
    to: 'off'
    for:
      minutes: 5
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: sun.sun
        state: 'below_horizon' 
      - condition: time
        after: '01:30:00'
  action:
    - service: light.turn_off
      entity_id:
      - light.baghave_spot
      - light.baghave_bed_v
      - light.baghave_bed_h
      - light.led_legehussti
      - light.led_terrasse

Now the above rule seem to take over now. The light just gets turned of if I walk out and trigger it.
What’s wrong with it :frowning:

That second one looks valid to me on a quick scan - any errors?

No… No errors. Only that the light now turns complete off as stated. But that should only happen after 01:30 in the middle og the night?

Your action sections for both of those automations are to switch off the lights - so isn’t that working as expected? :confused:

Almost… If you look closely at the one that is expected to run before 01:30:00… Then it doesn’t contain the line, which should be kept on…

- light.baghave_spot

Maybe it’s a problem with you system time?

Does it ever work properly if you change the time by an hour or so either direction?

Could be…

But I seem to got it fixed (it worked last night. Had to stay up to test it :sleeping):
The first one takes care of the light from sunset until 01:30:00 next morning.
The second takes care of the light between 01:30:00 and sunrise.
Here is my two working codes:

- id: Backyard Motion Sensor Dim Light
  alias: '[Udelys] Baghave Motion Sensor Dim Light'
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_1xxxxxxxxxxxxxxxxxxxx
    from: 'on'
    to: 'off'
    for:
      minutes: 5
  condition:
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon' 
    - condition: or
      conditions:
        - condition: sun
          after: sunset
        - condition: time
          before: '01:30:00'
  action:
    - service: light.turn_off
      entity_id:
      - light.baghave_bed_v
      - light.baghave_bed_h
      - light.led_legehussti
      - light.led_terrasse
      
- id: Backyard Motion Sensor Dim Light - Night
  alias: '[Udelys] Baghave Motion Sensor Dim Light - Night'
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_1xxxxxxxxxxxxxxxxxxxxxxxxxx
    from: 'on'
    to: 'off'
    for:
      minutes: 5
  condition:
    - condition: and
      conditions:
        - condition: sun
          before: sunrise
        - condition: time
          after: '01:30:00'
  action:
    - service: light.turn_off
      entity_id:
      - light.baghave_spot
      - light.baghave_bed_v
      - light.baghave_bed_h
      - light.led_legehussti
      - light.led_terrasse

So I was having my fingers crossed today, but unfortunately, my light didn’t turn on when the sunset was triggered. Does anyone have an idea why?
Should I try to put in an offset to see if that could do the trick?

- id: Outdoor light ON when sunset
  alias: '[Ourdoor] Sunset'
  trigger:
    platform: sun
    event: sunset
  action:
  - service: light.turn_on
    entity_id: 
    - light.front_spot
    - light.driveway_spot
    data:
      brightness_pct: 25
  - service: light.turn_on
    entity_id: 
    - light.backyard_spot
    data:
      brightness_pct: 100