Need some help with coding a Template Helper

I’m trying to write a template helper that examines the (numeric) state of a sensor and depending on the value returns a time value (h:mm:ss). The intent is that this helper can be used as the comparison target in a time condition. Despite being a programmer for many years I have to say that I am struggling somewhat with the syntax and functionality of HA scripting, templates etc. as the documentation does not seem complete or detailed enough. Can someone maybe take a look at my code and suggest what might be wrong (the value of this helper is always ‘Unknown’):

{% set resv = states(‘sensor.powerwall_backup_reserve_soc’) | int(0) %}
{% if resv == 15 %}
{% set starttime = ‘22:30:00’ %}
{% elif resv == 20 %}
{% set starttime = ‘20:00:00’ %}
{% elif resv == 25 %}
{% set starttime = ‘19:00:00’ %}
{% elif resv == 30 %}
{% set starttime = ‘18:00:00’ %}
{% elif resv == 35 %}
{% set starttime = ‘17:00:00’ %}
{% else %}
{% set starttime = ‘20:00:00’ %}
{% endif %}
{{ starttime }}

That should work if you use correct quotes. You’re using fancy quotes you need to use non-fancy quotes. i.e. ' not ’

The ‘fancy quotes’ are an artefact of pasting the text into the text entry field for thsi forum. My actual code has normal quotes. If I run the code via ‘Developer Tools → Template’ I can see that it is working and returning a (correct) string value. The problem is that I need the return type to be timestamp (actually time in reality) as I want to use the result in a time comparison condition (e.g. before hh:mm:ss) but it seems that either the fact that the helper is defined as type ‘Timestamp’ (there is surprisingly no ‘time’ type for helpers, though there is a ‘date’ type) or the fact that the return value is a string may be the issue.

I guess I have more questions:

  1. In HA is a ‘timestamp’ just a time or is it a date-time value?
  2. Is there a type for just a time (h:mm:ss[.sss])? and if so what is it?
  3. Is there a way to cast/coerce the time string to something of type timestamp?
  4. Is my approach (helper of type timestamp) the right one here or should I be doing something different?

If the Template Sensor’s device_class is timestamp then its state value should be a datetime string in ISO format.

Replace the last line of your template with this:

{{ today_at(starttime).isoformat() }}

From the documentation:

  • timestamp: Datetime object or timestamp string (ISO 8601)

EDIT

Here’s another way to achieve the same result. It uses a dictionary to cross-reference the sensor’s value with the time.

{% set resv = states('sensor.powerwall_backup_reserve_soc') | int(0) %}
{% set t = {
  15: '22:30:00',
  20: '20:00:00',
  25: '19:00:00',
  30: '18:00:00',
  35: '17:00:00' } %}
{{ today_at(t.get(resv, '20:00:00')).isoformat() }}

123’s solution is the way to go.

Also, when replying to people, make sure to reply to them not the whole post. I did not get notified, so you replied to the whole post and not my comment.

  1. Actually it’s a number. It’s the number of seconds from the 01.01.1970
    EDIT:

    or an iso formatted datetime string, or a datetime object

  2. Datetime
  3. Use the filters/functions from here:
    Templating - Home Assistant

The problem with the fancy quotes comes from using “blockquote” to insert your code, where you should have used the “code” insert. Blockquote does change the quotes, whereas “code” doesn’t! :slight_smile: I know, details… :rofl: :rofl:

It’s a number in seconds from Jan 1st 1970, or an iso formatted datetime string, or a datetime object.

I thought, that’s what we have datetime for? :open_mouth: I will never get these small differences that HA makes with all these time things… Can’t it just be a timestamp is a number (the seconds) and a datetime object is a datetime? :laughing:

Problem with timestamps is that they don’t contain the timezone. It’s up to the user to figure that out, which never goes over well. If it were my choice, we’d just have datetime objects everywhere except for in the database, which would be a timestamp from UTC.

1 Like

That would be nice, and it would avoid so many questions! :laughing:

@123 Thank you, this seems to be the most optimal / elegant solution and it works a treat.

1 Like