Help needed with time templating

I have a sensor that shows when the next bus leaves. The format is something like ‘hh:mm’

I want to get a bus leaving in mm minutes sensor.

I suppose this can be done with something like

{{now()|timestamp - states.sensor.bus.state|timestamp}}

the problem however is that the sensor delivers the time in a format that does not include date so the timestamp will not work…

Any idea of how to best approach this

You can format timestamps with strftime ala:

{{now().strftime('%H:%M %Y-%m-%d') }}

Just do all of your math with timestamps, then format for display at the end.


True. Share your projects was not the right category. Dunno How it ended up there. I moved it.

Sure I can do the math with timestamps. But how do I get to timestamp from a string that is ‘12:00’ ?

Now I can put into a timestamp easily but a string only containing hours and minutes - thats where I don’t get anywhere

I think you want strptime. That creates a datetime from a string.

{% set time = "12:45" %}
{{ strptime(time, "%H%M") }}

Using the template dev tool is a great way to experiment and test.

Do a forum search for strptime and you’ll see some examples. But I ran into issues getting it to work, so did some splitting of strings…

1 Like

I usually refer to this when trying to template time; might be helpful:

1 Like

Thanks. Thats helpful. Still i have the problem subtracting the two. I need something like:

{{ now() - strptime(time, "%H%M") }}

i thought

 {{ as_timestamp(now()) - as_timestamp( strptime(time, "%H%M")) }}

would be working but it doesn’t

I am working on EXACTLY the same thing right now!

I want to send a notification to pickup the kids by 5pm every day. So basically I want a notification when to leave accounting for traffic. My logic is 5pm - traffic. I have that working as:

{{ now().strftime("%H:%M:%S") == states("sensor.go_get_kids") }}

You should try this:

'{{ now().strftime("%H:%M:%S") == states("sensor.bus") }}'

The issue we will run into though, is that because this formats it so weirdly, we can’t build triggers off of this output for notifications. I am working through that piece now, and I can’t get it to fire at the correct time. I think it has to do with the seconds that seem to be unavoidable.

Let me know if you get this working and then also the notification.

This gives you minutes until ‘time’.

{% set time = "12:45" %}
{{ (((as_timestamp(strptime(time, "%H:%M"))) - as_timestamp( strptime( now().strftime("%H:%M"), "%H:%M") )) /60) | int}}

For the automation should be able to just toss a state in there and compare the values you want and return true/false

Thanks. Already looks complicated but I get your idea…

problem is that this

{{strptime( now().strftime("%H:%M"), "%H:%M") }}

is working.
getting a timestamp from it however is not.

{{as_timestamp( strptime( now().strftime("%H:%M"), "%H:%M" ) )}}
```evaluates to empty.

Not sure what you mean, your last line does return a timestamp…
Something like ‘-2208909780.0’ depending on when you’re now() is.

It’s based on Epoch/Unix time stamps. Since there is no date it defaults to the beginning (Jan 1 1900).
You don’t care about date though so having both default to that doesn’t affect the time output.

did you evaluate this in the dev tools of HASS? I get nothing from that:

Weird… I’m on 45.1

me too. 45.1
Can it be due to the browser? I’m on safari. But same problem firefox

It comes back blank for me, too, on .45.0

But I think you’re doing too much work on the “now as timestamp” part. Just use:

{{as_timestamp( now() ) }}

That’ll return a timestamp that you can add/subtract with.

Problem is that will return a time stamp with today’s date and then you’ll be subtracting apples and oranges. Something thing in 1900 and something in 2017. Timestamp is always a date/time representation so if you only have time on one of them you need to have than with the same base date for the math to make sense.

Yeah - I see what you mean. You’d have to append a full date to the dateless “bus” sensor for the math to work…

Right either add date to bus or remove date from now…


Just tried on Safari and iOS, works on both for me…

What about jinja2 versions? What we’re doing here really has nothing to do with HA.

$ pip3 show Jinja2

I’m on 2.9.6

The issue with adding or removing the date is what I am running in to as well. Even if you get a time, without a date (at least in my experience), will not work as a trigger. I think it has to do with creating the timestamp. If we do it as above, the output doesn’t seem to be recognized as a true complete timestamp.

As a side note, I couldn’t get {{as_timestamp( strptime( now().strftime("%H:%M"), "%H:%M" ) )}} to resolve either.

I figured you would be comparing minutes for the automation (integers) not times. i.e. 5:00pm - traffic <= 5 (min) trigger should leave soon, or something like that. Or even a sensor that shows minutes until needing to leave (can always be formatted as pretty hours:minutes) after the fact.

pip3 show Jinja2
Name: Jinja2
Version: 2.9.6
Summary: A small but fast and easy to use stand-alone template engine written in pure python.
Author: Armin Ronacher
Author-email: [email protected]
License: BSD
Location: /srv/homeassistant/lib/python3.4/site-packages
Requires: MarkupSafe