Conditional setting for sunset

Im having this automation that works perfect during spring and summer.
But now its getting darker earlier and i want to have the lights fired up earlier as well.

- alias: 'Fönster efter skymning'
  initial_state: true
  trigger:
  - platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    below: 2.5
  condition:
  - condition: state
    entity_id: group.all_devices
    state: home
  - condition: state
    entity_id: input_select.status_hemma
    state: 'Home'
  action:
  - service: script.turn_on
    entity_id: 
      - script.skymning

So i would like to get help to have this sensor
{{states.sensor.seasons.state}}
Identifying Spring and Summer to trigger with this condition

  - platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    below: 2.5

But when it is Autumn or Winter i want this trigger

  - platform: sun
    event: sunset
    offset: "-0:20:00"

Don’t get too complicated, Make two automations, and make one conditional on it being spring/summer and the other conditional on being autumn/winter.

I assume you have seen this sensor https://www.home-assistant.io/components/sensor.season/

1 Like

So this will do the trick?

- alias: 'Fönster efter skymning'
  initial_state: true
  trigger:
  - platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    below: 2.5
  condition:
  - condition: state
    entity_id: group.all_devices
    state: home
  - condition: state
    entity_id: input_select.status_hemma
    state: 'Home'
  - condition: state
    value_template: "{{states.sensor.seasons.state != 'Spring, Summer' }}"
  action:
  - service: script.turn_on
    entity_id: 
      - script.skymning
      
- alias: 'Fönster efter skymning'
  initial_state: true
  trigger:
  - platform: sun
    event: sunset
    offset: "-0:20:00"
  condition:
  - condition: state
    entity_id: group.all_devices
    state: home
  - condition: state
    entity_id: input_select.status_hemma
    state: 'Home'
  - condition: state
  - condition: state
    value_template: "{{states.sensor.seasons.state != 'Autumn, Winter' }}"
  action:
  - service: script.turn_on
    entity_id: 
      - script.skymning

I was a bit fast on copy/paste…
This is a code that doesnt get any errors at least

- alias: 'Window at evening spring'
  initial_state: true
  trigger:
  - platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    below: 2.5
  condition:
  - condition: state
    entity_id: group.all_devices
    state: home
  - condition: state
    entity_id: input_select.status_hemma
    state: 'Home'
  - condition: template
    value_template: "{{ states.sensor.season.state in ['spring','summer'] }}"
  action:
  - service: script.turn_on
    entity_id: 
      - script.skymning

- alias: 'Window at evening Winter'
  initial_state: true
  trigger:
  - platform: sun
    event: sunset
    offset: "-0:20:00"
  condition:
  - condition: state
    entity_id: group.all_devices
    state: home
  - condition: state
    entity_id: input_select.status_hemma
    state: 'Home'
  - condition: template
    value_template: "{{ states.sensor.season.state ['autumn','winter'] }}"
  action:
  - service: script.turn_on
    entity_id: 
      - script.skymning

It’s going to be ugly. Only a condition will work for that using the sun.sun platform. Also, assuming that the seasons are: Spring, Summer, Autumn, and Winter.

  trigger:
    - platform: state
      entity_id: sun.sun
  condition:
    - condition: template
      value_template: >
        {% if states('sensor.season') in ['Spring','Summer'] %}
        {{ trigger.to_state.attributes.elevation <= 2.5 }}
        {% else %} # Autumn, Winter
        # only move forward when we go from above to below.
        {{ trigger.from_state.state == 'above_horizon' and trigger.to_state.state == 'below_horizon' }}
        {% endif %}
    - condition: state
      entity_id: group.all_devices
      state: home
    - condition: state
      entity_id: input_select.status_hemma
      state: 'Home'
  action:
    - service: script.turn_on
      entity_id: 
        - script.skymning

but as @nickrout said, you could make 2 automations and it wouldn’t get this ugly.

1 Like

Thank you @nickrout and @petro now i have two solutions to test :slight_smile:
That will make a big plus on the WAF factor if it works :wink: