I recently set up the uk_transport & also the google_travel_time component. I would like to combine the “next bus estimated” with the data from Google to get an approx time of arrival.
I’ve been messing about with templates for a few days and can’t seem to get it right.
I need to replace the as_timestamp(now with sensor.next_bus_estimated. I’ve tried a few different ways using examples from the forum but just can’t see to get it working.
Would anyone be able to show/tell me how to add the times from these two sensors?
The output of the sensor.next_bus_estimated is “HH:MM”
The above does work, but it’s using the current time + the estimated travel time from Google.
In an ideal world what I would like is something like:
sensor.next_bus_estimated = 08:35
sensor.google_travel_time__transit = 25
Assuming that the date of the next bus is today, the safest way to get a full date/time that crosses midnight would be something like this:
# Get a time string that contains todays date and the next bus time
{% set time_string = now().strftime("%Y-%m-%d ")+states('sensor.next_bus_estimated') %}
# Convert that to a time_string into a datetime object and get the current time in seconds
{% set next_bus_time = strptime(time_string,"%Y-%m-%d %H:%M").timestamp() %}
# get the estimated bus route time in seconds
{% set trip_time = states('sensor.google_travel_time__transit') | int * 60) %}
# Add the bus time and the trip time, return the estimated arrival time
{{ ( next_bus_time + trip_time ) | timestamp_custom("%H:%M") }}
Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected ')') for dictionary value @ data['sensors']['trip_time']['value_template']. Got '{% set trip_time = states("sensor.google_travel_time__transit") | int * 60) %}'. (See ?, line ?). Please check the docs at https://home-assistant.io/components/sensor.template/
As I say, I’ve no idea if I’ve set it out correctly, it’s my first time trying to do something that isn’t “out of the box” so to speak.
Its only really the morning bus & traffic conditions I’m looking to gain insight into. Depending on traffic I can get a later bus.
- platform: template
sensors:
destination_time_from_bus:
value_template: >
# Get a time string that contains todays date and the next bus time
{% set time_string = now().strftime("%Y-%m-%d ")+states('sensor.next_bus_estimated') %}
# Convert that to a time_string into a datetime object and get the current time in seconds
{% set next_bus_time = strptime(time_string,"%Y-%m-%d %H:%M").timestamp() %}
# get the estimated bus route time in seconds
{% set trip_time = states('sensor.google_travel_time__transit') | int * 60) %}
# Add the bus time and the trip time, return the estimated arrival time
{{ ( next_bus_time + trip_time ) | timestamp_custom("%H:%M") }}
This template will only work if the bustime sensor always outputs a time. If it doesn’t, you need to add an if statement to output something else when it has no data.