Help needed with time templating

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…

@snizzleorg

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.
Home-page: http://jinja.pocoo.org/
Author: Armin Ronacher
Author-email: [email protected]
License: BSD
Location: /srv/homeassistant/lib/python3.4/site-packages
Requires: MarkupSafe

I suppose it would be easier to just compare the minutes, that probably can be done by extracting the minutes part of the srtring than turning it into integers and doing the math. Hower this will fail if the bus leaves lets say 12:05 and it is 11:59 …

still puzzled by why {{as_timestamp( strptime( now().strftime("%H:%M"), "%H:%M" ) )}} comes up empty.

Maybe this has to do with some kind of locale setting or the platform. I’m using hassbian on a raspberry pi. This is my locale:

locale
LANG=en_GB.UTF-8
LANGUAGE=
LC_CTYPE="en_GB.UTF-8"
LC_NUMERIC="en_GB.UTF-8"
LC_TIME="en_GB.UTF-8"
LC_COLLATE="en_GB.UTF-8"
LC_MONETARY="en_GB.UTF-8"
LC_MESSAGES="en_GB.UTF-8"
LC_PAPER="en_GB.UTF-8"
LC_NAME="en_GB.UTF-8"
LC_ADDRESS="en_GB.UTF-8"
LC_TELEPHONE="en_GB.UTF-8"
LC_MEASUREMENT="en_GB.UTF-8"
LC_IDENTIFICATION="en_GB.UTF-8"
LC_ALL=

That I’m not sure of, I’m running a manual install on my OS X server so no experience with the rpi. If you break it apart does any part of the nesting resolve in the template viewer?

All but the as_timestamp is working…

I’m getting closer, but I still can’t get it to trigger when used as a trigger with an automation.

Here’s how I got the two times to evaluate correctly.

First made a sensor to calculate the time to leave. This returns todays date appended to the time to leave accounting for traffic.

sensors:
  -platform: template
   sensors:
     go_get_kids:
       value_template: '{{now().strftime("%Y") }}-{{now().strftime("%m") }}-{{now().strftime("%d") }} {{strptime("17:00", "%H:%M") -strptime(states.sensor.dad_work_to_daycare.state, "%M") }}'

This allows me to create a template that I can evaluate the “leave time” against now.

{{ as_timestamp(states.sensor.go_get_kids.state) | timestamp_custom("%H:%M") == as_timestamp(now()) | timestamp_custom("%H:%M") }}

The problem I am having is getting an automation from here to evaluate true. I have tried simply setting {{ as_timestamp(states.sensor.go_get_kids.state) | timestamp_custom("%H:%M") == as_timestamp(now()) | timestamp_custom("%H:%M") }} as a template trigger like this:

automation:
  trigger:
    platform: template
    value_template: '{{ as_timestamp(states.sensor.go_get_kids.state) | timestamp_custom("%H:%M")  == as_timestamp(now()) | timestamp_custom("%H:%M") }}'

but that doens’t work.

I then tried to create a binary sensor but that failed as well. If anyone can figure that out, we can piece together the other pieces to make this type of time templating possible.

You might try to create a new sensor to include the date on top of your bus sensor.
something like this:
{{now().strftime("%Y") }}-{{now().strftime("%m") }}-{{now().strftime("%d") }} {{ states.sensor.bus.state }}

Then try to work through how to alert based off of that. That’s where I am failing so I don’t have any recommendations there unfortunately.

Did you ever find a solution for this?
I have a similar problem.

I just gave up… sorry

1 Like

I ended up utilizing a different approach based off of a solution provided here:

Maybe you could try going that route as I gave up trying to utilize this method just as @snizzleorg did…