Better way?

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.

service: persistent_notification.create
data_template:
  message: >-
     .......

Thanks

One improvenent is to use state_attr like this:

state_attr("input_datetime.martin_lundi","timestamp")

instead of

states.input_datetime.martin_lundi.attributes.timestamp

see why in the warning here.

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.

{% set today_weekday = now().weekday() %}
{% today_triggertime = state_attr("input_datetime.martin_"+today_weekday,"timestamp") %}

So now you only have to number the helpers instead of giving the days names.

Please let us know if you succeeded or not.

1 Like
{% set jours = {0:'lundi', 1:'mardi', 2:'mercredi', 3:'jeudi', 4:'vendredi', 5:'samedi', 6:'dimanche'} %}
{% set set_time = states('input_datetime.martin_'~jours[now().weekday()])[:5] %}
{{ set_time != "00:00" and set_time == states('sensor.time') }}

Tested and confirmed it works:


EDIT

If you following Recte’s suggestion to change the input_datetime names from text to numbers, the template reduces to this:

{% set set_time = states('input_datetime.martin_'~now().weekday())[:5] %}
{{ set_time != "00:00" and set_time == states('sensor.time') }}
2 Likes

Wow, thank you both :slight_smile:

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.

It’s how to slice a string in python.

https://www.pythoncentral.io/how-to-slice-listsarrays-and-tuples-in-python/

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:

{{ '11:57:00'[0:5] }}

What did you ultimately decide to do?

I chose the mix of both that you suggested:

{% set set_time = states('input_datetime.martin_'~now().weekday())[:5] %}
{{ set_time != "00:00" and set_time == states('sensor.time') }}

Works perfectly, thank you :slight_smile:

1 Like

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.