Automation: condition between sensor

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:
Alba_tramonto

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)

Card

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

You are triggering from the state of your input_select only, so once in automatic mode, it won’t get triggered again. I think you need to trigger on:

  • changes to the mode
  • time of “optimised sunrise”
  • time of “optimised sunset”

With sensor.time installed (docs):

  trigger:
    - platform: state
      entity_id: input_select.modalita_luce_dietro
    - platform: template
      value_template: "{{ states('sensor.time') == states('sensor.prossima_alba_calcolata') }}"
    - platform: template
      value_template: "{{ states('sensor.time') == states('sensor.prossima_tramonto_calcolata') }}"

and then build logic into your automatic action condition (docs) to determine what action to take based on the time. Like this:

        - conditions:
            - condition: state
              entity_id: input_select.modalita_luce_dietro
              state: "automatico"
          sequence:
            - service: >-
                {% if states('sensor.prossima_alba_calcolata') <= states('sensor.time') < states('sensor.prossima_tramonto_calcolata') %}
                  switch.turn_off
                {% else %}
                  switch.turn_on
                {% endif %}
              entity_id: switch.luce_porta_dietro

So if the mode is changed to automatic, or if it is one of your optimised times, the sequence will run, turning the light off if it is between sunrise and sunset, or on if it is not. Note I test for being between sunrise and sunset, as the simple test I use won’t work across midnight.

1 Like

FYI, if you don’t the hour to be zero-padded, simply do this (instead of using replace(" 0", "") in the template):

{{ as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom('%-H:%M') }}

The hyphen in %-H means you want the hour without zero-padding.

Fantastic, it works perfectly. Thank you very much

Thank you for your suggestion