I’m back!
First things first: I’m using split configuration so everything is separated in different files and folders. Remember that when cutting/pasting the code because of the spaces. YAML is really really picky about that.
Input_number:
auto_lp_quartos_light_offset_on:
name: Minutos Acerto ( - | + )
icon: mdi:timer
min: -90
max: 90
step: 10
auto_lp_quartos_light_offset_off:
name: Minutos Acerto ( - | + )
icon: mdi:timer
min: -90
max: 90
step: 10
Sensor:
- platform: template
sensors:
auto_lp_quartos_light_group_state:
friendly_name: 'Estado actual '
icon_template: mdi:apple-keyboard-option
value_template: '{{ states.group.lp_quartos.state }}'
auto_lp_quartos_light_start:
friendly_name: 'Luzes acendem às '
icon_template: mdi:lightbulb-on-outline
value_template: '{{ (as_timestamp(states.sun.sun.attributes.next_setting) + (states("input_number.auto_lp_quartos_light_offset_on") | int) * 60) | timestamp_custom("%H:%M") }}'
auto_lp_quartos_light_stop:
friendly_name: 'Luzes apagam às '
icon_template: mdi:lightbulb-outline
value_template: '{{ (as_timestamp(states.sun.sun.attributes.next_rising) + (states("input_number.auto_lp_quartos_light_offset_off") | int) * 60) | timestamp_custom("%H:%M") }}'
auto_lp_quartos_light_minutes_start_offset:
friendly_name: 'Minutos de Acerto '
icon_template: mdi:timer-sand
value_template: '{{ states("input_number.auto_lp_quartos_light_offset_on") | int }}'
auto_lp_quartos_light_minutes_stop_offset:
friendly_name: 'Minutos de Acerto '
icon_template: mdi:timer-sand
value_template: '{{ states("input_number.auto_lp_quartos_light_offset_off") | int }}'
auto_lp_quartos_light_start_seconds:
value_template: '{{ ((as_timestamp(states.sun.sun.attributes.next_setting) + (states("input_number.auto_lp_quartos_light_offset_on") | int) * 60) | timestamp_custom("%H",0) | int * 3600 + (as_timestamp(states.sun.sun.attributes.next_setting) + (states("input_number.auto_lp_quartos_light_offset_on") | int) * 60) | timestamp_custom("%M") | int * 60) }}'
auto_lp_quartos_light_stop_seconds:
value_template: '{{ (as_timestamp(states.sun.sun.attributes.next_rising) + (states("input_number.auto_lp_quartos_light_offset_off") | int) * 60) | timestamp_custom("%H",0) | int * 3600 + (as_timestamp(states.sun.sun.attributes.next_rising) + (states("input_number.auto_lp_quartos_light_offset_off") | int) * 60) | timestamp_custom("%M") | int * 60 }}'
auto_lp_quartos_light_start_offset:
friendly_name: 'Tempo até ligar (h) '
icon_template: mdi:timelapse
value_template: '{{ (((states.sensor.auto_lp_quartos_light_start_seconds.state | int)) - ((as_timestamp (now()) | int ))) | timestamp_custom("%H:%M") }}'
entity_id:
- sensor.time
auto_lp_quartos_light_stop_offset:
friendly_name: 'Tempo até desligar (h) '
icon_template: mdi:timelapse
value_template: '{{ (((states.sensor.auto_lp_quartos_light_stop_seconds.state | int)) - ((as_timestamp (now()) | int ))) | timestamp_custom("%H:%M") }}'
entity_id:
- sensor.time
Automation:
- alias: 'Acender Automático'
initial_state: True
trigger:
- platform: time
minutes: '/1'
seconds: 01
condition:
condition: and
conditions:
- condition: template
value_template: '{{ states.sensor.auto_lp_quartos_light_start_seconds.state | int <= ((((((as_timestamp (now(""))) | timestamp_custom("%H",0)) | int) * 3600) + ((((as_timestamp (now(""))) | timestamp_custom("%M")) | int) * 60) + (((as_timestamp (now(""))) | timestamp_custom("%S")) | int))) }}'
- condition: state
entity_id: group.familia
state: 'home'
- condition: state
entity_id: 'group.lp_quartos'
state: 'off'
action:
- service: light.turn_on
data_template:
entity_id: group.lp_quartos
- service: notify.jorge_notify
data:
title: '{{ as_timestamp (now()) | timestamp_custom("%d/%b/%Y %T") }}'
message: 'Luzes de Presença dos Quartos - LIGADAS'
- alias: 'Apagar Automático'
initial_state: True
trigger:
- platform: time
minutes: '/1'
seconds: 01
condition:
condition: and
conditions:
- condition: template
value_template: '{{ states.sensor.auto_lp_quartos_light_stop_seconds.state | int <= ((((((as_timestamp (now(""))) | timestamp_custom("%H",0)) | int) * 3600) + ((((as_timestamp (now(""))) | timestamp_custom("%M")) | int) * 60) + (((as_timestamp (now(""))) | timestamp_custom("%S")) | int))) <= states.sensor.auto_lp_quartos_light_start_seconds.state | int }}'
- condition: state
entity_id: 'group.lp_quartos'
state: 'on'
action:
- service: light.turn_off
entity_id: group.lp_quartos
- service: notify.jorge_notify
data:
title: '{{ as_timestamp (now()) | timestamp_custom("%d/%b/%Y %T") }}'
message: 'Luzes de Presença dos Quartos - DESLIGADAS'
Let me try to explain all that code.
Input_number:
auto_lp_quartos_light_offset_on
- Minutes of offset to turn lights on
auto_lp_quartos_light_offset_off
- Minutes of offset to turn lights off
(positive values means “after” sunset or sunrise, negative values mean “before” sunset or sunrise)
Sensor:
auto_lp_quartos_light_start
- At what time the lights turn on (sunset time plus offset)
auto_lp_quartos_light_stop
- At what time the lights turn off (sunrise time plus offset)
auto_lp_quartos_light_minutes_start_offset
- Displays the start offset value in minutes
auto_lp_quartos_light_minutes_stop_offset
- Displays the stop offset value in minutes
auto_lp_quartos_light_start_seconds
- Converts the final turn on time to seconds
auto_lp_quartos_light_stop_seconds
- Converts the final turn off time to seconds
auto_lp_quartos_light_start_offset
- How long, in hours, until lights turn on
auto_lp_quartos_light_stop_offset
- How long, in hours, until lights turn off
(this last two sensors gave me lots of headaches! It always gave an error or gave very strange numbers. In my case it only works if i have an entity_id: sensor.time
inside the sensor… other people on the forum had the same problem and that was the solution they found too)
Automation:
Time trigger every minute and one second.
Condition checks if it is time to turn on or off the lights.
Action turns on or off the lights.
Are you still there??? Really?? Man, this is a loooooooooong post… Sorry about that. And sorry about my awful English, it’s not my first language as you might have guessed ![:blush: :blush:](https://community.home-assistant.io/images/emoji/twitter/blush.png?v=9)
If you have any question, just ask!