-
Create the object for the source of the time travelling calculation.
In my case the source is my phone position.
Install home assistant application on your phone and connect with your home assistant server.
Activate the location and all other permissions on this application in this way the application shared your position with your home assistant. This point is very important. If you don’t activate all location permissions the application doesn’t share the position with Home assistant server.
You have to keep the GPS activate in the phone every time.
This activity create new device tracker with this name ‘device_tracker.’
Create a new person in home assistant with your name and connect the new entity ‘device_tracker.’.
-
Create the object for the destination of the time travelling calculation.
Connect your Google Calendar with your home assistant server.
Follow the instructions in this link. Google Calendars - Home Assistant
When you have connected your Google Calendar, automatically in your home assistant there is this file google_calendars.yaml.
This file is in the same folder of configuration.yaml.
In this file you can insert the rule to extract only the meeting that you need.
This is my setting.
- cal_id: <yourmail>
entities:
- device_id: <yourmail>gmailcom
ignore_availability: true
name: <yourmail>
track: true
search: "Rem."
You can see the line with the search.
In my case I prefer to insert in the meeting title a key world (‘Rem.’ Reminder).
Home assistant receive only the meeting information that have in the title this word (Ex. ‘Work Rem.’ or ‘Dinner with Friends Rem.’) You can change with your word.
Now you have in home assistant some entity with the next meeting from your calendar.
Location: (the address)
calendar..attributes.location
Title
calendar..attributes.message
Start Time
calendar..attributes.start_time
I put this 3 value in 3 new sensor in my configuration.yaml.
cal_location:
value_template: '{{ states.calendar.<yourmail>.attributes.location }}'
friendly_name: 'Calendar Location'
cal_title:
friendly_name: 'Calendar Title'
value_template: '{{ states.calendar.<yourmail>.attributes.message }}'
cal_start_time:
value_template: '{{ states.calendar.<yourmail>.attributes.start_time }}'
friendly_name: 'Calendar Start'
unit_of_measurement: 'time'
- Calculate travelling time.
We need to configure the google travel time
Follow these instructions: Google Maps Travel Time - Home Assistant
This is my setting in the configuration.yaml
- platform: google_travel_time
api_key: <YourAPIKeyGooglecloud>
origin: person.fabio
destination: sensor.cal_location
name: Viaggio
options:
units: metric
This setting creates a new sensor with the time to travel from your phone location to the calendar address location by car (default) every 5 minute recalculate.
sensor.viaggio
You can change the name Viaggio with your name (Ex. Travel)
Now I have all the value to create some automation.
- Time calculation
I need 3 warning message with 3 different lead time.
Create 3 input numbers with the lead time that you prefer.
tempo_di_preparazione:
name: Tempo di preparazione
min: 0
max: 200
step: 1
mode: box
unit_of_measurement: 'Min.'
icon: mdi:timer
tempo_di_warning:
name: Tempo di warning
min: 0
max: 200
step: 1
mode: box
unit_of_measurement: 'Min.'
icon: mdi:timer
tempo_di_anticipo:
name: Tempo di anticipo
min: 0
max: 200
step: 1
mode: box
unit_of_measurement: 'Min.'
icon: mdi:timer
In my case I set these numbers in:
I get a message 35 min. before the time to leave for my preparation.
I get a message 15 min. before the time to leave for Warning.
I get a message 5 min. before the time to leave. To Leave with a little margin.
I get the message with 0 min in advance this is the last time to leave without delay.
I calculate the correct time for each message in a different sensor.
calc_leave_time:
value_template: '{{ ((as_timestamp(states.calendar.<yourmail>.attributes.start_time) - (states.sensor.viaggio.state | int * 60) - (states.input_number.tempo_di_anticipo.state | int * 60)) | timestamp_custom("%Y-%m-%d %H:%M") ) }}'
friendly_name: 'Leave Time'
unit_of_measurement: 'time'
calc_last_time:
value_template: '{{ ((as_timestamp(states.calendar. <yourmail>.attributes.start_time) - (states.sensor.viaggio.state | int * 60) ) | timestamp_custom("%Y-%m-%d %H:%M") ) }}'
friendly_name: 'Last Time'
unit_of_measurement: 'time'
calc_prep_time:
value_template: '{{ ((as_timestamp(states.calendar. <yourmail>.attributes.start_time) - (states.sensor.viaggio.state | int * 60) - (states.input_number.tempo_di_preparazione.state | int * 60) ) | timestamp_custom("%Y-%m-%d %H:%M") ) }}'
friendly_name: 'Prep Time'
unit_of_measurement: 'time'
calc_warn_time:
value_template: '{{ ((as_timestamp(states.calendar. <yourmail>.attributes.start_time) - (states.sensor.viaggio.state | int * 60) - (states.input_number.tempo_di_warning.state | int * 60) ) | timestamp_custom("%Y-%m-%d %H:%M") ) }}'
friendly_name: 'warn Time'
unit_of_measurement: 'time'
Now I have the time for each message.
calc_prep_time = start time meeting – travel time – prep time (35 min.)
calc_warn_time = start time meeting – travel time – warn time (15 min.)
calc_leave_time = start time meeting – travel time – leave time (5 min.)
calc_last_time = start time meeting – travel time
- Automation for message
To check in automation I need another sensor with the sys time with the same format of the time of the 4 sensor calculated in the previous point.
Sensor
sys_time:
value_template: '{{ (as_timestamp(strptime(states.sensor.date_time.state, "%Y-%m-%d, %H:%M"))) | timestamp_custom("%Y-%m-%d %H:%M") }}'
friendly_name: 'Sys Time'
unit_of_measurement: 'time'
Automation
- id: Ultima chiamata
alias: Ultima chiamata
trigger:
platform: time_pattern
minutes: /1
seconds: 15
condition:
- condition: template
value_template: '{{ states.sensor.sys_time.state == states.sensor.calc_last_time.state }}'
action:
- service: script.ann_lastpartenza
- service: script.ann_lastpartenza_remote
- id: Annuncio partenza
alias: Annuncio partenza
trigger:
platform: time_pattern
minutes: /1
seconds: 15
condition:
- condition: template
value_template: '{{ states.sensor.sys_time.state == states.sensor.calc_leave_time.state
}}'
action:
- service: script.ann_partenza
- service: script.ann_partenza_remote
- id: Annuncio warning
alias: Annuncio warning
trigger:
platform: time_pattern
minutes: /1
seconds: 15
condition:
- condition: template
value_template: '{{ states.sensor.sys_time.state == states.sensor.calc_warn_time.state
}}'
action:
- service: script.ann_warning
- service: script.ann_warning_remote
- id: Annuncio preparazione
alias: Annuncio preparazione
trigger:
platform: time_pattern
minutes: /1
seconds: 15
condition:
- condition: template
value_template: '{{ states.sensor.sys_time.state == states.sensor.calc_prep_time.state
}}'
action:
- service: script.ann_preparazione
- service: script.ann_preparazione_remote
Script
For each message, I have two different scripts one when I stay home and one when I stay out
The normal script when I stay home send a vocal message to my home assistant.
The script when I stay out to send the same message to my telegram.
ann_lastpartenza:
sequence:
- condition: state
entity_id: person.fabio
state: home
- service: script.send_vocal_message
data_template:
device: '{{states.input_select.stanza_fabio.state}}'
message: cosa What ci fai ancora a casa devi andare via subito
volume: 0.7
wait: 10
ann_lastpartenza_remote:
sequence:
- condition: state
entity_id: person.fabio
state: not_home
- service: notify.fabio_telegram
data_template:
message: cosa ci fai ancora a casa devi andare via subito
ann_partenza:
sequence:
- condition: state
entity_id: person.fabio
state: home
- service: script.send_vocal_message
data_template:
device: '{{states.input_select.stanza_fabio.state}}'
message: è ora di partire per {{ states.calendar.fabiofava70gmailcom.attributes.message
| replace('Rem.', '') }} Ci vorranno {{ states.sensor.viaggio.state }} minuti. destinazione {{ states.sensor.cal_location.state }}
volume: 0.7
wait: 15
- condition: numeric_state
entity_id: sensor.viaggio
above: 0
- service: switch.turn_on
entity_id: switch.cancello
ann_partenza_remote:
sequence:
- condition: state
entity_id: person.fabio
state: not_home
- service: notify.fabio_telegram
data_template:
message: è ora di partire per {{ states.calendar.fabiofava70gmailcom.attributes.message
| replace('Rem.', '') }} Ci vorranno {{ states.sensor.viaggio.state }} minuti. destinazione {{ states.sensor.cal_location.state }}
ann_warning:
sequence:
- condition: state
entity_id: person.fabio
state: home
- service: script.send_vocal_message
data_template:
device: '{{states.input_select.stanza_fabio.state}}'
message: Tra {{ states.input_number.tempo_di_warning.state | int }} minuti devi partire per {{ states.calendar.fabiofava70gmailcom.attributes.message | replace('Rem.', '') }}
volume: 0.7
wait: 8
ann_warning_remote:
sequence:
- condition: state
entity_id: person.fabio
state: not_home
- service: notify.fabio_telegram
data_template:
message: Tra {{ states.input_number.tempo_di_warning.state | int }} minuti devi partire per {{ states.calendar.fabiofava70gmailcom.attributes.message | replace('Rem.', '') }}
ann_preparazione:
sequence:
- condition: state
entity_id: person.fabio
state: home
- service: script.send_vocal_message
data_template:
device: '{{states.input_select.stanza_fabio.state}}'
message: Ti devi preparare tra {{ states.input_number.tempo_di_preparazione.state | int }} minuti devi partire per {{ states.calendar.fabiofava70gmailcom.attributes.message | replace('Rem.', '') }}
volume: 0.7
wait: 8
ann_preparazione_remote:
sequence:
- condition: state
entity_id: person.fabio
state: not_home
- service: notify.fabio_telegram
data_template:
message: Ti devi preparare tra {{ states.input_number.tempo_di_preparazione.state | int }} minuti devi partire per {{ states.calendar.fabiofava70gmailcom.attributes.message| replace('Rem.', '') }}
send_vocal_message:
fields:
device:
description: nome del dispositivo
example: soggiorno
message:
description: testo del messaggio da spedire
example: prova messaggio
volume:
description: volume del massaggio
example: '0.6'
wait:
description: secondi di attesa
example: '5'
sequence:
- service: media_player.volume_set
data_template:
entity_id: media_player.{{ device }}
volume_level: '{{ volume }}'
- delay:
seconds: 2
- service: tts.cloud_say
data_template:
entity_id: media_player.{{ device }}
message: '{{ message }}'
language: it-IT
options:
gender: female
- delay:
seconds: '{{ wait }}'
- service: media_player.volume_set
data_template:
entity_id: media_player.{{ device }}
volume_level: 0.5