Tracking time a sensor is on/true/state

image

This sensor tracks when my phone is connected to the car, I’d like to use this data to track my commuting time. Basically I’d like to mimic the utility meter functionality (having a daily, weekly, monthly, yearly count).

I guess I could use the utility meter platform but then I would need to be able to measure the minutes the sensor is connected to the car. Is there an easy way to do this? Or perhaps is there a better way to pull out this information from the sensor?

Sounds like the history statistics sensor

Could you give an example on how to use it? I don’t see how I can grab the time duration. To clarify:

image

See the red boxes, I would like to get the duration of those. The last one is enough as well.

So in the end, I would like to extract from this graph that the sensor’s last true state lasted for 21 minutes.

Here is an example which shows the total time the input boolean ‘input_boolean.sleep_mode’ was in the state ‘on’ in the last 7 days.

sensor:
  - platform: history_stats
    name: Time Dimitri sleeping
    entity_id: input_boolean.sleep_mode
    state: 'on'
    type: time
    start: '{{ as_timestamp( now().replace(hour=0).replace(minute=0).replace(second=0) ) - 7 * 86400 }}'
    end: '{{ now() }}'

Thanks, however that’s not what I need.

What I ideally would want is as soon as the sensor goes from on to off state, the value (in the last case) of a given sensor or attribute is “21 minutes”. In such a case I can e.g. send a push notification to my phone with a msg on how long the drive was.

To make matters more complicated :smiley: , as a second thing, I actually only want to track when my phone is connected to the car, not e.g. to my headphones (so also based on the ‘last device name’ or mac)

I might have come up with a solution for you…

try this:

input_datetime:
  device_connected:
    name: Device Connected to Car
    has_date: true
    has_time: true
  device_disconnected:
    name: Device Disconnected from Car
    has_date: true
    has_time: true
  device_delta:
    name: Device Connected Delta
    has_time: true
    has_date: false
    
automation:
    - alias: set time connected
      trigger:
        - platform: state
          entity_id: input_boolean.bool_15
          to: 'on'
      action:
        - service: input_datetime.set_datetime
          data_template:
            entity_id: input_datetime.device_connected
            datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
    - alias: set time disconnected
      trigger:
        - platform: state
          entity_id: input_boolean.bool_15
          to: 'off'
      action:
        - service: input_datetime.set_datetime
          data_template:
            entity_id: input_datetime.device_disconnected
            datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
    - alias: set time delta
      trigger:
        - platform: state
          entity_id: input_boolean.bool_15
          to: 'off'
      action:
        - delay: '00:00:03'
        - service: input_datetime.set_datetime
          data_template:
            entity_id: input_datetime.device_delta
            time: "{{ (as_timestamp(states.input_datetime.device_disconnected.state) - as_timestamp(states.input_datetime.device_connected.state)) | timestamp_custom('%H:%M:%S', false) }}"

this will result in an entity that will contain the time between connecting and disconnecting. Obviously you need to put your own device triggers in.

Then here is where the magic happens…:wink:

you need to install the “variable” custom component from here.

then add the following code:

variable:
  last_delta:
    value: 'Not set'
    restore: true
    attributes:
      icon: mdi:map-marker
      name: 'Last Trip Time'

automation:
  - alias: 'Update Last Trip Time'
    trigger:
      - platform: state
        entity_id: input_boolean.bool_15
        to: 'off'
    action:
      - delay: '00:00:05'
      - service: variable.set_variable
        data:
          variable: last_delta
          attributes_template: >
            {
              "history_1": "{{ variable.state }}",
              "history_2": "{{ variable.attributes.history_1 }}",
              "history_3": "{{ variable.attributes.history_2 }}",
              "history_4": "{{ variable.attributes.history_3 }}",
              "history_5": "{{ variable.attributes.history_4 }}",
              "history_6": "{{ variable.attributes.history_5 }}",
              "history_7": "{{ variable.attributes.history_6 }}",
              "history_8": "{{ variable.attributes.history_7 }}",
              "history_9": "{{ variable.attributes.history_8 }}",
              "history_10": "{{ variable.attributes.history_9 }}"
            }
        data_template:
          value: >
            {{ states.input_datetime.device_delta.state }}

That code will end up giving you a sensor called “Last Trip Time” (variable.last_delta) and has a running history of the last 10 trips as attributes.

I tested it out by cycling my boolean and it worked for me.

ex2

3 Likes

Make one automation which is triggered by sensor-“on”, another automation is triggered by sensor-“off”. Then in the “on”-Automation you check attributes.last_triggered of the “off”-Automation and vice versa.

Hi Looks great. Can you tell me where to put both codes? First one in templates in Settings and second one in configuration.yaml? I get an error with “variable”. I have the addon installed…

the variable integration (it’s an integration not an add-on) has been changed recently so the above code will no longer work.

you will need to go the variables repository to get the instructions for new way to configure it.