So I’m doing this automation for my son that will call a sunrise routine but he wants to be able to specify a different time of the day per week day, so Monday could have a different time than Tuesday, etc. My solution is to use seven input_datetime (time only, no date) in Lovelace and the following automation trigger. From previous conversations here, I realize that I have way to learn on templates so this could probably be written in a more efficient way (if the time is set to 00:00, it means there is no sunrise for that day):
{% set today_weekday = now().weekday() %}
{% set today_time = states.sensor.time.state %}
{% if today_weekday == 0 %} {% set set_time = (states.input_datetime.martin_lundi.attributes.timestamp | int | timestamp_custom("%H:%M", False)) %}
{% elif today_weekday == 1 %} {% set set_time = (states.input_datetime.martin_mardi.attributes.timestamp | int | timestamp_custom("%H:%M", False)) %}
{% elif today_weekday == 2 %} {% set set_time = (states.input_datetime.martin_mercredi.attributes.timestamp | int | timestamp_custom("%H:%M", False)) %}
{% elif today_weekday == 3 %} {% set set_time = (states.input_datetime.martin_jeudi.attributes.timestamp | int | timestamp_custom("%H:%M", False)) %}
{% elif today_weekday == 4 %} {% set set_time = (states.input_datetime.martin_vendredi.attributes.timestamp | int | timestamp_custom("%H:%M", False)) %}
{% elif today_weekday == 5 %} {% set set_time = (states.input_datetime.martin_samedi.attributes.timestamp | int | timestamp_custom("%H:%M", False)) %}
{% else %} {% set set_time = (states.input_datetime.martin_dimanche.attributes.timestamp | int | timestamp_custom("%H:%M", False)) %}
{% endif %}
{{ set_time != "00:00" and set_time == today_time }}
Currently, that code is used in a script s such as below to test it. I haven’t implemented it in a automation trigger yet.
Another way to pick the correct helper is the code below. I tested this with the number in the middle of the entity_id, like this input_datetime.martin_1_wakeup, but I am sure the solution below will work too. The only if you will need now, is one that catches the the today_weekday > 5.
I understand the logic of each line but the [:5] escapes me. I think it has to do with keeping just the time part (ie, the | int | timestamp_custom("%H:%M", False) in my code but not sure how it works.
edit:
Playing around in template editor, I understand now that it keeps the first 5 characters, dropping the seconds which states('sensor.time') doesn’t return.
Your input_datetime contains a string representing the time. It looks like this:
11:57:00
However, sensor.time reports a string that looks like this:
11:57
If we wish to compare those two strings, we need to use only the first five characters of 11:57:00. So we use [:5] which is the same as [0:5] to get what we need.
Paste this into the Template Editor and experiment with it:
Thanks for your feedback @123
Good to see I could help and you took it to a higher level.
I am always into making code as short as reasonable possible, as long as it’s easy to read and understand.
I’d have picked the same solution @SylvainGa did by the way.