How to set Automation Condition from Weekday from Input Select with Template

Tags: #<Tag:0x00007f7802cad970> #<Tag:0x00007f7802cad830>

Hi!

I have an automation, which I would like to run on a specific day.

The automation runs at Midnight (00:00), and uses a condition to check if it’s the correct day…

I have an input select, input_select.day_picker, with the days of the week in its list.

I’d like my automation to check if TODAY is the day picked in my input select, but I can’t quite work out how to do it.

I know I will probably need a Template based condition, and somehow use now().time().strftime("%A") to find out if today matches the input select… but not sure exactly what code to use.

Any suggestions? Thanks in advance!!

Hi,
Did you ever figure this one out?

Nope, didn’t get any further. :pensive:

Hey guys, I just came across this and realised that what you are looking for is very similar to how I have my irrigation system setup. I use input_booleans to select if a day is a watering day or not, then templates to check if today is a watering day. There is a lot more to the code but I have pasted the bits that I think will help with what you need.

sensor:
  - platform: template    # determine if today is selected as a watering day for program 1
    sensors:
      retic_program1_watering_day:
        entity_id: input_boolean.retic_program1_monday, input_boolean.retic_program1_tuesday, input_boolean.retic_program1_wednesday, input_boolean.retic_program1_thursday, input_boolean.retic_program1_friday, input_boolean.retic_program1_saturday, input_boolean.retic_program1_sunday
        value_template:  >
          {% set sensor_names = [ 'monday', 'tuesday', 'wednesday','thursday','friday','saturday','sunday'] %}
          {% set today_name = sensor_names[now().weekday()] %}
          {% set entity_id = 'input_boolean.retic_program1_'+today_name %}
          {{ is_state(entity_id, 'on') }}

      retic_program2_watering_day:
        entity_id: input_boolean.retic_program2_monday, input_boolean.retic_program2_tuesday, input_boolean.retic_program2_wednesday, input_boolean.retic_program2_thursday, input_boolean.retic_program2_friday, input_boolean.retic_program2_saturday, input_boolean.retic_program2_sunday
        value_template:  >
          {% set sensor_names = [ 'monday', 'tuesday', 'wednesday','thursday','friday','saturday','sunday'] %}
          {% set today_name = sensor_names[now().weekday()] %}
          {% set entity_id = 'input_boolean.retic_program2_'+today_name %}
          {{ is_state(entity_id, 'on') }}

input_boolean:
  retic_program1_monday:   # watering day selections for program 1
    name: Monday

  retic_program1_tuesday:
    name: Tuesday

  retic_program1_wednesday:
    name: Wednesday

  retic_program1_thursday:
    name: Thursday
    
  retic_program1_friday:
    name: Friday

  retic_program1_saturday:
    name: Saturday
    
  retic_program1_sunday:
    name: Sunday

  retic_program2_monday:   # watering day selections for program 2
    name: Monday

  retic_program2_tuesday:
    name: Tuesday

  retic_program2_wednesday:
    name: Wednesday

  retic_program2_thursday:
    name: Thursday
    
  retic_program2_friday:
    name: Friday

  retic_program2_saturday:
    name: Saturday
    
  retic_program2_sunday:
    name: Sunday
1 Like

Hi @sparkydave and thank you for the code! How do you ensure the entity will change the state after it turn to another day. I noticed that it keeps unchanged. Any help on this please?

Not sure if I fully understand what you are asking. Which entity are you referring to? the sensors? All that my code does is give you input_selects for each day of the week, then sensors to see if ‘today’ is selected to ‘on’ or not. When the day changes (ie: midnight) the sensor will automatically change to on/off depending on what the input_select status is for the new day of the week

I think it’s the known limitation of now(). being used to TRIGGER automations.

Personally i setup a sensor that gives me what day of the week it is:

    date_dayoftheweek:
      value_template: >
        {% set ct = states('sensor.date') %}
        {% set ct = as_timestamp(strptime(ct,'%Y-%m-%d')) %}
        {{ ct | timestamp_custom("%A") }}

FWIW I also have one for weekdays/weekends:

    date_weekend:
      value_template: >
        {% if is_state('sensor.date_dayoftheweek', 'Saturday') %} weekend
        {% elif is_state('sensor.date_dayoftheweek', 'Sunday') %} weekend
        {% else %} weekday
        {% endif %}

I then use these as conditions in automations to allow or not allow it to proceed. Guessing you might be able to use some of the same for your needs?

1 Like

It won’t update me after midnight. I need to run automation which runs homeassistant.update_entity on the sensor. Just tested it and it works :slight_smile: