Hello everybody.
In addition to the two sensors for sunrise and sunset, I have configured other 2 sensors always starting from sunrise and sunset and I have added the minutes set in a variable to optimize the time of switching on and off of a light.
sensors:
prossima_alba:
entity_id: sun.sun
friendly_name: 'Prossima alba'
value_template: >
{{ as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom(' %H:%M') | replace(" 0", "") }}
icon_template: mdi:weather-sunset-up
prossimo_tramonto:
entity_id: sun.sun
friendly_name: 'Prossimo tramonto'
value_template: >
{{ as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom(' %H:%M') | replace(" 0", "") }}
icon_template: mdi:weather-sunset-down
prossima_alba_calcolata:
entity_id: sun.sun
friendly_name: 'Prossima alba calcolata'
value_template: >
{% set correzione_alba = (states('input_number.minuti_correzione_alba')) | int * 60 %}
{% set alba = as_timestamp(state_attr('sun.sun','next_rising')) %}
{{ (alba + correzione_alba) | timestamp_custom('%H:%M') }}
icon_template: mdi:weather-sunset-up
prossimo_tramonto_calcolato:
entity_id: sun.sun
friendly_name: 'Prossimo tramonto calcolato'
value_template: >
{% set correzione_tramonto = (states('input_number.minuti_correzione_tramonto')) | int * 60 %}
{% set tramonto = as_timestamp(state_attr('sun.sun','next_setting')) %}
{{ (tramonto + correzione_tramonto) | timestamp_custom('%H:%M') }}
icon_template: mdi:weather-sunset-down
This is the result in a lovelace card:
Later I created a card to select the ignition mode: on, automatic, off
type: custom:mod-card
style: |
ha-card {
border: 1px solid black;
background: white;
}
card:
type: vertical-stack
cards:
- type: custom:button-card
custom_fields:
name_luce_dietro: Luce dietro
icon_luce_dietro: |
<ha-icon
icon="mdi:lightbulb"
style="width: 30px; height: 30px;">
</ha-icon>
styles:
grid:
- grid-template-columns: 1fr 30%
- grid-template-rows: 1fr
- grid-template-areas: '"name_luce_dietro icon_luce_dietro"'
custom_fields:
name_luce_dietro:
- font-size: 20px
icon_luce_dietro:
- color: |-
[[[
if (states['switch.luce_porta_dietro'].state == "on")
return "yellow";
return "black";
]]]
- type: horizontal-stack
cards:
- type: custom:button-card
entity: input_select.modalita_luce_dietro
icon: mdi:lightbulb-on-outline
name: sempre_acceso
tap_action:
action: call-service
service: input_select.select_option
service_data:
entity_id: input_select.modalita_luce_dietro
option: sempre_acceso
show_state: false
show_name: false
state:
- value: sempre_acceso
color: rgb(5, 147, 255)
- type: custom:button-card
entity: input_select.modalita_luce_dietro
icon: mdi:calendar-clock
name: automatico
tap_action:
action: call-service
service: input_select.select_option
service_data:
entity_id: input_select.modalita_luce_dietro
option: automatico
show_state: false
show_name: false
state:
- value: automatico
color: rgb(5, 147, 255)
- type: custom:button-card
entity: input_select.modalita_luce_dietro
icon: mdi:lightbulb-off-outline
name: sempre_spento
tap_action:
action: call-service
service: input_select.select_option
service_data:
entity_id: input_select.modalita_luce_dietro
option: sempre_spento
show_state: false
show_name: false
state:
- value: sempre_spento
color: rgb(5, 147, 255)
Finally I created an automation to manage the three states and when I select the automatic mode, I would like the light to switch on between sunset and sunrise (the ones I have optimized), but I don’t know how to do it. You can help me?
This is the automation where I should correct the “after” and “before” in the “automatico” state
- alias: modalita_accensione_luce_dietro
trigger:
platform: state
entity_id: input_select.modalita_luce_dietro
action:
- choose:
- conditions:
- condition: state
entity_id: input_select.modalita_luce_dietro
state: "sempre_acceso"
sequence:
- service: switch.turn_on
entity_id: switch.luce_porta_dietro
- service: automation.turn_off
entity_id: automation.accensione_automatica_luce_porta_dietro
- conditions:
- condition: state
entity_id: input_select.modalita_luce_dietro
state: "sempre_spento"
sequence:
- service: switch.turn_off
entity_id: switch.luce_porta_dietro
- service: automation.turn_off
entity_id: automation.accensione_automatica_luce_porta_dietro
- conditions:
- condition: state
entity_id: input_select.modalita_luce_dietro
state: "automatico"
- condition: time
after: {{ sensor.prossimo_tramonto_calcolato }}
before: {{ sensor.prossima_alba_calcolata }}
sequence:
- service: switch.turn_on
entity_id: switch.luce_porta_dietro