Templating: History Stats

Morning Community!

So I came across the history_stats component and built a sensor using documented examples, in this case, it’s to track states of a given device (in my case, device_tracker) and it appears that it’s not consistent. So I crawled around the forum looking for anything I could around history_stats and only one person had an issue due to typo whereas others had it working then stopped.

My setup:

HA 0.99.3 venv
history and recorder are at defaults (for the most part) in configuration.yaml – I can see the device_trackers I want in the History tab along with all the states it has. Using MySQL as a DB backend.

history:
  default: info
recorder:
  db_url: !secret mysql_url

And I have my device_tracker that I’m trying to pull history stats from:

This tracker has one of several states that I track: Home, Away, Work (Office), etc.

So I created a history_stats sensor which appears to be working for all my states except “Away”. This means “Work (Office)”, “School” and even “Home” all work as expected but “Away” doesn’t. Here’s my config around the previously mentioned sensors:


  - platform: history_stats
    name: Chris Time Home
    entity_id: device_tracker.life360_chris_nodered
    state: 'Home'
    type: time
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'

  - platform: history_stats
    name: Chris Time Away
    entity_id: device_tracker.life360_chris_nodered
    state: 'Away'
    type: time
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'

  - platform: history_stats
    name: Chris Time Office
    entity_id: device_tracker.life360_chris_nodered
    state: 'Work (Office)'
    type: time
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'


  - platform: template
    sensors:
      chris_time_away_today:
        friendly_name: 'Away'
        value_template: '{{ states.sensor.chris_time_away.attributes.value }}'

  - platform: template
    sensors:
      chris_time_at_home_today:
        friendly_name: 'Home'
        value_template: '{{ states.sensor.chris_time_home.attributes.value }}'

  - platform: template
    sensors:
      chris_time_at_work_office_today:
        friendly_name: 'Work (Office)'
        value_template: '{{ states.sensor.chris_time_office.attributes.value }}'

I know the sensor is case-sensitive and “Away” is “Away” in the history for the device_tracker. Home and Work (Office) and other various states work without issue but I cannot for the life of me figure out why “Away” is not working and thinking perhaps I can’t see the trees through the forest (although I just copied the sensor and adjusted the parameters for each, even the working ones.)

Can anyone see where my mistake is? Or can explain why this one state doesn’t seem to match when the others do without issue?

As always, appreciate the help and support!

-Chris

Are you sure it’s away instead of not_home?

1 Like

Yes, it’s a custom sensor that changes not_home to Away or home to Home. For all other zones, it simply labels them as “Work (Office)” etc.

Ah makes sense, sorry. I could have probably figured that out since home is Home in your example.

Thought you were using a template sensor that changes the state, but looking again it’s the device tracker entity itself.

However, it appears you’re smarter than I am :slight_smile:
I have a custom automation that tracks the device_tracker and change not_home to Away and in the history for the device tracker, I see “Away”. I decided on a whim to try your suggestion of using “not_home” and…

image

I guess that even though I change the “friendly name” of the tracker, I guess it still logs historically as “not_home”.

Much appreciated my friend!

No. The under the hood state is still home and not_home. This is for the custom library and the built in life365.

1 Like

Thought I was pretty well versed in HA semantics/etc but this one got me. I love this community, even when I’m being obtuse, someone clearly shows how obtuse I am :slight_smile:

Everything in the UI is translated to the user unless it’s set by the user. So to you it appears as “Away” to someone else in another region it will be translated for them.

Just for completeness, here’s the automation I was referencing that changed not_home to Away:

- alias: Presence NodeRed
  trigger:
    - platform: state
      entity_id: device_tracker.life360_chris_nodered
    - platform: state
      entity_id: device_tracker.life360_val_nodered
    - platform: state
      entity_id: device_tracker.life360_logan_nodered
  action:
    - service: input_select.select_option
      data_template:
        entity_id: >
          {% if trigger.entity_id == 'device_tracker.life360_chris_nodered' %}
            input_select.chris_status_dropdown
          {% elif trigger.entity_id == 'device_tracker.life360_val_nodered' %}
            input_select.val_status_dropdown
          {% else %}
            input_select.logan_status_dropdown
          {% endif %}
        option: >
          {% if trigger.entity_id == 'device_tracker.life360_chris_nodered' %}
           {{ states.device_tracker.life360_chris_nodered.state|replace('not_home','Away')|replace('home','Home') }}
          {% elif trigger.entity_id == 'device_tracker.life360_val_nodered' %}
           {{ states.device_tracker.life360_val_nodered.state|replace('not_home','Away')|replace('home','Home') }}
          {% else %}
           {{ states.device_tracker.life360_logan_nodered.state|replace('not_home','Away')|replace('home','Home') }}
          {% endif %}

Now I’m curious…what is your use case for this automation? I’m not following its purpose.

I change the state identifier to a specific string to make it easier to work with in my NodeRed flow :slight_smile: and it makes it easier for the family to understand.

Here’s a snippet of my flow:

you can have a look at this:

used to work nicely, not sure if it is still supported under Lovelace (the _stateDisplay isnt) but it could be an elegant solution to your quest

Much appreciated, will definitely take a look.

In case the next question is why don’t I use the built-in zones – it’s because it’s a radius, my zones don’t work well with a radius and I found my family GPS-wise would drift in and out of a radius vs geometric-shaped zones. It’s a small thing but it bugged me to no end and this was my solution :slight_smile:

Hey @chrisw,

Sorry to revive this post, but I saw that your card show the time well formatted in hours/minutes, can you share how did you manage to do this template?

Thank you!

History stats sensors have a value state attribute with the time formatted how you see in that screenshot. Use a template sensor to grab that value using the state_attr() method.

Hmm not sure if i understand it correctly. The way I got it is to create another template sensor and convert the hist_stats sensor ?

My hist_stats:

- platform: history_stats
  name: TV ligada hoje
  entity_id: media_player.tv_samsung
  state: 'on'
  type: time
  start: '{{ now().replace(hour=0, minute=0, second=0) }}'
  end: '{{ now() }}'

The way i got it working is like this:

- platform: template
  sensors:
    tv_on_time_today:
        value_template: "{% set hours = states('sensor.tv_ligada_hoje') | float %} 
        {% set minutes = ((hours % 1) * 60) | int %}  
        {% set hours = (hours - (hours % 1)) | int %}  
        {{ '%02ih %02im'%(hours, minutes) }}"

But i feel i’m doing too much work and there is a simple way of doing it that i don’t know about.

1 Like

Nope, that’s the right amount of work. Welcome to converting time club.

EDIT: You could optimize the 3rd line using int division

{% set hours = hours // 1 %}  

or be super lazy and put it right in your format

        {% set hours = states('sensor.tv_ligada_hoje') | float %} 
        {% set minutes = ((hours % 1) * 60) | int %}
        {{ '%02ih %02im'%(hours // 1, minutes) }}
1 Like

Cool. Thanks @petro!

I finnally understand what @Tediore said.

- platform: template
  sensors:
    tv_on_today:
        value_template: "{{state_attr('sensor.tv_ligada_hoje','value')}}"

It turns out that the value from the hist_stats already gives me the time the way i want.

Thanks a lot!! :grin:

I use the custom addon (HACS) called “Flex Table - Highly customizable, Data visualization”

1 Like