Is there a simpler way to achieve this template output, please?

I have a water_heater entity that has an attribute for each day of the week that holds an array of start and end times.

If I wanted to pull back today’s array of start and end times, to display in a dashboard card, what’s the best approach please?

Here’s the relevant attribute for the entity…

time_program_dhw: 
extra_fields: {}
monday:
  - extra_fields: {}
    start_time: 180
    end_time: 300
tuesday:
  - extra_fields: {}
    start_time: 180
    end_time: 300
  - extra_fields: {}
    start_time: 1260
    end_time: 1320
wednesday:
  - extra_fields: {}
    start_time: 180
    end_time: 300

Is this the best way to pass in the current day of the week to get the array for today?

{% set d = as_timestamp(states('sensor.date')) | timestamp_custom('%A') %}
{% set tProgDHW = state_attr('water_heater.home_domestic_hot_water_0','time_program_dhw') %}
{% for item in tProgDHW[d.lower()] %}  {# expects lowercase weekday string #}
{%- set st = as_timestamp(today_at(timedelta(minutes=item["start_time"]))) | timestamp_custom('%H:%M') %}
{%- set et = as_timestamp(today_at(timedelta(minutes=item["end_time"]))) | timestamp_custom('%H:%M') %}
{{ st }}{{ " - " }}{{ et }}
{%- endfor %}

Renders the following for ‘tuesday’ :

03:00 - 05:00
21:00 - 22:00

Is there a better/simpler way to convert the minutes from midnight in to HH:MM, please?

Any recommendations for a dashboard card to display this info?
Simplest option I guess is a markdown card.

{{ now().weekday() }}

or

{{ now().isoweekday() }}
1 Like

Thank you. In this instance I need the word, rather than integer.

Just stumbled on this…

{% set d = now().strftime("%A").lower() %}

But it would nice to avoid now() as that causes it to update at the start of each minute.

Your template employs today_at so it’s already updating every minute.

Excellent point. :wink:

Is there a better way to add minutes, as an integer, to midnight today?

All I’m really trying to achieve is to convert integer 180 (start_time) to 03:00 and 300 (end_time) to 05:00

I overlooked this important detail. I have Template Sensors on the brain and thought this was yet another Template Sensor. :man_facepalming:t3:

I may be wrong but I think the template in a Markdown card is evaluated only while the card is displayed. In other words, the code only runs when there’s a need to run it. So unless you are displaying that card all day, the template will update every minute while the card is displayed (it’s not a resource hog).

In contrast a template in a Template entity (sensor, binary_sensor, lock, cover, etc) or in a Template Trigger is evaluated, continuously, according to the frequency described in the documentation.

Thanks, that’s really worth knowing.
Amusingly, I opted to use my template as the state in an paper-buttons-row card. :man_facepalming:
I will swap that out for the markdown card.

Tip:

If you ever need to create a Template Sensor having a complex template but you want fine-grained control over when it’s evaluated, consider using a Trigger-based Template Sensor.

A Trigger-based Template Sensor will evaluate its template only when triggered by one of its triggers. In other words, a template containing now() or today_at() won’t be evaluated every minute. Nothing that changes state in the template forces an update. Evaluation only occurs when one of the triggers is triggered.

1 Like