Skip to content

Instantly share code, notes, and snippets.

@kalhimeo
Last active February 4, 2024 20:30
Show Gist options
  • Save kalhimeo/8a4b100b8a4540080c9893a5826b78cc to your computer and use it in GitHub Desktop.
Save kalhimeo/8a4b100b8a4540080c9893a5826b78cc to your computer and use it in GitHub Desktop.
Automatic outdoor lighting
blueprint:
name: Automatic outdoor lighting
description: |
Turn outdoor lights automaticly depending of :
- Luminosity (with optional time limit)
- Presence/Movement detection
- Person / Device location (arriving home)
**Version**: 2024.02.0
author: kalhimeo
source_url: https://gist.github.com/kalhimeo/8a4b100b8a4540080c9893a5826b78cc
domain: automation
input:
light_entity:
name: Light entity
description: >
Choose the light to control
selector:
entity:
filter:
domain:
- light
light_percent:
name: Light entity dimm percent
description: >
Define the dimm percentage of the light if applicable
(leave 0 to simply turn ON the light without dimm value)
selector:
number:
min: 0
max: 100
step: 5
unit_of_measurement: "%"
mode: slider
default: 0
reset_helper:
name: "'Date and time' helper entity"
description: >
Choose the mandatory 'date and time' helper (previously created) to allow auto light OFF
selector:
entity:
filter:
domain:
- input_datetime
luminosity_entity:
name: Luminosity entity
description: >
This entity should be ON during night time and OFF during day time
selector:
entity:
filter:
domain:
- binary_sensor
detectors_entity:
name: Detectors entities
description: >
Choose any number of detector for triggering the light
(leave empty if unused)
selector:
entity:
multiple : true
filter:
domain:
- binary_sensor
default: []
detector_time:
name: Detector turn-on time
description: How many seconds should the light stay ON after a dector triggers
selector:
number:
min: 5
max: 3600
step: 5
unit_of_measurement: seconds
mode: slider
default: 120
# location_zone:
# name: Location based zone
# description: >
# Select the HA zone in which the persons should arrive to turn on the lights
# (leave empty if unused)
# default: ""
# selector:
# entity:
# filter:
# domain: zone
location_zone2:
name: Location based zone
description: >
Enter the entity name of the HA zone in which the persons should arrive to turn on the lights
(important: do not leave empty, a valid zone is required: keep 'zone.home' if unused)
default: "zone.home"
location_entity:
name: Location based persons
description: >
Choose any person or device_tracker who turn on the light when arriving in the zone
(leave empty if unused)
selector:
entity:
multiple : true
filter:
domain:
- person
- device_tracker
default: []
location_time:
name: Location based turn-on time
description: How many seconds should the light stay ON after a location triggers
selector:
number:
min: 5
max: 3600
step: 5
unit_of_measurement: seconds
mode: slider
default: 900
automatic_luminosity:
name: Automatic light turn-on at night
description: If enabled, the light will turn ON automatically when the luminosity entity is ON (=night)
selector:
boolean:
default: false
max_luminosity_weekday:
name: Limit automatic lighting at weekday nights
description: >
Define a time limit after which the automatic lighting stops at weekday nights (Sun-Th)
(anything after 12:00 is considered as same day/evening, anything below as next day morning)
selector:
time:
default: "22:00:00"
max_luminosity_weekend:
name: Limit automatic lighting at weekend nights
description: >
Define a time limit after which the automatic lighting stops at weekend nights (Fr-Sa)
(anything after 12:00 is considered as same day/evening, anything below as next day morning)
selector:
time:
default: "00:00:00"
force_luminosity:
name: Force light ON at night
description: >
If enabled, when manually switching OFF the light during the night, the automation will turn the light ON again after a delay
(does not affect other triggers like detector or location based events)
selector:
boolean:
default: false
force_time:
name: Force light ON delay
description: After how many seconds the light should turn ON again, after it was manually switched OFF during the night
selector:
number:
min: 5
max: 300
step: 5
unit_of_measurement: seconds
mode: slider
default: 15
mode: queued
max: 999
#trigger_variables:
# location_zone: !input location_zone
variables:
light_entity: !input light_entity
light_percent: !input light_percent
luminosity_entity: !input luminosity_entity
reset_helper: !input reset_helper
detectors_entity: !input detectors_entity
detector_time: !input detector_time
location_entity: !input location_entity
location_time: !input location_time
automatic_luminosity: !input automatic_luminosity
max_luminosity_weekday: !input max_luminosity_weekday
max_luminosity_weekend: !input max_luminosity_weekend
force_luminosity: !input force_luminosity
force_time: !input force_time
trigger:
- platform: state
entity_id: !input light_entity
id: light_state
from: null
to: null
alias: " When Light changes state"
- platform: state
entity_id: !input detectors_entity
id: detector
from: null
to: "on"
alias: When Presence is detected on any detector
- platform: zone
entity_id: !input location_entity
id: location
zone: !input location_zone2
event: enter
alias: When Persons arrives in zone
- platform: state
entity_id: !input luminosity_entity
id: luminosity
from: null
to: null
alias: When Luminosity threshold changes state
- platform: time
id: auto-off
at: !input reset_helper
alias: When auto OFF time is reached
- platform: event
event_type: automation_reloaded
id: reload
condition: []
action:
- choose:
- conditions:
- condition: trigger
id:
- light_state
sequence:
- if:
- condition: template
value_template: >-
{{ trigger.to_state.state != trigger.from_state.state and trigger.to_state.state == "off" }}
alias: Light state changed to OFF
then:
- service: input_datetime.set_datetime
data:
timestamp: 1
alias: Reset auto OFF
target:
entity_id: !input reset_helper
- delay:
hours: 0
minutes: 0
seconds: !input force_time
milliseconds: 0
- if:
- condition: template
value_template: >-
{{ automatic_luminosity }}
- condition: template
value_template: >-
{{ force_luminosity }}
- condition: state
entity_id: !input luminosity_entity
state: "on"
alias: Luminosity is below threshold (NIGHT)
- condition: state
entity_id: !input light_entity
state: "off"
then:
- if:
- condition: template
value_template: >-
{{ (now().hour > 12 and now().hour < 24 and now().weekday() in (0,1,2,3,6)) or (now().hour < 12 and now().hour > 0 and now().weekday() in (1,2,3,4,5)) }}
alias: Weekday
then:
- if:
- condition: template
value_template: >-
{{ (720 < (max_luminosity_weekday[0:2])|int * 60 + (max_luminosity_weekday[3:5])|int and 720 < (now().hour * 60) + now().minute and (now().hour * 60) + now().minute < (max_luminosity_weekday[0:2])|int * 60 + (max_luminosity_weekday[3:5])|int) or (721 > (max_luminosity_weekday[0:2])|int * 60 + (max_luminosity_weekday[3:5])|int and 721 > (now().hour * 60) + now().minute and (now().hour * 60) + now().minute < (max_luminosity_weekday[0:2])|int * 60 + (max_luminosity_weekday[3:5])|int) }}
alias: Limit is between 12:01 and 24:00, actual time between 12:01 and 00:00, and actual time before that Limit OR Limit is between 00:00 and 12:00, actual time between 00:00 and 12:00, and actual time before that limit
then:
- service: input_datetime.set_datetime
target:
entity_id: !input reset_helper
alias: Set auto OFF at max time
data_template:
datetime: >-
{{ (now()|as_timestamp|timestamp_custom('%Y-%m-%d') + ' ' + max_luminosity_weekday) }}
- if:
- condition: template
value_template: >-
{{ light_percent > 0 }}
then:
- service: light.turn_on
data_template:
entity_id: !input light_entity
brightness_pct: "{{ light_percent }}"
alias: Turn ON the light with brightness
else:
- service: light.turn_on
data: {}
target:
entity_id: !input light_entity
else:
- if:
- condition: template
value_template: >-
{{ 721 > (max_luminosity_weekday[0:2])|int * 60 + (max_luminosity_weekday[3:5])|int and 720 < (now().hour * 60) + now().minute and (now().hour * 60) + now().minute > (max_luminosity_weekday[0:2])|int * 60 + (max_luminosity_weekday[3:5])|int }}
alias: Limit is between 00:00 and 12:00, actual time between 12:01 and 00:00, and actual time above that limit
then:
- service: input_datetime.set_datetime
target:
entity_id: !input reset_helper
alias: Set auto OFF at max time
data_template:
datetime: >-
{{ (((now()|as_timestamp)|int + 86400)|timestamp_custom('%Y-%m-%d') + ' ' + max_luminosity_weekday) }}
- if:
- condition: template
value_template: >-
{{ light_percent > 0 }}
then:
- service: light.turn_on
data_template:
entity_id: !input light_entity
brightness_pct: "{{ light_percent }}"
alias: Turn ON the light with brightness
else:
- service: light.turn_on
data: {}
target:
entity_id: !input light_entity
alias: Check if we are still before the time limit
else:
- if:
- condition: template
value_template: >-
{{ (720 < (max_luminosity_weekend[0:2])|int * 60 + (max_luminosity_weekend[3:5])|int and 720 < (now().hour * 60) + now().minute and (now().hour * 60) + now().minute < (max_luminosity_weekend[0:2])|int * 60 + (max_luminosity_weekend[3:5])|int) or (721 > (max_luminosity_weekend[0:2])|int * 60 + (max_luminosity_weekend[3:5])|int and 721 > (now().hour * 60) + now().minute and (now().hour * 60) + now().minute < (max_luminosity_weekend[0:2])|int * 60 + (max_luminosity_weekend[3:5])|int) }}
alias: Limit is between 12:01 and 24:00, actual time between 12:01 and 00:00, and actual time before that Limit OR Limit is between 00:00 and 12:00, actual time between 00:00 and 12:00, and actual time before that limit
then:
- service: input_datetime.set_datetime
target:
entity_id: !input reset_helper
alias: Set auto OFF at max time
data_template:
datetime: >-
{{ (now()|as_timestamp|timestamp_custom('%Y-%m-%d') + ' ' + max_luminosity_weekend) }}
- if:
- condition: template
value_template: >-
{{ light_percent > 0 }}
then:
- service: light.turn_on
data_template:
entity_id: !input light_entity
brightness_pct: "{{ light_percent }}"
alias: Turn ON the light with brightness
else:
- service: light.turn_on
data: {}
target:
entity_id: !input light_entity
else:
- if:
- condition: template
value_template: >-
{{ 721 > (max_luminosity_weekend[0:2])|int * 60 + (max_luminosity_weekend[3:5])|int and 720 < (now().hour * 60) + now().minute and (now().hour * 60) + now().minute > (max_luminosity_weekend[0:2])|int * 60 + (max_luminosity_weekend[3:5])|int }}
alias: Limit is between 00:00 and 12:00, actual time between 12:01 and 00:00, and actual time above that limit
then:
- service: input_datetime.set_datetime
target:
entity_id: !input reset_helper
alias: Set auto OFF at max time
data_template:
datetime: >-
{{ (((now()|as_timestamp)|int + 86400)|timestamp_custom('%Y-%m-%d') + ' ' + max_luminosity_weekend) }}
- service: light.turn_on
data: {}
target:
entity_id: !input light_entity
alias: Turn ON the light
alias: Try to turn the light ON again at night
alias: >-
When state changes to OFF -> Reset auto OFF and test for Luminosity
- conditions:
- condition: trigger
id:
- luminosity
sequence:
- choose:
- conditions:
- condition: state
entity_id: !input luminosity_entity
state: "off"
alias: Luminosity above threshold (DAY)
- condition: state
entity_id: !input light_entity
state: "on"
alias: Light is presently ON
sequence:
- service: light.turn_off
data: {}
target:
entity_id: !input light_entity
alias: Always turn the light OFF
- conditions:
- condition: state
entity_id: !input luminosity_entity
state: "on"
alias: Luminosity is below threshold (NIGHT)
sequence:
- choose:
- conditions:
- condition: template
value_template: >-
{{ automatic_luminosity }}
- condition: state
entity_id: !input light_entity
alias: Light is presently OFF
state: "off"
sequence:
- if:
- condition: template
value_template: >-
{{ (now().hour > 12 and now().hour < 24 and now().weekday() in (0,1,2,3,6)) or (now().hour < 12 and now().hour > 0 and now().weekday() in (1,2,3,4,5)) }}
alias: Weekday
then:
- if:
- condition: template
value_template: >-
{{ (720 < (max_luminosity_weekday[0:2])|int * 60 + (max_luminosity_weekday[3:5])|int and 720 < (now().hour * 60) + now().minute and (now().hour * 60) + now().minute < (max_luminosity_weekday[0:2])|int * 60 + (max_luminosity_weekday[3:5])|int) or (721 > (max_luminosity_weekday[0:2])|int * 60 + (max_luminosity_weekday[3:5])|int and 721 > (now().hour * 60) + now().minute and (now().hour * 60) + now().minute < (max_luminosity_weekday[0:2])|int * 60 + (max_luminosity_weekday[3:5])|int) }}
alias: Limit is between 12:01 and 24:00, actual time between 12:01 and 00:00, and actual time before that Limit OR Limit is between 00:00 and 12:00, actual time between 00:00 and 12:00, and actual time before that limit
then:
- service: input_datetime.set_datetime
target:
entity_id: !input reset_helper
alias: Set auto OFF at max time
data_template:
datetime: >-
{{ (now()|as_timestamp|timestamp_custom('%Y-%m-%d') + ' ' + max_luminosity_weekday) }}
- if:
- condition: template
value_template: >-
{{ light_percent > 0 }}
then:
- service: light.turn_on
data_template:
entity_id: !input light_entity
brightness_pct: "{{ light_percent }}"
alias: Turn ON the light with brightness
else:
- service: light.turn_on
data: {}
target:
entity_id: !input light_entity
else:
- if:
- condition: template
value_template: >-
{{ 721 > (max_luminosity_weekday[0:2])|int * 60 + (max_luminosity_weekday[3:5])|int and 720 < (now().hour * 60) + now().minute and (now().hour * 60) + now().minute > (max_luminosity_weekday[0:2])|int * 60 + (max_luminosity_weekday[3:5])|int }}
alias: Limit is between 00:00 and 12:00, actual time between 12:01 and 00:00, and actual time above that limit
then:
- service: input_datetime.set_datetime
target:
entity_id: !input reset_helper
alias: Set auto OFF at max time
data_template:
datetime: >-
{{ (((now()|as_timestamp)|int + 86400)|timestamp_custom('%Y-%m-%d') + ' ' + max_luminosity_weekday) }}
- if:
- condition: template
value_template: >-
{{ light_percent > 0 }}
then:
- service: light.turn_on
data_template:
entity_id: !input light_entity
brightness_pct: "{{ light_percent }}"
alias: Turn ON the light with brightness
else:
- service: light.turn_on
data: {}
target:
entity_id: !input light_entity
alias: Check if we are still before the time limit
else :
- if:
- condition: template
value_template: >-
{{ (720 < (max_luminosity_weekend[0:2])|int * 60 + (max_luminosity_weekend[3:5])|int and 720 < (now().hour * 60) + now().minute and (now().hour * 60) + now().minute < (max_luminosity_weekend[0:2])|int * 60 + (max_luminosity_weekend[3:5])|int) or (721 > (max_luminosity_weekend[0:2])|int * 60 + (max_luminosity_weekend[3:5])|int and 721 > (now().hour * 60) + now().minute and (now().hour * 60) + now().minute < (max_luminosity_weekend[0:2])|int * 60 + (max_luminosity_weekend[3:5])|int) }}
alias: Limit is between 12:01 and 24:00, actual time between 12:01 and 00:00, and actual time before that Limit OR Limit is between 00:00 and 12:00, actual time between 00:00 and 12:00, and actual time before that limit
then:
- service: input_datetime.set_datetime
target:
entity_id: !input reset_helper
alias: Set auto OFF at max time
data_template:
datetime: >-
{{ (now()|as_timestamp|timestamp_custom('%Y-%m-%d') + ' ' + max_luminosity_weekend) }}
- if:
- condition: template
value_template: >-
{{ light_percent > 0 }}
then:
- service: light.turn_on
data_template:
entity_id: !input light_entity
brightness_pct: "{{ light_percent }}"
alias: Turn ON the light with brightness
else:
- service: light.turn_on
data: {}
target:
entity_id: !input light_entity
else:
- if:
- condition: template
value_template: >-
{{ 721 > (max_luminosity_weekend[0:2])|int * 60 + (max_luminosity_weekend[3:5])|int and 720 < (now().hour * 60) + now().minute and (now().hour * 60) + now().minute > (max_luminosity_weekend[0:2])|int * 60 + (max_luminosity_weekend[3:5])|int }}
alias: Limit is between 00:00 and 12:00, actual time between 12:01 and 00:00, and actual time above that limit
then:
- service: input_datetime.set_datetime
target:
entity_id: !input reset_helper
alias: Set auto OFF at max time
data_template:
datetime: >-
{{ (((now()|as_timestamp)|int + 86400)|timestamp_custom('%Y-%m-%d') + ' ' + max_luminosity_weekend) }}
- if:
- condition: template
value_template: >-
{{ light_percent > 0 }}
then:
- service: light.turn_on
data_template:
entity_id: !input light_entity
brightness_pct: "{{ light_percent }}"
alias: Turn ON the light with brightness
else:
- service: light.turn_on
data: {}
target:
entity_id: !input light_entity
- conditions:
- condition: template
value_template: >-
{{ automatic_luminosity }}
- condition: state
entity_id: !input light_entity
alias: Light is presently ON
state: "on"
- condition: not
conditions:
- condition: state
entity_id: !input reset_helper
attribute: timestamp
state: 1
alias: Auto OFF not 1
sequence:
- if:
- condition: template
value_template: >-
{{ (now().hour > 12 and now().hour < 24 and now().weekday() in (0,1,2,3,6)) or (now().hour < 12 and now().hour > 0 and now().weekday() in (1,2,3,4,5)) }}
alias: Weekday
then:
- if:
- condition: template
value_template: >-
{{ (720 < (max_luminosity_weekday[0:2])|int * 60 + (max_luminosity_weekday[3:5])|int and 720 < (now().hour * 60) + now().minute and (now().hour * 60) + now().minute < (max_luminosity_weekday[0:2])|int * 60 + (max_luminosity_weekday[3:5])|int) or (721 > (max_luminosity_weekday[0:2])|int * 60 + (max_luminosity_weekday[3:5])|int and 721 > (now().hour * 60) + now().minute and (now().hour * 60) + now().minute < (max_luminosity_weekday[0:2])|int * 60 + (max_luminosity_weekday[3:5])|int) }}
alias: Limit is between 12:01 and 24:00, actual time between 12:01 and 00:00, and actual time before that Limit OR Limit is between 00:00 and 12:00, actual time between 00:00 and 12:00, and actual time before that limit
- condition: template
value_template: >-
{{ states(reset_helper)|as_timestamp < (now()|as_timestamp|timestamp_custom('%Y-%m-%d') + ' ' + max_luminosity_weekday)|as_timestamp }}
alias: Check next auto OFF time
then:
- service: input_datetime.set_datetime
target:
entity_id: !input reset_helper
alias: Set auto OFF at max time
data_template:
datetime: >-
{{ (now()|as_timestamp|timestamp_custom('%Y-%m-%d') + ' ' + max_luminosity_weekday) }}
else:
- if:
- condition: template
value_template: >-
{{ 721 > (max_luminosity_weekday[0:2])|int * 60 + (max_luminosity_weekday[3:5])|int and 720 < (now().hour * 60) + now().minute and (now().hour * 60) + now().minute > (max_luminosity_weekday[0:2])|int * 60 + (max_luminosity_weekday[3:5])|int }}
alias: Limit is between 00:00 and 12:00, actual time between 12:01 and 00:00, and actual time above that limit
- condition: template
value_template: >-
{{ states(reset_helper)|as_timestamp < (((now()|as_timestamp)|int + 86400) |timestamp_custom('%Y-%m-%d') + ' ' + max_luminosity_weekday) }}
alias: Check next auto OFF time
then:
- service: input_datetime.set_datetime
target:
entity_id: !input reset_helper
alias: Set auto OFF at max time
data_template:
datetime: >-
{{ (((now()|as_timestamp)|int + 86400)|timestamp_custom('%Y-%m-%d') + ' ' + max_luminosity_weekday) }}
alias: Increase auto OFF time if not already set for longer period
else:
- if:
- condition: template
value_template: >-
{{ (720 < (max_luminosity_weekend[0:2])|int * 60 + (max_luminosity_weekend[3:5])|int and 720 < (now().hour * 60) + now().minute and (now().hour * 60) + now().minute < (max_luminosity_weekend[0:2])|int * 60 + (max_luminosity_weekend[3:5])|int) or (721 > (max_luminosity_weekend[0:2])|int * 60 + (max_luminosity_weekend[3:5])|int and 721 > (now().hour * 60) + now().minute and (now().hour * 60) + now().minute < (max_luminosity_weekend[0:2])|int * 60 + (max_luminosity_weekend[3:5])|int) }}
alias: Limit is between 12:01 and 24:00, actual time between 12:01 and 00:00, and actual time before that Limit OR Limit is between 00:00 and 12:00, actual time between 00:00 and 12:00, and actual time before that limit
- condition: template
value_template: >-
{{ states(reset_helper)|as_timestamp < (now()|as_timestamp|timestamp_custom('%Y-%m-%d') + ' ' + max_luminosity_weekend)|as_timestamp }}
alias: Check next auto OFF time
then:
- service: input_datetime.set_datetime
target:
entity_id: !input reset_helper
alias: Set auto OFF at max time
data_template:
datetime: >-
{{ (now()|as_timestamp|timestamp_custom('%Y-%m-%d') + ' ' + max_luminosity_weekend) }}
else:
- if:
- condition: template
value_template: >-
{{ 721 > (max_luminosity_weekend[0:2])|int * 60 + (max_luminosity_weekend[3:5])|int and 720 < (now().hour * 60) + now().minute and (now().hour * 60) + now().minute > (max_luminosity_weekend[0:2])|int * 60 + (max_luminosity_weekend[3:5])|int }}
alias: Limit is between 00:00 and 12:00, actual time between 12:01 and 00:00, and actual time above that limit
- condition: template
value_template: >-
{{ states(reset_helper)|as_timestamp < (((now()|as_timestamp)|int + 86400) |timestamp_custom('%Y-%m-%d') + ' ' + max_luminosity_weekend) }}
alias: Check next auto OFF time
then:
- service: input_datetime.set_datetime
target:
entity_id: !input reset_helper
alias: Set auto OFF at max time
data_template:
datetime: >-
{{ (((now()|as_timestamp)|int + 86400)|timestamp_custom('%Y-%m-%d') + ' ' + max_luminosity_weekend) }}
alias: Increase auto OFF time if not already set for longer period
- conditions:
- condition: trigger
id:
- detector
- condition: state
entity_id: !input luminosity_entity
state: "on"
alias: If luminosity is low enough
sequence:
- choose:
- conditions:
- condition: state
entity_id: !input light_entity
state: "off"
alias: Light is presently OFF
sequence:
- service: input_datetime.set_datetime
target:
entity_id: !input reset_helper
data_template:
timestamp: "{{ (now()|as_timestamp + detector_time) }}"
alias: Set auto OFF in x minutes
- if:
- condition: template
value_template: >-
{{ light_percent > 0 }}
then:
- service: light.turn_on
data_template:
entity_id: !input light_entity
brightness_pct: "{{ light_percent }}"
alias: Turn ON the light with brightness
else:
- service: light.turn_on
data: {}
target:
entity_id: !input light_entity
- conditions:
- condition: state
entity_id: !input light_entity
alias: Light is presently ON
state: "on"
- condition: not
conditions:
- condition: state
entity_id: !input reset_helper
attribute: timestamp
state: 1
alias: Auto OFF not 1
sequence:
- if:
- condition: template
value_template: >-
{{ states(reset_helper)|as_timestamp < (now()|as_timestamp + detector_time) }}
alias: Check next auto OFF time
then:
- service: input_datetime.set_datetime
target:
entity_id: !input reset_helper
data_template:
timestamp: "{{ (now()|as_timestamp + detector_time) }}"
alias: Set auto OFF in x minutes
alias: Increase auto OFF time if not already set for longer period
- conditions:
- condition: trigger
id:
- location
- condition: state
entity_id: !input luminosity_entity
state: "on"
alias: If luminosity is low enough
- condition: template
value_template: "{{ not not location_entity }}"
alias: Persons or trackers must have been selected since zone is always defined
sequence:
- choose:
- conditions:
- condition: state
entity_id: !input light_entity
state: "off"
alias: Light is presently OFF
sequence:
- service: input_datetime.set_datetime
target:
entity_id: !input reset_helper
data_template:
timestamp: "{{ (now()|as_timestamp + location_time) }}"
alias: Set auto OFF in x minutes
- if:
- condition: template
value_template: >-
{{ light_percent > 0 }}
then:
- service: light.turn_on
data_template:
entity_id: !input light_entity
brightness_pct: "{{ light_percent }}"
alias: Turn ON the light with brightness
else:
- service: light.turn_on
data: {}
target:
entity_id: !input light_entity
- conditions:
- condition: state
entity_id: !input light_entity
alias: Light is presently ON
state: "on"
- condition: not
conditions:
- condition: state
entity_id: !input reset_helper
attribute: timestamp
state: 1
alias: Auto OFF not 1
sequence:
- if:
- condition: template
value_template: >-
{{ states(reset_helper)|as_timestamp < (now()|as_timestamp + location_time) }}
alias: Check next auto OFF time
then:
- service: input_datetime.set_datetime
target:
entity_id: !input reset_helper
data_template:
timestamp: "{{ (now()|as_timestamp + location_time) }}"
alias: Set auto OFF in x minutes
alias: Increase auto OFF time if not already set for longer period
- conditions:
- condition: trigger
id:
- auto-off
- condition: not
conditions:
- condition: state
entity_id: !input reset_helper
attribute: timestamp
state: 1
alias: Auto OFF not 1
sequence:
- service: light.turn_off
data: {}
target:
entity_id: !input light_entity
alias: Turn light OFF
- service: input_datetime.set_datetime
data:
timestamp: 1
alias: Reset auto OFF
target:
entity_id: !input reset_helper
- conditions:
- condition: trigger
id:
- reload
sequence:
- choose:
- conditions:
- condition: template
value_template: >-
{{ automatic_luminosity }}
- condition: state
entity_id: !input luminosity_entity
state: "on"
alias: Luminosity is below threshold (NIGHT)
- condition: state
entity_id: !input light_entity
state: "off"
sequence:
- if:
- condition: template
value_template: >-
{{ (now().hour > 12 and now().hour < 24 and now().weekday() in (0,1,2,3,6)) or (now().hour < 12 and now().hour > 0 and now().weekday() in (1,2,3,4,5)) }}
alias: Weekday
then:
- if:
- condition: template
value_template: >-
{{ (720 < (max_luminosity_weekday[0:2])|int * 60 + (max_luminosity_weekday[3:5])|int and 720 < (now().hour * 60) + now().minute and (now().hour * 60) + now().minute < (max_luminosity_weekday[0:2])|int * 60 + (max_luminosity_weekday[3:5])|int) or (721 > (max_luminosity_weekday[0:2])|int * 60 + (max_luminosity_weekday[3:5])|int and 721 > (now().hour * 60) + now().minute and (now().hour * 60) + now().minute < (max_luminosity_weekday[0:2])|int * 60 + (max_luminosity_weekday[3:5])|int) }}
alias: Limit is between 12:01 and 24:00, actual time between 12:01 and 00:00, and actual time before that Limit OR Limit is between 00:00 and 12:00, actual time between 00:00 and 12:00, and actual time before that limit
then:
- service: input_datetime.set_datetime
target:
entity_id: !input reset_helper
alias: Set auto OFF at max time
data_template:
datetime: >-
{{ (now()|as_timestamp|timestamp_custom('%Y-%m-%d') + ' ' + max_luminosity_weekday) }}
- if:
- condition: template
value_template: >-
{{ light_percent > 0 }}
then:
- service: light.turn_on
data_template:
entity_id: !input light_entity
brightness_pct: "{{ light_percent }}"
alias: Turn ON the light with brightness
else:
- service: light.turn_on
data: {}
target:
entity_id: !input light_entity
else:
- if:
- condition: template
value_template: >-
{{ 721 > (max_luminosity_weekday[0:2])|int * 60 + (max_luminosity_weekday[3:5])|int and 720 < (now().hour * 60) + now().minute and (now().hour * 60) + now().minute > (max_luminosity_weekday[0:2])|int * 60 + (max_luminosity_weekday[3:5])|int }}
alias: Limit is between 00:00 and 12:00, actual time between 12:01 and 00:00, and actual time above that limit
then:
- service: input_datetime.set_datetime
target:
entity_id: !input reset_helper
alias: Set auto OFF at max time
data_template:
datetime: >-
{{ (((now()|as_timestamp)|int + 86400)|timestamp_custom('%Y-%m-%d') + ' ' + max_luminosity_weekday) }}
- if:
- condition: template
value_template: >-
{{ light_percent > 0 }}
then:
- service: light.turn_on
data_template:
entity_id: !input light_entity
brightness_pct: "{{ light_percent }}"
alias: Turn ON the light with brightness
else:
- service: light.turn_on
data: {}
target:
entity_id: !input light_entity
alias: Check if we are still before the time limit
else :
- if:
- condition: template
value_template: >-
{{ (720 < (max_luminosity_weekend[0:2])|int * 60 + (max_luminosity_weekend[3:5])|int and 720 < (now().hour * 60) + now().minute and (now().hour * 60) + now().minute < (max_luminosity_weekend[0:2])|int * 60 + (max_luminosity_weekend[3:5])|int) or (721 > (max_luminosity_weekend[0:2])|int * 60 + (max_luminosity_weekend[3:5])|int and 721 > (now().hour * 60) + now().minute and (now().hour * 60) + now().minute < (max_luminosity_weekend[0:2])|int * 60 + (max_luminosity_weekend[3:5])|int) }}
alias: Limit is between 12:01 and 24:00, actual time between 12:01 and 00:00, and actual time before that Limit OR Limit is between 00:00 and 12:00, actual time between 00:00 and 12:00, and actual time before that limit
then:
- service: input_datetime.set_datetime
target:
entity_id: !input reset_helper
alias: Set auto OFF at max time
data_template:
datetime: >-
{{ (now()|as_timestamp|timestamp_custom('%Y-%m-%d') + ' ' + max_luminosity_weekend) }}
- if:
- condition: template
value_template: >-
{{ light_percent > 0 }}
then:
- service: light.turn_on
data_template:
entity_id: !input light_entity
brightness_pct: "{{ light_percent }}"
alias: Turn ON the light with brightness
else:
- service: light.turn_on
data: {}
target:
entity_id: !input light_entity
else:
- if:
- condition: template
value_template: >-
{{ 721 > (max_luminosity_weekend[0:2])|int * 60 + (max_luminosity_weekend[3:5])|int and 720 < (now().hour * 60) + now().minute and (now().hour * 60) + now().minute > (max_luminosity_weekend[0:2])|int * 60 + (max_luminosity_weekend[3:5])|int }}
alias: Limit is between 00:00 and 12:00, actual time between 12:01 and 00:00, and actual time above that limit
then:
- service: input_datetime.set_datetime
target:
entity_id: !input reset_helper
alias: Set auto OFF at max time
data_template:
datetime: >-
{{ (((now()|as_timestamp)|int + 86400)|timestamp_custom('%Y-%m-%d') + ' ' + max_luminosity_weekend) }}
- if:
- condition: template
value_template: >-
{{ light_percent > 0 }}
then:
- service: light.turn_on
data_template:
entity_id: !input light_entity
brightness_pct: "{{ light_percent }}"
alias: Turn ON the light with brightness
else:
- service: light.turn_on
data: {}
target:
entity_id: !input light_entity
- conditions:
- condition: state
entity_id: !input light_entity
state: "off"
alias: Reset auto OFF timer if light is OFF
- condition: not
conditions:
- condition: state
entity_id: !input reset_helper
attribute: timestamp
state: "1"
alias: Auto OFF is not reset
sequence:
- service: input_datetime.set_datetime
data:
timestamp: 1
alias: Reset auto OFF
target:
entity_id: !input reset_helper
- conditions:
- condition: state
entity_id: !input light_entity
state: "on"
alias: Reset auto OFF timer if light is ON and timer in the past
- condition: not
conditions:
- condition: state
entity_id: !input reset_helper
attribute: timestamp
state: "1"
alias: Auto OFF is not reset
- condition: template
value_template: >-
{{ states(reset_helper)|as_timestamp < now()|as_timestamp }}
sequence:
- service: input_datetime.set_datetime
data:
timestamp: 1
alias: Reset auto OFF
target:
entity_id: !input reset_helper
@Dkspeedy
Copy link

Dkspeedy commented Jan 5, 2024

Thanks a lot for this blueprint. It works really good. Only thing i wonder,if it is possible to add another period of lights ON. In addition to evening light i like the lights on from some time In the morning until sunrise. I tried modifying the automation, But it quickly became clear, that i dont have the skills to get it working 😉

@kalhimeo
Copy link
Author

kalhimeo commented Jan 7, 2024

Hi Dkspeedy, I will keep your suggestion in mind but unfortunately I don't have enough time to implement it for the moment :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment