Time template troubles

Could someone help me with this template please? I don’t quite understand the new default value in relation to time formatting.

          {% set e7_normal = 0.2328 %}
          {% set e7_cheap = 0.1378 %}
          {% if (states('sensor.time_utc')| timestamp_custom('%H:%M', False, now())) >= ('07:30' | timestamp_custom('%H:%M', False, now())) %}
          {{( e7_normal ) | float(0)}}  {% else %}
          {% if (states('sensor.time_utc')| timestamp_custom('%H:%M', False, now())) >= ('00:30' | timestamp_custom('%H:%M', False, now())) %}
          {{( e7_cheap ) | float(0)}}  {% else %}
          {{( e7_normal ) | float(0)}}  {% endif %} {% endif %}

I get the normal price all the time now. Upon investigation, it appears it is always using the default value. So is ‘7:30’ an invalid input?

What is the best way to compare time?

Your template contains a basic error that has nothing to do with default values. Please move it to a new topic (this topic isn’t meant for solving everyone’s fundamental templating mistakes). Done.

I guess introduction of default values have brought bad template coding out of the wood works.

What would be the basic error? invalid cast into timestamp_custom? What would be the correct way to write it?

{{ (states('sensor.time_utc')| timestamp_custom('%H:%M', False, now())) }} 
{{  ('00:30' | timestamp_custom('%H:%M', False, now())) }}

{{ (states('sensor.time_utc') | timestamp_custom('%H:%M', False)) }} 
{{  ('00:30' | timestamp_custom('%H:%M', False)) }}

Gives me

2021-10-14 15:10:00.005156+01:00 
2021-10-14 15:10:00.005177+01:00

14:10 
00:30

This isn’t necessarily the best way but it’s an easy way:

          {% set e7_normal = 0.2328 %}
          {% set e7_cheap = 0.1378 %}
          {% set t = (now().time()|string)[:5] %}
          {% if t >= '07:30' %} {{ e7_normal }}
          {% elif t >= '00:30' %} {{e7_cheap }}
          {% else %} {{  e7_normal }}  
          {% endif %}

That’s just comparing string isn’t it? I thought we needed to use a time format for comparison.

Could you enlighten my why does this works:

{{ (state_attr('input_datetime.plug_start', 'timestamp') | timestamp_custom('%H:%M', False, now())) }}

But this doesn’t?

{{ (states('sensor.time_utc')| timestamp_custom('%H:%M', False, now())) }} 

But thank you very much for the easy solution. I’m using UTC time because my meter doesn’t switch to summer time. This is what I ended up with:

          {% set e7_normal = 0.2328 %}
          {% set e7_cheap = 0.1378 %}
          {% set t = (states('sensor.time_utc')|string)[:5] %}
          {% if t >= '07:30' %} {{ e7_normal }}
          {% elif t >= '00:30' %} {{e7_cheap }}
          {% else %} {{  e7_normal }}  
          {% endif %}

In that case, just change now() to utcnow(). There’s no need to use the sensor.time_utc entity.

          {% set e7_normal = 0.2328 %}
          {% set e7_cheap = 0.1378 %}
          {% set t = (utcnow().time()|string)[:5] %}
          {% if t >= '07:30' %} {{ e7_normal }}
          {% elif t >= '00:30' %} {{e7_cheap }}
          {% else %} {{ e7_normal }}  
          {% endif %}
1 Like

timestamp_custom expects to receive a numeric timestamp value (seconds since the Unix Epoch).

  • The first one works because it supplies timestamp_custom with a numeric timestamp value.
  • The second one fails because it supplies timestamp_custom with a time string value.

NOTE

If timestamp_custom is given a value it cannot convert it simply reports the supplied value by default. For example, the result of this is cat

{{ 'cat' | timestamp_custom('%H:%M') }}

This default behavior will change soon.

Starting in version 2021.10.0 it will warn you that you overlooked to provide timestamp_custom with a default value. In 2021.12.0 you will be obligated to supply a default value.

Thank you. That perfectly explains why adding the default no longer works. I guess I was comparing strings before 2021.10.

What is the correct way to turn a string, like ‘00:30’ or states('sensor.time_utc'), into numeric value can be used by timestamp for formatting?

Why do you want to convert a time string into a numeric timestamp only to convert it back to a time string with timestamp_custom?

To me, this part suggests (states('sensor.time_utc') is converting time string into numeric timestramp. What is the correct way to extract time string from a sensor?

No.

states('sensor.time_utc') returns the state value of sensor.time_utc which is, like for all entities, a string value. The states() function doesn’t “convert” an entity’s state value.

If you have not read this section in the documentation, I highly recommend it.

Afterwards, review this as well:

1 Like

Thank you.

You are right, I’ve not studied the state objects page, only read the templating page, which enabled me to write broken templates :sweat_smile: