Trigger closing shades via time and light

I want to close my shades at a given time or when it becomes dark, whatever comes first.
What’s the best practice to achieve this?
I came up with the following.
Is there a better or more elegant way?
is the action triggered each time the sensor gets another value of zero during the night?

I’m sorry, if this has been answered elsewhere, but my search was not successful.

blueprint:
    name: Cover Down
    description: Close cover at a specific time or if it is dark
    domain: automation
    input:
        time_spec:
            name: Time
            description: Latest time at which the cover should close
            selector:
               time:
        cover_entity:
            name: Cover
            description: The cover to close
            selector:
                entity:
                   domain: cover

trigger:
  - platform: time
    at: !input time_spec
  - platform: numeric_state
    entity_id: sensor.lux_aussen
    below: '1'
condition:
  - condition: or
    conditions:
      - condition: time
        after: !input time_spec
      - condition: numeric_state
        entity_id: sensor.lux_aussen
        below: '1'
action:
  - service: cover.close_cover
    data: {}
    entity_id: !input cover_entity
mode: single

Replying to myself in case somebody has a similar task.

I changed the blueprint a bit:

  • use of a binary trigger for daylight so that the trigger algorithm might be changed from a light sensor to for example calculated sunrise and sunset without changing the cover automation
  • two open times, one for workdays, another for holidays
  • only one automation for opening and closing the covers for an easier automation management

The number of if-clauses in the action section could be reduced by using braces “and”. I guess this is a matter of taste without any impact on anything.


```blueprint:
    name: Cover
    description: Open and close cover dependend on time and darkness
    domain: automation
    input:
        cover_entity:
            name: Cover
            description: The cover to close
            selector:
                entity:
                   domain: cover
        timeOpenWork:
            name: TimeOpenWorkday
            description: Earliest time at which the cover should open on workdays
            selector:
               time:
        timeOpenWeekend:
            name: TimeOpenWeekend
            description: Earliest time at which the cover should open on weekends
            selector:
               time:
        timeClose:
            name: TimeClose
            description: Latest time at which the cover should close
            selector:
               time:

trigger:
  - platform: time
    at: !input timeOpenWork
  - platform: time
    at: !input timeOpenWeekend
  - platform: time
    at: !input timeClose
  - platform: state
    entity_id: binary_sensor.daylight

variables:
  timeCloseVar: !input timeClose
  timeOpenWeekendVar: !input timeOpenWeekend
  timeOpenWorkVar: !input timeOpenWork

action:
    service: >
      {% set curTime = now().strftime( '%T' ) %}
      {% if curTime >= timeCloseVar %}
        cover.close_cover
      {% elif is_state('binary_sensor.workdays', 'on') and curTime < timeOpenWorkVar %}
        cover.close_cover
      {% elif is_state('binary_sensor.workdays', 'off') and curTime < timeOpenWeekendVar %}
        cover.close_cover
      {% elif is_state('binary_sensor.daylight', 'on') %}
        cover.open_cover
      {% else %}
        cover.close_cover
      {% endif %}
    data: {}
    entity_id: !input cover_entity
mode: single