Update a value based on the time between two states

Looking for help comparing time between two states.

I want to know how long it took me to get from one zone to another zone.

Ultimatlly I’d like to send three values
Time from zone x to zone y
Friendly name of zone x
Friendly name of zone y

to an ifttt trigger that updates a good spreadsheet.

Basically its a commute tracker. I have the IFTTT pretty much worked out. Basically it’s a “log data to google sheets” and a web hook.

I think figuring out how to compare previous values will be useful for other automations as well. Thanks in advance for any help

You’ll need 2 automations and sensor(s) that can store information like MQTT.

The automation would watch a known_device and track it’s movements. When the trackter transitions from zone x to away, set the MQTT sensor to a as_timestamp(now()). This will store a integer inside that MQTT sensor by posting to the topic.

Then your 2nd automation would watch away->zone y. When that transition occurs, you subtract the mqtt sensor value from as_timestamp(now()). This will give you a value in seconds that it took you to get from zone x to zone y. Convert that to hours, minutes, seconds.

This can be done dynamically using a trigger that does not specify from: and to:. You’d have to build a condition to ignore ‘away’. Doing it this way would only require 1 automation, but you’d have to preload the mqtt sensor on startup with as_timestamp(now()), and it’s first trigger would send you crap that you don’t care about because the timestamp is from home_assistant startup.

Basically, this can be done, but it won’t be easy. I can help with your automations once you start posting yaml and have MQTT setup.

I’m still trying to understand what I need MQTT for. I do see a lot of mention of it, and understand that I will eventually need it, but it seems like this is for interfacing with items that are outside of home assistant? I’ve looked around trying to find a “not too technical” guide explaining what MQTT does overall for HA.

Like in my case, I can see the history of my presence sensor. Since I have zones set up in my configuration.yaml, it changes from home to zone1, to zone 2, to home, etc. I can see time stamps in history for exactly when that happened. Is there no way to use that data without using mqtt. Again, not opposed to using it, just want to try to use HA for as much as I can. I appreciate the help so far. So many people on these forums say things like “HA is so easy, I had it up and running in 10 minutes” Well, its not “easy”, but it is powerful. :slight_smile: It gets “easier” the more I play around.

Like the trigger would be left zone, then look at time that I entered the previous zone, then do something with that data.

You need something that will store a value between shutdown and startup. Home assistant does not do that. You could make a template sensor to store the info, but if you restart, its wiped clean. I typically restart once a week.

1 Like

I also remember why I said MQTT as well. WIth a template sensor, you cannot push data to it. It collects data actively. With what you want to do, you can only use an automation. You’d need to be pushing information from the automation to some sort of storage, the only options are MQTT or an Input_number. Unfortunately, I don’t think the Input_number can handle a number that high. when you use as_timestamp, it returns a time stamp in seconds from january 1st 1970 at midnight. As of this post, that number is 1524666614.01. I don’t know if an input_number can handle that. Also, what you want to do isn’t normal, I think you may be the first who wants this information. That’s probably why this seems complicated.

I feel like this seems so simple, but apparently is complicated. If I go to the UI and look at state history for my presence sensor, I can see where I was all day. I even restarted HASS several times today, but I see the times from overnight, showing me at home, and how I see I am at work. I can see exact timestamps of this under state. Is state history not accessible via automations. Not as a trigger of course, but as a value? Seems like such as waste to keep all that data, and not make it easily accessible. I will say when I first loaded Home assistant I did not even know about the state history, but when I saw it, I thought to myself, hmmm, this might be useful for something. :slight_smile:

Thanks for your patience, and help

You can attempt to get the history statistics component to work for you.

It looks like it may do what you want. I’m not sure how quick it updates. Also, it appears as if it would only get durations of you being places. So again, this won’t work for the areas between your zones… which is what you want.

1 Like

Thanks, this looks interesting. Maybe I could have a binary sensor change states when I enter a zone to on. Then check the history of that sensor. when ever the state changes.

so something like this

I leave any zone --> automation Binary sensor turns on (or toggle)
I enter any zone --> automation binary sensor turns off (or toggle)
Automation --> trigger binary sensor turning off
Automation uses history sensor logic on binary sensor to see how long it was on.

Something like that. So that would give me my commute time, but I’d still want to capture which zone turned it on and which zone turned it off.

Pretty exciting. Thanks for the help.

This is code I use for slightly different reason than you want to use it for. But look at my template. In my case it checks the garage door opening in the last 2 min, but you could certainly use this method to compare the times from leaving/entering a zone.

- alias: Someone arrived home
  trigger:
    - platform: state
      entity_id: binary_sensor.foyerpir1
      to: 'on'
  condition:
    - condition: template
      value_template: '{{as_timestamp(now()) - as_timestamp(states.group.family.last_changed) < 120 }}'
    - condition: template
      value_template: '{{as_timestamp(now()) - as_timestamp(states.switch.garage_door.last_changed) < 120 }}'
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'

  action:
    - service: light.turn_on
      entity_id: light.linear_wd500z1_wall_dimmer_switch_level_6_0
1 Like

A sensor that will tell you the time between the state change of two different entities, You can use the templates page on the front end menu to check the value_template you need.

sensor:
  - platform: template
    sensors:
      time_difference:
        friendly_name: "Time between lights"
        value_template: {{as_timestamp(states.light.outside_side_light.last_changed) - as_timestamp(states.light.kitchen_light.last_changed) }}

ok, still trying to figure the logic, but maybe?? a start for the trigger actions?

- alias: entererd a zone
  trigger:
    - platform: state
      entity_id: device_tracker.pauls_iphone
      to: 'home'
    - platform: state
      entity_id: device_tracker.pauls_iphone
      to: 'zone1'
    - platform: state
      entity_id: device_tracker.pauls_iphone
      to: 'zone2'
  condition:

Again, if you use an automation for the trigger, you cannot throw that information in the @RobDYI’s example. Template sensors pull the information. Automations push and you cannot push to a template sensor. The example @ericleejoe is talking about uses the as_timestamp() that I was talking about. Your last piece of the puzzle is storing that number somewhere for the next time the automation fires. x.x.last_changed will not work because it’s currently changing and that number will be now. Even if you attempted to grab the information from the sensor that is causing the trigger, that will already be updated because it’s being used as the trigger.

or perhaps create two sensors so you can track a third

sensor:
  - platform: template
    sensors:
      pauls_home:
        value_template: "{{ is_state('device_tracker.pauls_iphone', 'home') }}"
  - platform: template
    sensors:
      pauls_work:
        value_template: "{{ is_state('device_tracker.pauls_iphone', 'work') }}"
  - platform: template
    sensors:
      time_home_to_work:
        friendly_name: "Time between home and work"
        value_template: {{as_timestamp(states.sensor.pauls_home.last_changed) - as_timestamp(states.pauls_work.last_changed) }}

Yeah that’s a good solution! The only drawback is that you’d have to make a sensor for every zone and every person, but it would get the job done.

Ok, I’ve left this on the back burner and I think I have it down, using simliar logic just a couple questions
So two automations
Two Sensors
One called current_location
One called previous_location

Automation One

- alias: I've left
  trigger:
  - platform: state
    entity_id: device_tracker.pauls_iphone
    to: away
action:
  set value of previous_location to be current location

2nd automation

- alias: I've left
  trigger:
  - platform: state
    entity_id: device_tracker.pauls_iphone
    from: away
action:
  set value of current_location to be the state of device_tracker.pauls_iphone

The do the math in the value template
friendly_name: “Time between home and work”
value_template: {{as_timestamp(states.current_location.last_changed) - as_timestamp(states.previous_location.last_changed) }}

This would work for any commute between two identified locations.

I just don’t quite know how to set the state/value of one item to another.

I’m not sure this is accurate. In theory, if you have the recorder enabled (which will be automatically enabled if you have history enabled), then components like input_number have their values saved when HA stops and reloaded when HA restarts. This is documented here:

and for input_number’s, here:

From what I see in the implementation, both input_numbers and timestamps are Python floats, so there should be no problem saving a timestamp in an input_number, if that’s what you wanted to do.

For my example I wanted to store an ascii value for previous and current location. My presence sensor currently show Home, work1, work2, school, etc when I am in those zones. Then I just want to subtract the time stamps from last state change of those two variables.

I guess a better way to look at this at least for me is

#1 - Can I set a sensor, input, etc to be = the current state of another sensor, in this case the state would be the zone of my location sensor
#2 - there was an example above about subtracting last change from a sensor. I don’t need to store it anywhere, right?

I think it would be easiest just to create a sensor for each zone as I mentioned above. That way, the state change of that sensor will act a time mark of when you left or entered. and the calculation of time between zone is simply

{{as_timestamp(states.sensor.pauls_home.last_changed) - as_timestamp(states.pauls_work.last_changed) }}

you could create a template sensor from above to find the time since leaving the last zone (without knowing which zone) by looking at the time stamp of all the zone sensors you created and choosing the smallest. The value of the time since leaving sensor would be timestamp of now less the time stamp of the smallest of the zone sensors last changed attribute.

How about something like this:

- alias: Leave Zone 1
  trigger:
    platform: zone
    entity_id: device_tracker.pauls_iphone
    zone: zone.zone1
    event: leave
  action:
    service: input_number.set_value
    data_template:
      entity_id: input_number.start_commute
      value: '{{ as_timestamp(now()) }}'
- alias: Enter Zone 2
    platform: zone
    entity_id: device_tracker.pauls_iphone
    zone: zone.zone2
    event: enter
  action:
    service: ifttt.trigger
    data_template:
      time: '{{ as_timestamp(now()) - (states("input_number.start_commute")|int) }}'
      zone1: '{{ state_attr("zone.zone1", "friendly_name") }}'
      zone2: '{{ state_attr("zone.zone2", "friendly_name") }}'

The time would be in seconds. If you want something else, then adjust the template accordingly.

1 Like