Work Travel Time Alert

I have a sensor.work_travel_time setup using Waze and I would like to setup an alert when the travel time is greater than 20 minutes so that I can make it to work early. So I need to be alerted with about 20 minutes of notice meaning that the alert needs to be 20 minutes greater than the travel time and I need to be at work at 8:00. So if all of the sudden the travel time is 30 minutes I need to be alerted at 7:10.

How would I make an automation using those requirements?

I was thinking of creating a new Leave announcement time sensor that uses sensor.work_travel_time but adds 20 minutes. Then if that sensor is greater than 40 then it triggers an automation at 8:00 minus the Leave sensor time.

How would I create that automation? I get stuck trying to figure out how to make the Leave announcement time sensor and how to make the trigger for the automation greater than 40. Do I need to add an input boolean?

1 Like

Something like that

automation:
  trigger:
    - platform: template
      value_template: >-
	    {% set lag_in_minute = 20 %}
        {% set travel_time_in_minute = states("sensor.work_travel_time")|int %}
        {% set target_time = now().replace(hour=8).replace(minute=0).replace(second=0) %}

        {{ as_timestamp(target_time) - (travel_time_in_minute+lag_in_minute)*60 <= as_timestamp(now())}}

  action:
	- service: notify.notify
	  data:
		message: "You're gonna be late!"

2 Likes

Thanks, that’s something I can base the final alert on. I need to think about a condition for between which times to announce the alert. Since I am adding 20 minutes to drive time and normal drive time is about 17-19 minutes then latest I can get the announcement and be on time is about 7:20. So the earliest I want the announcement is what I need to figure out next. I think I will have to add a Choose in order to make the announcement in the bedroom between certain times and the kitchen after a certain time.

I also need to add the travel time as part of the announcement but I have figured out how to do that.

I decided to use example by koying as a condition instead of a trigger. I made it into a sensor in my template.yaml file

  - sensor:
      - name: Traffic Warning Condition
        state: >-
          {% set lag_in_minute = 20 %}
          {% set travel_time_in_minute = states("sensor.work_travel_time")|int %}
          {% set target_time = now().replace(hour=8).replace(minute=0).replace(second=0) %}
          {{ as_timestamp(target_time) - (travel_time_in_minute+lag_in_minute)*60 <= as_timestamp(now())}}

I will use Time Pattern of /5 minutes for the Trigger and a condition of Time between 6:30 and 7:30 so that it will nag every 5 minutes between those times if both conditions are met.

How do I make a Leave Sensor that does a calculation of Leave Time = 8:00-Travel Time? The following doesn’t work

{% set travel_time_in_minute = states("sensor.work_travel_time")|int %}
{% set target_time = now().replace(hour=8).replace(minute=0).replace(second=0) %}
{{ as_timestamp(target_time) - (travel_time_in_minute)*60}}

I want to use the Leave Sensor as part of the alert and maybe as part of the condition

What exactly does not work?

{% set travel_time_in_minute = states("sensor.work_travel_time")|int %}
{% set target_time = now().replace(hour=8).replace(minute=0).replace(second=0) %}
{{ as_timestamp(target_time) - (travel_time_in_minute)*60}}

Result type: number

1628167320.004825

How do I convert that to the Leave Time? ie 8:00 AM - Travel Time

Add timestamp_custom to make it a timestamp:

{{ as_timestamp(target_time) - (travel_time_in_minute)*60 | timestamp_custom('%H:%M') }}

Or, for a full date/time

{{ (as_timestamp(target_time) - (travel_time_in_minute)*60) | timestamp_local }}

I get TypeError: unsupported operand type(s) for -: 'float' and 'str' on both of those. I searched Google for a possible fix but I can’t find one. Any ideas what I can add or change to remove the error?

Sorry…

{{ (as_timestamp(target_time) - (travel_time_in_minute)*60) | timestamp_custom('%H:%M') }}
1 Like

Thanks. I see now that koying had it correct but I was just changing what I thought was the only difference instead of the whole line.

So what I learned is that I always need to place the whole calculation within parentheses even if there is a conversion after the calculation. I hope I can remember that or at least remember to be come back here to find that information. Is the need for parentheses documented somewhere so I can maybe reference that also?

No, @Hellis81 did the right fix, and I updated my answer to not let wrong code hanging around :wink:

1 Like

I don’t know of a reference about jinja.
There probably is a language docs somewhere, but it’s not Home Assistants language.

1 Like

This is as close as a jinja2 reference you can get, I guess.
Not sure if everything is implemented in HA,.but I’m sure HA added multiple functions/filters

https://jinja.palletsprojects.com/en/2.10.x/templates/