Exterior light in 2 zones trigging on sun elevation, time and motion

My exterior lighting has tow zones. Both should be on after sunset, but one zone should turn off between 22:00 (10 pm) and 06:45. Additionally the same zone should turn on when motion is detected.

Apparently I don’t get this to work properly. Zone1 does not turn off, and the code could probably be more effective.

Any suggestions are welcome!

Zone 1: cornice
Zone 2: entrance and porch

- alias: Exterior Light ON
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    below: '-3.0'
  action:
    - service: switch.turn_on
      entity_id: 
        - switch.fibaro_ext_light_zone1
        - switch.fibaro_ext_light_zone2
    - service: notify.ios_pals_iphone
      data:
        title: "Utelys"
        message: "Utelyset er slått på"

The above code is working, but how do I turn Zone1 off at 22:00?
This is how far I’ve got

- alias: Exterior light in cornice off at 22:00
  trigger:
    platform: state
    entity_id: switch.fibaro_ext_light_zone1
    to: 'on'
  condition: 
    condition: time
    after: '22:00'
    before: '06:45'
  action:
    service: switch.turn_off
    entity_id: switch.fibaro_ext_light_zone1

Additionally Zone1 should turn ON when motion is detected.

- alias: Motion Sensor Lights Off
  trigger:
    - platform: state
      entity_id: binary_sensor.pir_motion_sensor
      to: 'off'
      for:
        minutes: 4
  action:
    - service: homeassistant.turn_off
      entity_id: switch.fibaro_ext_light_zone1
    - service: notify.ios_pals_iphone
      data:
        title: "Exterior light"
        message: "Exterior light in cornice is off"

All lights off at sunrise

- alias: 'Exterior light OFF'
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    above: '-4.0'
  action:
    - service: switch.turn_off
      entity_id: group.ext_light
    - service: notify.ios_pals_iphone
      data:
        title: "Exterior light"
        message: "Exterior light is OFF"

From the trigger page:

Triggers are what starts the processing of an automation rule. It is possible to specify multiple triggers for the same rule - when any of the triggers becomes true then the automation will start. Once a trigger starts, Home Assistant will validate the conditions, if any, and call the action.

That automation will start processing when the switch turns on, and then check to see if it’s after 22:00 and before 06:45. Instead you want to use a time trigger to turn it off at 22:00:

- alias: Exterior light in cornice off at 22:00
  trigger:
    platform: time
    at: '22:00:00'
  condition: 
    condition: state
    entity_id: switch.fibaro_ext_light_zone1
    state: 'on'
  action:
    service: switch.turn_off
    entity_id: switch.fibaro_ext_light_zone1

Then we get some confusion:

Which? You say on but then write an automation for off. Did you want this?

- alias: Motion Sensor Lights On
  trigger:
    - platform: state
      entity_id: binary_sensor.pir_motion_sensor
      to: 'on'
  condition:
    - condition: numeric_state
      entity_id: sun.sun
      value_template: {{ state_attr('sun.sun','elevation') }}
      below: -3
  action:
    - service: switch.turn_on
      entity_id: switch.fibaro_ext_light_zone1
    - service: notify.ios_pals_iphone
      data:
        title: "Exterior light"
        message: "Exterior light in cornice is on"

I want both Zones to be on when it’s dark enough (sun elevation=-3).
Zone1 should be OFF between 22:00-06:45.
Both zones OFF when sun elevation is -4

The trick is: in Norway the sun now sets at 22:15 and rises at 04:11. So both zones should be turned on… but only zone2 if after 22:00.

Ok, that’s more complicated. You’ve got TWO on automations, one off, and the motion:

  1. Zone one on when going below -3 as long as it’s before 22:00
  2. Zone two on when going below -3
  3. Both zones off when going to above -4

The first would be something like:

- alias: Exterior Light zone one ON
  trigger:
    - platform: numeric_state
     entity_id: sun.sun
     value_template: '{{ state.attributes.elevation }}'
     below: '-3.0'
  condition: 
    - condition: time
     before: '22:00'
  action:
    - service: switch.turn_on
      entity_id: 
        - switch.fibaro_ext_light_zone1
    - service: notify.ios_pals_iphone
      data:
        title: "Utelys"
        message: "Utelyset er slått på"

Thanks! I´ll give this a shot.

EDIT: This worked well :smiley:

In addition to the above, I want the light to be OFF between 22:00 and 06:45 unless when motion is detected.
Now the two automations fight over each other making the light flash for .1 seconds when motion is detected before the other automation restricting light after 22:00 kicks in.

How can I get this to work?
Any clue helping me understand this is highly appreciated.

You can use time conditions.

If you can post both your automations, and describe which you want to be active when, I’m sure we can help you.

1 Like

I appreciate your help, @Tinkerer!

Here are the two automations.
NOTE there are two zones of external lighting. These are lighting up the outside stairs and other lights by the main door, which turns ON using this…

- alias: Utelys - PÅ  # All lights ON
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    below: '-3.0'
  action:
    - service: switch.turn_on
      entity_id: 
        - switch.utelys_inngang_terrasse  # several lights - zone1
        - switch.utelys_gesims.           # several lights - zone2
    - service: notify.ios_pals_iphone
      data:
        title: "Utelys - på kl. {{ states.sensor.time.state }}"
        message: "Utelys er slått på"  

And all external lights turns OFF using this automation:

- alias: 'Utelys AV'
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    above: '-4.0'
  action:
    - service: switch.turn_off
      entity_id: group.utelys
    - service: notify.ios_pals_iphone
      data:
        title: "Utelys av kl. {{ states.sensor.time.state }}"
        message: "Utelyset er slått av"

This all works great.

Now. The one zone, Zone2 controlled by switch.utelys_gesims should act like this:

Zone2 should be off between 22:00 and 06:45, and turn on when motion is detected between 22:00 and 06:45.

The two next automation should do this, but they fight over each other.
I would prefer to have this all in one automaton if possible.

- alias: LYS - Lights off at kl 22:00
  trigger:
    platform: template
    value_template: "{{ is_state('switch.utelys_gesims', 'on') }}" 
  condition:  ## should there be one more condition here to check for motion?
    - condition: time
      after: '22:00:00'
      before: '06:45:00'
  action:
    - service: switch.turn_off
      entity_id: switch.utelys_gesims
    - service: notify.ios_pals_iphone
      data:
        title: "Utelys - Gesims av kl. {{ states.sensor.time.state }}"
        message: "Gesimslys er slått av"
- alias: 'LYS - Utelys på i gesims ved bevegelse'    # turn lights in zone2 ON when motion is detected between 22:00 og 06:45
  trigger:
    platform: state
    entity_id: 
       # Both pirs should equally trigger - they are physically placed in each end of the stairs
      - binary_sensor.utetrapp_pir
      - binary_sensor.ringeapparat_pir
    to: 'on'
  condition:
    condition: and
    conditions:
      - condition: time
        after: '22:00:00'
        before: '06:45:00'
      - condition: state
        entity_id: 'switch.utelys_gesims'
        state: 'off'
  action:
    - service: switch.turn_on
      entity_id: switch.utelys_gesims
    - delay: 00:02:00
    - service: switch.turn_off
      entity_id: switch.utelys_gesims

That should just be a normal state trigger

- alias: LYS - Lights off at kl 22:00
  trigger:
    platform: state
    entity_id: switch.utelys_gesims
    to: 'on' 

There are a couple of solutions, one involving an input boolean, but the simpler is likely to have the PIR automation turn on and off the other one:

  action:
    - service: automation.turn_off
      entity_id: automation.lys_lights_off_at_kl_2200
    - service: switch.turn_on
      entity_id: switch.utelys_gesims
    - delay: 00:02:00
    - service: switch.turn_off
      entity_id: switch.utelys_gesims
    - service: automation.turn_on
      entity_id: automation.lys_lights_off_at_kl_2200

Replace automation.lys_lights_off_at_kl_2200 with the actual entity_id of the first automation

I ended up with this, which is triggered by the two PIRs and otherwise seems to work as I want except for one thing: It does not turn off at 21:00 .

However, when triggering the [Lys] Utelys AV i gesims kl 21:00 manually from the States-page all seems to work. What am I missing?

- alias: '[Lys] Utelys - PÅ'  ## Both Zones
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    below: '-3.0'
  action:
    - service: switch.turn_on
      entity_id: 
        - switch.utelys_inngang_terrasse
        - switch.utelys_gesims     # This zone should be off at 21:00 and triggered by two PIR´s
    - service: notify.ios_pals_iphone
      data:
        title: "Utelys - på kl. {{ states.sensor.time.state }}"
        message: "Utelys er slått på"  
  
- alias: '[Lys] Utelys AV i gesims kl 21:00' # turn off the lights in the "gesims" (cornice) zone
  trigger:
    platform: state
    entity_id: switch.utelys_gesims
    to: 'on'. # is this equal to `state=on`, or does it only trigger when state changes to `on`?
  condition: 
    - condition: time
      after: '21:00:00'
      before: '06:45:00'
    - condition: or
      conditions:
        - condition: state
          entity_id: binary_sensor.pir_utetrapp
          state: 'off'
        - condition: state
          entity_id: binary_sensor.ringeapparat_pir
          state: 'off'
  action:
    - delay: 00:00:30    # how long the light should be on
    - service: switch.turn_off
      entity_id: switch.utelys_gesims
    - service: notify.ios_pals_iphone
      data:
        title: "Utelys - Gesims av kl. {{ states.sensor.time.state }}"
        message: "Gesimslys er slått av"

- alias: '[Lys] Utelys på i gesims ved bevegelse'  #This part works. Light goes on when motion is detected by the PIRs.
  trigger:
    platform: state
    entity_id: 
      - binary_sensor.pir_utetrapp
      - binary_sensor.ringeapparat_pir
    to: 'on'
  condition:
    - condition: time
      after: '21:00:00'
      before: '06:45:00'
  action:
    - service: switch.turn_on
      entity_id: switch.utelys_gesims

- alias: '[Lys] Utelys AV'
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    above: '-4.0'
  action:
    - service: switch.turn_off
      entity_id: group.utelys
    - service: notify.ios_pals_iphone
      data:
        title: "Utelys av kl. {{ states.sensor.time.state }}"
        message: "Utelyset er slått av"

What does not turn off at 21:00?

  • The first automation triggers when the sun goes below -3 degrees (regardless of time)
    • The switches are turned on
  • The second triggers when switch.utelys_gesims turns on
    • The time has to be between 21:00 and 06:45
    • Either of the sensors have to be off
    • The switches are turned off after 30 seconds
  • The third triggers when either switch turns on
    • The time has to be between 21:00 and 06:45
    • It turns on switch.utelys_gesims
  • The last triggers when the sun goes below -4 degrees (regardless of time)
    • It turns off all switches in group.utelys

switch.utelys_gesims is not turning off at 21:00

There are two zones:

- switch.utelys_inngang_terrasse
- switch.utelys_gesims 

So I want the witch.utelys_gesims to turn off at 21:00. This Zone also turns on when motion is detected by the PIR´s.
The switch.utelys_inngang_terrasse stays on according to the sun.

Now it works like this:

  • The first automation triggers when the sun goes below -3 degrees (regardless of time)
    • The switches are turned on
  • The second triggers when switch.utelys_gesims turns on
    • The time has to be between 21:00 and 06:45
    • Either of the PIRS have to be off
    • The switches are turned off after 30 seconds
  • The third triggers when either PIR turns on (on motion)
    • The time has to be between 21:00 and 06:45 (this is when the gesims zone is off)
    • It turns on switch.utelys_gesims
  • The last triggers when the sun goes below -4 degrees (regardless of time)
    • It turns off all switches in group.utelys

You haven’t got any automation that triggers at 21:00 to do that. The closest you’ve got is that second automation that _if it turns on after 21:00 will turn it off 30 seconds later if either sensor hasn’t detected motion.

I’m not entirely sure that’s the logic you want there then. I’d suggest that you need that automation triggered by:

  • The time being 21:10
  • The switch being on for 30 seconds
  • The motion sensors reporting no motion for 30 seconds

With conditions of:

  • The time being between 21:00 and 06:45
  • The switch being on
  • The motion sensor(s) being off
- alias: '[Lys] Utelys AV i gesims kl 21:00' # turn off the lights in the "gesims" (cornice) zone
  trigger:
    - platform: state
      entity_id: switch.utelys_gesims
      to: 'on'
      for: '00:00:30'
    - platform: state
      entity_id: binary_sensor.pir_utetrapp
      to: 'off'
      for: '00:00:30'
    - platform: state
      entity_id: binary_sensor.ringeapparat_pir
      to: 'off'
      for: '00:00:30'
    - platform: time
      at: '21:00:00'
  condition: 
    - condition: time
      after: '21:00:00'
      before: '06:45:00'
    - condition: state
      entity_id: switch.utelys_gesims
      state: 'off'
    - condition: or
      conditions:
        - condition: state
          entity_id: binary_sensor.pir_utetrapp
          state: 'off'
        - condition: state
          entity_id: binary_sensor.ringeapparat_pir
          state: 'off'
  action:
    - service: switch.turn_off
      entity_id: switch.utelys_gesims
    - service: notify.ios_pals_iphone
      data:
        title: "Utelys - Gesims av kl. {{ states.sensor.time.state }}"
        message: "Gesimslys er slått av"

I was hoping this would turn it off, if no motion was detected whitin the time frame, delayed by 30 seconds.

  trigger:
    platform: state
    entity_id: switch.utelys_gesims
    to: 'on'
  condition: 
    - condition: time
      after: '21:00:00'
      before: '06:45:00'
    - condition: or
      conditions:
        - condition: state
          entity_id: binary_sensor.pir_utetrapp
          state: 'off'
          # for: 
          #   seconds: 5
        - condition: state
          entity_id: binary_sensor.ringeapparat_pir
          state: 'off'
          # for: 
          #   seconds: 5
  action:
    - delay: 00:00:30    # this is how long the light should be on
    - service: switch.turn_off
      entity_id: switch.utelys_gesims

Will this trigger _only when the state _chages to on?
When triggering this automation manually through the Stadges page, it works as expected - leading to the trikker itself…

- alias: '[Lys] Utelys AV i gesims kl 21:00'
  trigger:
    platform: state
    entity_id: switch.utelys_gesims
    to: 'on'

If that is the case, it will never trigger, because the light is (most of the year) on at 21:00.
I am looking for a trigger that fires when the light is on, not when it turns on.

I suspect you may have skipped reading the documentation on how trigger works:

Triggers are what starts the processing of an automation rule. It is possible to specify multiple triggers for the same rule - when any of the triggers becomes true then the automation will start. Once a trigger starts, Home Assistant will validate the conditions, if any, and call the action.

I’ve added some emphasis there

You really need to read the docs :wink: Triggers are always on change

Because, as explained in the docs, that bypasses the trigger and condition blocks completely

It can’t because the trigger is on the switch turning on.

If you want the light to turn off at 21:00 if it is on, then that’s what you have to write.

Thank you for clarifying this.

Thank you for your help @Tinkerer! I really appreciate it. :+1:t2::+1:t2::+1:t2:
Here is my final code for controlling two zone external light, triggered by the sun, time and two motion sensors.

- alias: '[Light] Exterior lights ON'
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    below: '-3.0'
  action:
    - service: switch.turn_on
      entity_id: 
        - switch.utelys_zone1
        - switch.utelys_zone2
    - service: notify.ios_pals_iphone
      data:
        title: "Exterior light - on at {{ states.sensor.time.state }}"
        message: "Exterior light is switched on"  


- alias: '[Light] Exterior light OFF in zone2 at 21:00' # turn off the lights in the cornice zone
  trigger:
    - platform: state
      entity_id: switch.utelys_zone2
      to: 'on'
      for: '00:00:30'
    - platform: state
      entity_id: binary_sensor.pir_exterior_stairs
      to: 'off'
      for: '00:00:30'
    - platform: state
      entity_id: binary_sensor.doorbell_pir
      to: 'off'
      for: '00:00:30'
    - platform: time
      at: '21:00:00'
  condition: 
    - condition: time
      after: '21:00:00'
      before: '06:45:00'
    - condition: state
      entity_id: switch.utelys_zone2
      state: 'on'
    - condition: or
      conditions:
        - condition: state
          entity_id: binary_sensor.pir_exterior_stairs
          state: 'off'
        - condition: state
          entity_id: binary_sensor.doorbell_pir
          state: 'off'
  action:
    - service: switch.turn_off
      entity_id: switch.utelys_zone2    
    - service: notify.ios_pals_iphone
      data:
        title: "Exterior lights - Cornice off at {{ states.sensor.time.state }}"
        message: "Lights in cornice is off"


- alias: '[Light] Exterior light ON in cornice when motion is detected'
  trigger:
    platform: state
    entity_id:
      - binary_sensor.pir_exterior_stairs
      - binary_sensor.doorbell_pir
    to: 'on'
  condition:
    - condition: time
      after: '21:00:00'
      before: '06:45:00'
  action:
    - service: switch.turn_on
      entity_id: switch.utelys_zone2

- alias: '[Light] Exterior lights OFF'
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    above: '-4.0'
  action:
    - service: switch.turn_off
      entity_id: 
        - switch.utelys_zone1
        - switch.utelys_zone2
    - service: notify.ios_pals_iphone
      data:
        title: "Exterior lights off at {{ states.sensor.time.state }}"
        message: "Exterior lights are off"
1 Like