[Solved] Day of Week and Time Condition issue

Hi folks,

I want to use single Automation for the alarm. I switched from a single date-time helper to 7 of them, each for a different day. This automation is triggered every day, which equals different values from the helpers. Then Time conditions are checking a weekday and because I can use just after and not the exact time, the condition that day is also met at later times (i.e. Mon-Fri 6:20 or 6:30, Weekend 8:00).

I don’t want to split this automation into 7 different, also I can’t use the day of the week in triggers and as I’m HA n00b so I don’t know if/how I can use some templating and combine after/before and add and subtract some seconds in Time condition.

Thanks a lot for any suggestions.

alias: morning alarm
description: ""
trigger:
  - platform: time
    at: input_datetime.alarm_mon
  - platform: time
    at: input_datetime.alarm_tue
  - platform: time
    at: input_datetime.alarm_wed
  - platform: time
    at: input_datetime.alarm_thu
  - platform: time
    at: input_datetime.alarm_fri
  - platform: time
    at: input_datetime.alarm_sat
  - platform: time
    at: input_datetime.alarm_sun
condition:
  - condition: state
    entity_id: input_boolean.alarm_radio
    state: "on"
  - condition: or
    conditions:
      - condition: state
        entity_id: binary_sensor.workday_sensor
        state: "on"
      - condition: state
        entity_id: input_boolean.alarm_nonworkingday
        state: "on"
  - condition: or
    conditions:
      - condition: time
        weekday:
          - mon
        after: input_datetime.alarm_mon
      - condition: time
        weekday:
          - tue
        after: input_datetime.alarm_tue
      - condition: time
        weekday:
          - wed
        after: input_datetime.alarm_wed
      - condition: time
        weekday:
          - thu
        after: input_datetime.alarm_thu
      - condition: time
        weekday:
          - fri
        after: input_datetime.alarm_fri
      - condition: time
        weekday:
          - sat
        after: input_datetime.alarm_sat
      - condition: time
        weekday:
          - sun
        after: input_datetime.alarm_sun
    enabled: true
action:

Have you considered a Schedule Helper? That would allow you to get rid of all those trigger/condition pairs…

alias: morning alarm
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.morning_alarm
    to: 'on'
condition:
  - condition: state
    entity_id: input_boolean.alarm_radio
    state: "on"
  - condition: or
    conditions:
      - condition: state
        entity_id: binary_sensor.workday_sensor
        state: "on"
      - condition: state
        entity_id: input_boolean.alarm_nonworkingday
        state: "on"
action:

I didn’t know about this helper but it doesn’t solve my other criteria to have time inputs in the Lovelace available to other regular users. And in the case of this helper, I will have to use yaml files because I’m not able to set for example 6:20 by GUI. And at the end, I lost patience after this…
image

Ok, since you need to keep the input datetimes, you can assign trigger id’s and use template conditions based on the current day’s strftime abbreviation:

alias: morning alarm
description: ""
trigger:
  - platform: time
    id: Mon
    at: input_datetime.alarm_mon
  - platform: time
    id: Tue
    at: input_datetime.alarm_tue
  - platform: time
    id: Wed
    at: input_datetime.alarm_wed
  - platform: time
    id: Thu
    at: input_datetime.alarm_thu
  - platform: time
    id: Thu
    at: input_datetime.alarm_fri
  - platform: time
    id: Sat
    at: input_datetime.alarm_sat
  - platform: time
    id: Sun
    at: input_datetime.alarm_sun
condition:
  - condition: trigger
    id: "{{ now().strftime('%a') }}"
  - condition: state
    entity_id: input_boolean.alarm_radio
    state: "on"
  - condition: or
    conditions:
      - condition: state
        entity_id: binary_sensor.workday_sensor
        state: "on"
      - condition: state
        entity_id: input_boolean.alarm_nonworkingday
        state: "on"
action:

Since it will be possible for you to have triggers that occur simultaneously, or nearly so, you will want to set the automation mode to something other than single to make sure you conditions get tested against all the triggers. Also, the abbreviations used by strftime() are localized so you may need to alter the trigger id’s if they are different wherever you are.

I like your solution with a trigger id but I’m not able to pass this condition even with inverted quotation marks: id: '{{ now().strftime("%a") }}' In developer mode, the template is producing correct string (i.e. Wed), but the condition is always False (except static value but I don’t want to go this way).

So I tried some templating in before: (time condition) where I added one second to the input_datetime.alarm_xxx … but it seems that I can’t use a template with before:, it produces an error while saving (I’ve tried even just {{ now() }}).

Solution: this single template condition can replace that OR with 7 conditions:

{{ now().strftime('%a%H:%M') == (trigger.id + states('input_datetime.alarm_'+trigger.id)[:5]) }}

Thank you @Didgeridrew for the hints! :slight_smile: