There is likely a thread or a simple solution to my question, and I am struggling to word it correctly.
The Reason
My wife and I both work jobs where we have a set start time and a set end time, however, the end time can vary +/- 30 minutes depending on conditions, and then our commute home can vary as well. Because of this, it makes it difficult to use fixed time triggered or zone triggered automations.
I want to use this data to turn the house from eco-mode to comfort-mode.
The Question
I would like to create an automation/script/sensor that can time stamp the time we each leave work and the time we arrive home, each day, but only if we left work and then arrived home, as I do not want it to record us leaving work to get lunch or run an errand. I would like this to be able to store 5 days worth of timestamps each, which I can then take the average to determine an average home arrival time. I would need this not to collect data on the weekends and only on weekdays.
Yes I could set a fixed time to trigger the automation based on an average on
data that I collect. But doesn’t that defeat the purpose of smart home
automations? Why wouldn’t I just use a programmable thermostat.
Information
Things I already have setup:
Waze Travel Time Integration
Geocoded locations
Zones
I am not entirely sure this is even possible but I wanted to at least ask.
I understand I can trigger the automation to run when I leave my work zone, but without traffic and all green lights, my commute is only 15 minutes, which is not enough time to heat or cool my home to a comfortable temperature. By using the average home arrival time, I could apply a negative 30 minute offset to allow sufficient time. I also like the idea of having an average that changes based on the last 5 days, because some seasons are busier at work and require us to stay later.
I can help you with that small part. Given a list of datetime strings (containing different days and times in UTC), the following template reports the average (local) time.
The challenge is that you will have datetime strings representing different times on different days. To find the average time, we have to first “normalize” the datetime strings to be all on the same day.
Steps it performs:
Datetime string to a Unix timestamp.
Timestamp to a time string.
Time string to a datetime object using today’s date.
Thank you!
Although this is probably far more complicated than it needs to be, I was thinking it may be possible to create 5 individual inputs or sensors for the data, and conditionally have the data sent to an input/sensor based on the day of the week. Then remove the date from the equation. I was then thinking I could convert the time to minutes, so 17:53, would become 1073, then average the number of minutes that passed in the day before I arrived home, convert it back to hours, and then I’d have my answer…
Again, I know this is crazy complex, but I work with excel on a daily basis, so this is how my brain works.
You can create a single Trigger-based Template Sensor that stores a running list of the last 5 arrival times (or last 10 or whatever you want) in its attributes. Here are three examples of maintaining a running list of data.
Assume we have a Trigger-based Template Sensor called sensor.coming_home, and it has an attribute named arrivals containing a list of the last 5 arrival date and times. The template I posted above can reference that list like this:
I do something kind of similar by passing the arrival time to Google Sheets then processing the data there and passing back the desired values to HA using a POST command derived from this Youtube video. I cobbled my set up together about 3 years, so it is more complicated than it could be using some of the methods Taras has shown above… but I haven’t gotten around to converting it; and it is nice to have a sheet full of historical data if I decide I want to experiment with additional statistical methods.
For your situation I would probably start with a trigger-based template sensor that holds the time when you leave your work zone. Then I would set up a second trigger-based template sensor to store when you arrived home.
Sample Sensor Configurations
template:
- trigger:
- trigger: zone
entity_id: person.example
zone: zone.work
event: leave
condition:
- alias: Check if this is a Workday
# You can use a State condition with a Workday sensor or a Time condition with selected weekdays.
# I prefer a Workday sensor since it will reject holidays as well as weekends
condition: state
entity_id: binary_sensor.person_a_workday
state: "on"
- condition: time
# Use reasonable values for when you are likely to leave work to go home.
after: "16:00:00"
before: "20:00:00"
sensor:
- name: Person A Left Work
state: "{{ now() }}"
device_class: timestamp
- trigger:
- trigger: zone
entity_id: person.example
zone: zone.home
event: enter
condition:
- condition: state
entity_id: binary_sensor.person_a_workday
state: "on"
- condition: time
after: sensor.person_a_left_work
before: "20:00:00"
sensor:
- name: Person A Home from Work
device_class: timestamp
state: |
{% if this is not defined or (this.state | as_datetime | as_local < today_at()) %}
{# This If clause, combined with the general conditions should capture
the first time the person arrives home after leaving work on a workday #}
{{ now() }}
{% else %}
{{ this.state }}
{% endif %}
I built this some time ago, it looks like it does what you need. You can decide which events you want to store, how many weeks, etc. It was built for averages per weekday, but you can also get the average for all days you chose to record for one week: