JLindvig

Dynamic travel time

Dec 7th, 2021 (edited)
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 1.45 KB | None | 0 0
  1. transport:
  2.   template:
  3.     - sensor:
  4.         - name: dynamic travel time
  5.           unique_id: 08420e6f28e54d45b49de95436047abf
  6.           state: >-
  7.             {# PARAMETERS TO BE CHANGED #}
  8.             {% set calendar = 'calendar.jacob_lindvig_gmail_com' %}
  9.             {% set work_keyword = 'Arbejde' %}
  10.             {% set travel_sensor = 'sensor.waze_travel_time_arbejde' %}
  11.             {% set off_state = 'off' %}
  12.             {% set time_before = 300 %}
  13.  
  14.             {# EXTRACTIONS OF VARIABLES #}
  15.             {# EXTRACT DATE AND TIME OF WORK #}
  16.             {% set work_datetime_str = state_attr(calendar, 'start_time') %}
  17.             {# EXTRACT THE DATE OF WORK #}
  18.             {% set work_date_str = work_datetime_str.split(' ')[0] %}
  19.             {# EXTRACT TODAYS DATE AS STRING #}
  20.             {% set today_date_str = (now() ~ '').split(' ')[0] %}
  21.  
  22.             {# MAGIC BELOW #}
  23.             {# IS THE EVENT TODAY AND IS IT MARKED WITH THE KEYWORD #}
  24.             {% if work_date_str == today_date_str and is_state_attr(calendar, 'message', work_keyword) %}
  25.               {# CALCULATE THE DEPATURE TIME AND OUTPUT IT AS HH:MM #}
  26.               {{
  27.                 (
  28.                   (work_datetime_str | as_timestamp()) -
  29.                   time_before -
  30.                   (( states(travel_sensor) | float ) * 60)
  31.                 ) | timestamp_custom('%H:%M')
  32.               }}
  33.             {% else %}
  34.               {{ off_state }}
  35.             {% endif %}
Add Comment
Please, Sign In to add comment