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
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 %}
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....
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
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') }}
I just tried it in the template checker and it works, including changing state based on my input_booleans, so its looking good just waiting on a HA restart to confirm…
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.
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.
- 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') }}