I used daenny’s Sun Event timers as an inspiration to create the following automatic dimming light timer:
I utilized some dummy sensors to calculate Unix time for simpler logic. I also used a dummy group to reduce the number of automations required. It is all set up as a package
sensor:
- platform: template
sensors:
auto_outdoor_light_start:
friendly_name: 'Lights on at '
value_template: '{{ (as_timestamp(states.sun.sun.attributes.next_setting) + (states("input_slider.auto_outdoor_light_offset_on") | int) * 60) | timestamp_custom("%I:%M %p") }}'
auto_outdoor_light_dim:
friendly_name: 'Lights dim at '
value_template: '{{ (as_timestamp(states.sun.sun.attributes.next_midnight) + (states("input_slider.auto_outdoor_light_offset_dim") | int) * 60) | timestamp_custom("%I:%M %p") }}'
auto_outdoor_light_stop:
friendly_name: 'Lights off at '
value_template: '{{ (as_timestamp(states.sun.sun.attributes.next_rising) - (states("input_slider.auto_outdoor_light_offset_off") | int) * 60) | timestamp_custom("%I:%M %p") }}'
# The following sensors are used to control the timing logic
outdoor_light_on_unix_time:
friendly_name: 'Unix Time On'
value_template: '{{(as_timestamp(states.sun.sun.attributes.next_rising) + (states("input_slider.auto_outdoor_light_offset_on") | int) * 60)}}'
outdoor_light_dim_unix_time:
friendly_name: 'Unix Time Dim'
value_template: '{{(as_timestamp(states.sun.sun.attributes.next_rising) + (states("input_slider.auto_outdoor_light_offset_dim") | int) * 60)}}'
outdoor_light_off_unix_time:
friendly_name: 'Unix Time Off'
value_template: '{{(as_timestamp(states.sun.sun.attributes.next_rising) - (states("input_slider.auto_outdoor_light_offset_off") | int) * 60)}}'
- platform: time_date
display_options:
- 'time'
input_slider:
auto_outdoor_light_offset_on:
name: Minutes after sunset
icon: mdi:timer
initial: 0
min: -60
max: 60
step: 10
auto_outdoor_light_on_level:
name: Auto on brightness
icon: mdi:brightness-6
initial: 80
min: 0
max: 100
step: 5
auto_outdoor_light_offset_dim:
name: Minutes after midnight
icon: mdi:timer
initial: 30
min: 0
max: 120
step: 10
auto_outdoor_light_dim_level:
name: Auto dim brightness
icon: mdi:brightness-6
initial: 20
min: 0
max: 100
step: 5
auto_outdoor_light_offset_off:
name: Minutes before sunrise
icon: mdi:timer
initial: 60
min: 0
max: 240
step: 10
auto_outdoor_light_on_time:
name: Minutes before sunrise
icon: mdi:timer
initial: 0
min: 0
max: 2000000000
step: 10
auto_outdoor_light_dim_time:
name: Minutes before sunrise
icon: mdi:timer
initial: 0
min: 0
max: 2000000000
step: 10
auto_outdoor_light_off_time:
name: Minutes before sunrise
icon: mdi:timer
initial: 0
min: 0
max: 2000000000
step: 10
automation:
- alias: 'auto_outdoor_light_time_setting'
initial_state: True
trigger:
- platform: sun
event: sunrise
- platform: homeassistant
event: start
action:
- service: input_slider.select_value
entity_id: input_slider.auto_outdoor_light_on_time
data_template:
value: '{{states.sensor.outdoor_light_on_unix_time.state|int}}'
- service: input_slider.select_value
entity_id: input_slider.auto_outdoor_light_dim_time
data_template:
value: '{{states.sensor.outdoor_light_dim_unix_time.state|int}}'
- service: input_slider.select_value
entity_id: input_slider.auto_outdoor_light_off_time
data_template:
value: '{{states.sensor.outdoor_light_off_unix_time.state|int}}'
- alias: 'auto_outdoor_light'
initial_state: True
trigger:
- platform: time
minutes: '/5'
seconds: 00
condition:
condition: or
conditions:
- condition: state
entity_id: 'sun.sun'
state: 'below_horizon'
action:
- service: light.turn_off
data_template:
entity_id: >
{%if (as_timestamp(now()))|int>=(states.input_slider.auto_outdoor_light_off_time.state|int)%}
group.automatic_lights_outside
{%elif (as_timestamp(now()))|int>=(states.input_slider.auto_outdoor_light_dim_time.state|int)%}
group.automatic_lights_outside_dummy
{%else%}
group.automatic_lights_outside_dummy
{%endif%}
- service: light.turn_on
data_template:
entity_id: >
{%if (as_timestamp(now()))|int>=(states.input_slider.auto_outdoor_light_off_time.state|int)%}
group.automatic_lights_outside_dummy
{%elif (as_timestamp(now()))|int>=(states.input_slider.auto_outdoor_light_dim_time.state|int)%}
group.automatic_lights_outside
{%else%}
group.automatic_lights_outside
{%endif%}
brightness_pct: >
{%if (as_timestamp(now()))|int>=(states.input_slider.auto_outdoor_light_off_time.state|int)%}
0
{%elif (as_timestamp(now()))|int>=(states.input_slider.auto_outdoor_light_dim_time.state|int)%}
{{states.input_slider.auto_outdoor_light_dim_level.state|int}}
{%else%}
{{states.input_slider.auto_outdoor_light_on_level.state|int}}
{%endif%}
group:
# This first group is used as a dummy group in the service call
Automatic Lights Outside Dummy:
name: AutomatiC Lights Outside Dummy
Automatic Lights Outside:
name: Automatic Lights Outside
entities:
- light.front_walkway_level
Outdoor Timer Light:
name: Outdoor Lights Timer Settings
control: hidden
entities:
- automation.auto_outdoor_light
- input_slider.auto_outdoor_light_on_level
- input_slider.auto_outdoor_light_offset_on
- sensor.auto_outdoor_light_start
- input_slider.auto_outdoor_light_dim_level
- input_slider.auto_outdoor_light_offset_dim
- sensor.auto_outdoor_light_dim
- input_slider.auto_outdoor_light_offset_off
- sensor.auto_outdoor_light_stop