Help with coding for day of the week check

Hi All,

I am trying to create a way of checking if today is ‘selected’ or not to be a watering day in my reticulation program.

At first I was going to use an input_select to choose from a list of watering days, which the code then needs to check against what day it is today to prove true or false. To make the code a bit more internationally friendly (ie: not just for Western Australian watering days), and to hopefully figure out the code easier, I was looking at using an input_boolean for each day of the week and then evaluate that against what day it is today. I also looked at this example but its way over my head with code complexity.

Unfortunately… I’m stuck. Can someone please help? I have a feeling that maybe templates are the way to go but I cant get my head around how to create them and anything I try in the template dev tool just throws errors at me. I don’t find the template docs very helpful (ie: where does it explain the use of % etc…?)

to explain the above use-case I’m thinking something along the lines of:

#input_booleans to select garden watering days on/off
input_boolean: #details for below left out for the sake of this explaination
  monday:
  tuesday:
  wednesday:
  thursday:
  friday:
  saturday:
  sunday:

some code to determine if today is selected to 'on' in the above input_booleans

Assistance would be greatly appreciated

This might help? I have shamelessly squirreled away someone else’s ‘Good morning’ announcement code for my reworking when I get around to it. It checks day of the week.

- service: tts.google_say
  data_template:
    entity_id:
    - media_player.chalkboard_room
    message: >
      {% set hour = now().strftime("%H") %}
      {% set weekday_full_name = now().strftime("%A") %}
      {% set month_full_name = now().strftime("%B") %}
      {% set day = now().strftime("%-d") %}
      {% set weekday = now().strftime("%w") %}
      {% if hour | int < 12 %}
      Good morning. Today is {{ weekday_full_name }} {{ month_full_name }} the
        {% if day | int == 1 or day | int == 21 or day | int == 31 %}
          {{ day }}st
        {% elif day | int == 2 or day | int == 22 %}
          {{ day }}nd
        {% elif day | int == 3 or day | int == 23 %}
          {{ day }}rd
        {% else %}
          {{ day }}th
        {% endif %}.
      {% elif hour | int < 18 %}

with your input booleans, you could create a template_sensor that is on or off depending on those switches:

sensor:
  - platform: template
    sensors:
      run_the_water_system:
        value_template:  >
          {% sensor_names = [ 'monday', 'tuesday', 'wednesday','thursday','friday','saturday','sunday'] %}
          {% today_name = sensor_names[now().weekday()] %}
          {% entity_id = 'input_boolean.'+today_name %}
          {{ is_state(entity_id, 'on') }}
          

Then just look at the state of sensor.run_the_water_system being on.

so, just to let you know. now() gets the current time. now().weekday() gets the current weekday index based of monday equaling 0. So if we make a list of weekday names with monday being the first, then use the now().weekday() to pull out the name, we can then add the strings together to look at the current entity_id and extract the state.

EDIT:
Then in your automation, lets say you wanted to trigger the water every day at 5am:

- alias: Water the lawn
  trigger:
    - platform: time
      at: '05:00:00'
  condition:
    contidion: state
    entity_id: sensor.run_the_water_system
    state: 'on'
  action:
    - service: .... etc....
3 Likes

thank you so much! I will give this a try, plus it has helped me understand some of things that can be done with templates

I have tried to use your example and implement two ‘programs’ for my reticulation system but it fails the config verification. (once I get one program working I will duplicate for the second) This is what I have in my package:

reticulation.yaml

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

input_boolean:
  retic_program1_monday:
    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

but I get this error:

image

Any ideas?

I think it’s missing ‘set’

sensor:
  - platform: template    # determine if today is selected as a watering day for program 1
    sensors:
      retic_program1_watering_day:
        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') }}
2 Likes

I just tried it in the template checker and it works, including changing state based on my input_booleans, so its looking good :slight_smile: just waiting on a HA restart to confirm… :zipper_mouth_face:

thank you so much @petro and @anon43302295, your code has worked perfectly. now I just need to complete the automations and duplicate for the two ‘programs’ and I’ll be done. greatly appreciated! I’ll post the final product in the original thread once its all done.

2 Likes

I always forget the ‘set’ when going off memory.

you did very well to get it so close!

Please help. Hassio reports an errors:

Template sensor 'retic_program1_watering_day' has no entity ids configured to track nor were we able to extract the entities to track from the value template(s). This entity will only be able to be updated manually.

What’s wrong there???

add sensor.date integration.

sensor:
  - platform: time_date
    display_options:
      - 'date'

then add

  - platform: template    # determine if today is selected as a watering day for program 1
    sensors:
      retic_program1_watering_day:
        entity_id: sensor.date #<---------------ADD THIS
        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') }}

I only today needed to do exactly this but I came up with:

{% set today = now().strftime('%A') | lower %}

Is your way better or just different?

1 Like

nope, I didn’t even look at the template to be honest. I Just looked for what is wrong.

1 Like

Hello,
I write the code in the configuration.yaml but the “arrbambou_day” stay in false state even if i put all the day button on.:

  - platform: template 
    sensors:
      arrbambou_day:
        entity_id: arr_bambou_day    
        value_template:  >
          {% set sensor_names = ['monday', 'tuesday', 'wednesday','thursday','friday','saturday','sunday'] %}
          {% set today_name = sensor_names[now().weekday()] %}
          {% set entitid = 'input_boolean.arr_bambou_'+today_name %}
          {{ is_state(entitid, 'on'),on }} 

image
image

Can please anyone help?

Your last line is the problem. Not sure what type of sensor this is or what output you’re looking for. If you want the string ‘on’ or ‘off’, then:

{% if is_state(entitid, 'on') %}on{% else %}off{% endif %} 

…and if you want True or False (for a binary_sensor):

{{ is_state(entitid, 'on') }}

remove the ,on at the end.

Thank you both! it work!!!

Hello,
I want now to make a week day / zone selection for my vacuum program.
So i make a input selection for every week/day.


I now try to get value of the correct input text to launch the script
the automation would be something like that:

  action:
  - data: {}
    service: script.turn_on
    entity_id: script.+ value(input_text.'nameday')

the zones script are like this:

Couloir:
  sequence:
  - data:
      command: app_zoned_clean
      params:
      - - 25749
        - 16115
        - 30799
        - 20265
        - 1
    entity_id: vacuum.xiaomi_vacuum_cleaner
    service: vacuum.send_command 

i can’t manage to make it work.
Cane someone help please?

action:
 - data: {}
   service: script.turn_on
   entity_id: script.+ value(input_text.'nameday')

Would be

action:
  service_template: "script.{{ states('input_text.nameday') }}" 
1 Like