Help with coding for day of the week check

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

Thanks for your reply.
I appli your code and it give me:

- id: '1593642521028'
  alias: Aspirateur
  description: ''
  trigger:
  - platform: template
    value_template: 'value_template: "{{ states(''sensor.date_time'') == (states.input_datetime.heure_passage_aspirateur.attributes.timestamp
      | int | timestamp_custom(''%H:%M'', True)) }}"'
  condition:
    {% if not is_state('input_select.aspi_'+nameday, 'Aucun')%}      #check if a script is selected in the inputselect
  action:
  - data:
      service_template: "script.{{ states('input_text.nameday') }}"      #launch the script

nameday is a sensor value who gives the name of the day (tuesday for exemple)
there is something wrong in the condition but i can’t make it work.

  condition:
    condition: template
    value_template: "{{ not is_state('input_select.aspi_'+nameday, 'Aucun') }}"