Adding Two Time based Sensors

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.

The closest I’ve got is this:

{{ ( as_timestamp(now()) + (states("sensor.google_travel_time__transit") | int) * 60 ) | timestamp_custom("%H:%M %p") }}

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?

Many Thanks

What is the output of your bus travel sensor?

The code above should indeed work, it nigh a perfect replica of what I use for ETA for mu commute home via waze.

Hi @lolouk44, Many thanks for your reply

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

sensor.next_bus_estimated + sensor.google_travel_time__transit = 09:00

Many Thanks

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") }}
2 Likes

Hi @petro, many thanks for taking the time to reply.

Please forgive me as I’m still getting to grips with Templating so I’m not too sure if I have done this right but I tried it like this:

- platform: template
    sensors:
      time_string:
        friendly_name: 'time_string'
        value_template: '{% set time_string = now().strftime("%Y-%m-%d ")+states("sensor.next_bus_estimated") %}'
  - platform: template
    sensors:
      next_bus_time:
        friendly_name: 'next_bus_time'
        value_template: '{% set next_bus_time = strptime(time_string,"%Y-%m-%d %H:%M").timestamp() %}}'
  - platform: template
    sensors:
      trip_time:
        friendly_name: 'trip_time'
        value_template: '{% set trip_time = states("sensor.google_travel_time__transit") | int * 60) %}'
  - platform: template
    sensors:
      estimated_time_of_arrival:
        friendly_name: 'Estimated Time Of Arrival'
        value_template: '{{ ( next_bus_time + trip_time ) | timestamp_custom("%H:%M") }}'

& it returns an error of:

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.

Many thanks

No, that all goes in 1 template:

- 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") }}
1 Like

think you have one too many ) in there?

1 Like

I did try it all as one in the template tester in the dev tools and got:

Error rendering template: TemplateSyntaxError: unexpected ')'

Many thanks

This returns no error “but” I can’t test it as there are no buses on the route I have selected until the morning.

 - platform: template
    sensors:
      destination_time_from_bus:
        value_template: >
          {% set time_string = now().strftime("%Y-%m-%d ")+states('sensor.next_bus_estimated') %}
          {% set next_bus_time = strptime(time_string,"%Y-%m-%d %H:%M").timestamp() %}
          {% set trip_time = states('sensor.google_travel_time__transit') | int * 60 %}
          {{ ( next_bus_time + trip_time ) | timestamp_custom("%H:%M") }}

The template tester in the dev tools returns:

Error rendering template: UndefinedError: 'str object' has no attribute 'timestamp'

I’m guessing that due to the sensor “sensor.next_bus_estimated” not having a current value in it??

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.

1 Like

Many thanks for all your help, tested this sensor this morning & it is working fine. :+1: