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.
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"
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"
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.
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:
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
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"
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)
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 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 your help @Tinkerer! I really appreciate it.
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"