Control cover in the morning and evening

The automation below does trigger in the morning but not in the evening. What can be causing this?

alias: Open or Close Blinds
description: “”
trigger:

  • platform: template
    value_template: |-
    {% set below_horizon = is_state(‘sun.sun’, ‘below_horizon’) %} >
    {% set after_dawn = now().strftime(’%H:%M’) >= ‘18:00’ %} >
    {% set above_horizon = is_state(‘sun.sun’, ‘above_horizon’) %} >
    {% set after_sunrise = now().strftime(’%H:%M’) >= ‘07:00’ %} >
    {% if below_horizon %}
    {{ after_dawn }}
    {% elif above_horizon %}
    {{ after_sunrise }}
    {% endif %}
    condition:
  • condition: or
    conditions:
    • condition: sun
      after: sunrise
      after_offset: “+00:00”
    • condition: sun
      before: sunset
      after_offset: “+00:00”
      action:
  • if:
    • condition: template
      value_template: is_state(‘sun.sun’, ‘above_horizon’)
      then:
    • device_id: e777508491d4b745f06ccea9598f4424
      domain: cover
      entity_id: cover.screen_all
      type: open
      else:
    • device_id: e777508491d4b745f06ccea9598f4424
      domain: cover
      entity_id: cover.screen_all
      type: close
      mode: single

Your trigger and conditions are confusing.

Please clarify what your goal for the logic of this automation is.

The goal is to open the covers at sunrise but not before 07:00 and to close the covers at sunset but not before 18:00

trigger:
  - platform: time
    at: "07:00:00"
    id: open
  - platform: sun
    event: sunrise
    id: open
  - platform: time
    at: "18:00:00"
    id: close
  - platform: sun
    event: sunset
    id: close
condition:
  - condition: template
    value_template: >
      {% if trigger.id == 'open' %}
         {{ today_at('07:00') < now()
         and is_state('sun.sun', 'above_horizon')
         and is_state('cover.screen_all', 'closed') }}
      {% else %}
         {{ today_at('18:00') < now() 
         and is_state('sun.sun', 'below_horizon') 
         and is_state('cover.screen_all', 'open') }}
      {% endif %}
action:
  - service: cover.{{ trigger.id }}_cover
    target:
      entity_id: cover.screen_all

EDIT: Added sun position to template and fixed a couple “fancy” quote marks

1 Like

Thank you! Will try this one out tonight

Works almost :wink: Cover closed at 18:00, but sunset is at 20:39. Covers should close at 20:39. However in winter time sunset is before 18:00. In that case covers should close not earlier than 18:00

I should have included the state of the sun sensor in that part of the template so the action execute on the later of the two triggers. I have added it in the original post above.

Works great! thank you for your support.

1 Like