Best way to automate?

I have a cover that I want to open at 8:30 on weekdays and at 10:00 on weekends. Initially I set up a Time Trigger at 8:30 and a condition (Time) for weekdays. It works well. I could simply do 2 automations (one for weekdays and the other for weekends) but I would like to set up everything on a single automation.

So I set up 2 triggers, (Time 8:30 and Time 10:00) then I set 2 OR conditions, one that says Time after 8:29 on weekdays OR Time after 9:59 on weekends.

Surprise the automation doesn’t get triggered anymore… why it’s that ? what is the best way to do what I want to do ?

alias: Open covers
description: ''
trigger:
  - at: '8:30'
    platform: time
  - platform: time
    at: '10:00'
condition:
  - condition: time
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
    after: '8:29'
  - condition: or
    conditions:
      - condition: time
        after: '9:59'
        weekday:
          - sat
          - sun
action:
  - data: {}
    entity_id: cover.rho1_cover
    service: cover.open_cover
mode: single

Post the automation so we can look at it.

alias: Open covers
description: ''
trigger:
  - platform: time
    at:
      - '8:30'
      - '10:00'
condition:
  - condition: template
    value_template: >
      {% set h = now().hour %}
      {% set wd = now().isoweekday() %}
      {{ (h == 8  and 1 <= wd <= 5) or
         (h == 10 and wd in [6,7]) }}
action:
  - service: cover.open_cover
    target:
      entity_id: cover.rho1_cover
mode: single

The template works like this:

If the current hour is 8 and the weekday is between 1 and 5 inclusively OR
If the current hour is 10 and the weekday is either 6 or 7

now().isoweekday() returns the current weekday as a number between 1 (Monday) and 7 (Sunday).


NOTE

In case you’re wondering why your condition didn’t work it’s because the logical OR statement is in the wrong place. Look at the first example on this page:

The way you wrote your condition looks like this:

X and (Y or nothing)
2 Likes

You could use a choose block with your trigger as is with an automation like this. It’s perhaps a little more complex than using templates but I’ve become averse to templates because the “new” automation options are very powerful.

alias: 'Test '
description: ''
trigger: []
condition: []
action:
  - choose:
      - conditions:
          - condition: time
            weekday:
              - sat
              - sun
            after: '9:59:00'
        sequence:
          - service: light.turn_on
            target:
              entity_id: light.ge_14294_inwall_smart_dimmer_level
      - conditions:
          - condition: time
            weekday:
              - mon
              - tue
              - wed
              - thu
              - fri
            before: '8:31:00'
        sequence:
          - service: light.turn_on
            target:
              entity_id: light.ge_14294_inwall_smart_dimmer_level
    default:
      - delay:
          hours: 0
          minutes: 0
          seconds: 1
          milliseconds: 0
mode: single

The delay is just to have something at the “wrong time.” You may be able to leave it out.

I’ll test 123 Taras solution, the code looks cleaner and easier to understand. Manual execution works, so I’ll check if tomorrow it opens :slight_smile: thanks a lot

For those averse to templates, here’s the automation with its original condition in the proper “this or that” format:

alias: Open covers
description: ''
trigger:
  - platform: time
    at:
      - '8:30'
      - '10:00'
condition:
  - condition: or
    conditions:
      - condition: time
        weekday:
          - mon
          - tue
          - wed
          - thu
          - fri
        after: '8:29'
      - condition: time
        after: '9:59'
        weekday:
          - sat
          - sun
action:
  - service: cover.open_cover
    target:
      entity_id: cover.rho1_cover
mode: single