Time Calculation - here Sensor Template , need help

Hi,

i hope you are all fine.
Today i am seeking help for an automation i have in mind for very long time but was not able to build.

I would like to show the point with a led stripe and color, when i have to have to start drive to work in the morning.
So if there is more traffic, i have to drive a bit more earlier.

Wih the new here sensor, this seems to be realizable.

So here is what i have so far (pretty the same from the example).

Automaton:

- id: weekdays check traffic every minute
  initial_state: 'on'
  trigger: 
  - platform: time_pattern
      minutes: '/1'
  condition:
    - condition: time
      after: '06:00:00'
      before: '07:30:00'
    - condition: time
      weekday:
        - mon
        - tue
        - wed
        - thu
        - fri
  action:
    - service: homeassistant.update_entity
      entity_id: sensor.work_tro

sensor:

  - platform: here_travel_time
    name: work_tro
    traffic_mode: true
    app_id: "xxxx"
    app_code: "xxx"
    origin_latitude: "xxxx"
    origin_longitude: "xxxx"
    destination_latitude: "xxxx"
    destination_longitude: "xxxx"

i think and im not sure about this, i need then a template sensor, which checks the work_tro sensor.

I need to be at work at 8 AM, so check the current time plus the travel time, if it is then 7.45 AM make the string green, will it be 7.55 AM orange and everything after 8 AM make it red

The rest i could with normal automations, like turn off the string if it is after 8 AM etc etc.
But i have an issue on how to realise the calculöation.

then i would need an automation like this:

Automation:

- id: traveltime work_to
  initial_state: 'on'
  trigger:
  	platform: numeric_state
    entity_id: sensor.xxxx
    above: 45
  action: 
  	service: light.turn_on
  	entity_id: light.xxx

I hope what i am tryining to reach and someone can help me here

Can I push it up ?
Anyone an idea ?

did you get it to work?

Here’s a template sensor that turns on when it’s time to leave for work. Requires sensor.time integration

binary_sensor:
- platform: template
  sensors:
    time_to_leave_for_work:
      value_template: >
        {% set arrival = '08:20' %}
        {% set travel_time = states('sensor.time_to_work') | float %}
        {% set arrival = as_timestamp(strptime(arrival, '%H:%M')) %}
        {% set t = as_timestamp(strptime(states('sensor.time'), '%H:%M')) %}
        {{ t > arrival - travel_time * 60 }}
      friendly_name: 'Time to Leave for work'

here’s a sensor that gives colors based on the last possible leave time.

sensor:
- platform: template
  sensors:
    work_drive_time_color:
      value_template: >
        {% set arrival = '08:20' %}
        {% set travel_time = states('sensor.time_to_work') | float %}
        {% set arrival = as_timestamp(strptime(arrival, '%H:%M')) %}
        {% set t = as_timestamp(strptime(states('sensor.time'), '%H:%M')) %}
        {% set delta = arrival - (t + travel_time * 60) %}
        {% if 60*60 <= delta < 15*60 %}
          green
        {% elif 15*60 <= delta < 5*60 %}
          yellow
        {% elif 5*60 <= delta <= 0 %}
          red
        {% else %}
          off
        {% endif %}
      friendly_name: 'Time to Leave for work'

You can use that sensor as the color for the automation. You can also use the state changes on the sensor to fire the automation.

- alias: turn on colored light for work travel time
  trigger:
  - platform: state
    entity_id: sensor.work_drive_time_color
  condition:
  - condition: template
    value_template: "{{ trigger.to_state.state != 'off' }}"
  action:
  - service: light.turn_on
    data_template:
      entity_id: light.xxxxx
      color_name: "{{ trigger.to_state.state }}"
2 Likes

Thanks for sharing. This is exactly what I wanted to accomplish but I came across an issue

I tried to use the above code for an automation I had in mind but the sensor always shows as unavailable and there is an error.
I do have a time sensor set up and I am using a google_travel time sensor as well. I am not sure why I am getting this .
I get this error message:
TemplateError(‘OverflowError: timestamp out of range for platform time_t’) while processing template 'Template("{% set arrival = ‘09:55’ %}

work_drive_time_color:
      value_template: >-
        {% set arrival = '07:40' %}
        {% set travel_time = states('sensor.google_travel_time') | float %}
        {% set arrival = as_timestamp(strptime(arrival, '%H:%M')) %}
        {% set t = as_timestamp(strptime(states('sensor.time'), '%H:%M')) %}
        {% set delta = arrival - (t + travel_time * 60) %}
        {% if 60*60 <= delta < 15*60 %}
          green
        {% elif 15*60 <= delta < 5*60 %}
          yellow
        {% elif 5*60 <= delta <= 0 %}
          red
        {% else %}
          off
        {% endif %} 
      friendly_name: 'Time to Leave for work

Thanks in advance