History statistics component


#1

Hi,

For a few weeks, I’ve been thinking about what could be a real nice feature in Home assistant.

Since we log everything in the history, we could extract some data to display on the dashboard.


I’ve made a detailed example that speaks for itself :

  • Here is a picture of what you could see on the component

  • And here is the configuration that would be needed to setup the component :

      history_stats:
        - entity_id: device_tracker.android_boris
          state: 'home'
          duration: '{{ 3600 * 24 }}'
          end: '{{ as_timestamp(now().replace(hour=0).replace(minute=0).replace(second=0)) }}'
          name: Boris at home yesterday
          
        - entity_id: light.kitchen_lamp
          state: 'on'
          start: '{{ as_timestamp(now()) - 7 * 24 * 3600 }}'
          end: '{{ as_timestamp(now()) }}'
          name: Kitchen lamp, last 7 days
    

With the following parameters :

  • entity_id : The entity you want to track

  • state: The state you want to track

  • name: What to display on the frontend

  • start: When to start the mesure, timestamp

  • end: When to stop the measure, timestamp

  • duration: Duration of the measure, seconds

Only 2 of start, end and duration will be required, of course.


I’m not really a python dev, but if I manage to do something, I’ll share my github repo so you can contribute. Feel free to do the same.


What do you think about it ?
Could it be interesting ?
How would you name the component (I was thinking about History stats or History report) ?


#2

I think it’s a great idea; I’d call it history stats myself.

I think the toughest thing for an inexperienced user would be figuring out the start and end values; they’d have to be documented well enough so that someone not familiar or comfortable with time calculations would be able to understand it conceptually and make use of it. If you could do that, I could see it being a very popular sensor.


#3

Well, I started the development of this component.

Here is my repo if anyone want to contribute / review

By the way, I thought about a way to make calculations easier, I’ll add some aliases that will be transformed in more complex code, for example :

_NOW_ -> as_timestamp(now())
_TODAY_ -> as_timestamp(now().replace(hour=0).replace(minute=0).replace(second=0))

#4

Really nice piece of work @bokub! I do have one small problem: how can I put all the history_stats into 1 group? I would normally do that with the group option, but that is not working for the statistics component…


#5

Can you share an example config of what you’d like to do ?

I’ve grouped my sensors like this and it works great:

group:
  stats:
    name: Statistics
    view: no
    entities:
      - sensor.tv_yesterday
      - sensor.lights_today

Result:


#6

My configuration.yaml looks like:

  • platform: history_stats
    name: Eettafel on last month
    entity_id: light.eettafel
    state: 'on’
    end: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    duration:
    days: 30
  • platform: history_stats
    name: Keuken on last month
    entity_id: light.keuken
    state: 'on’
    end: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    duration:
    days: 30

And in groups.yaml I have:

stats:
name: Statistics
icon: mdi:cloud
entities:
- history_stats.light.eettafel
- history_stats.light.keuken


#7

The entity ids are wrong, they shoud be sensor.xxxxx.
Open yourhomeassistant:8123/dev-state to check what the real IDs are.

I guess they should be sensor.keuken_on_last_month and sensor.eettafel_on_last_month, but I’m not sure about it.


#8

Thanks @bokub! It’s working now thanks to your guidance.


#9

Could it be that after the update of last week the component doesn’t update anymore? Values do not appear to change any longer…


#10

It’s a bug, fixed but not merged into the master branch yet. I guess you’ll have to wait until the next major version.


#11

Will be testing it in my custom_components folder. Thanks for the notification!


#12

@bokub Could you take a look at this pull request and give me your thoughts?


#13

Hi Ryan,

As Pascal Vizeli said, you should not implement friendly names on components, because that’s something managed by the platform.

Plus, I don’t see why you need a friendly_name option when the name option does the exact same thing! Or am I missing something ?


#14

Thank you for the input. For me, and maybe I am just doing things wrong, I look to keep the identifier more technical and friendly_name more friendly. I find using customize: is not consistent when some components allow friendly_names in the config.

From the command line switch component: (which allows friendly names in the config)

When set, the friendly_name had been previously used for API calls and backend configuration instead of the object_id (“identifier”), but this behavior is changing to make the friendly_name for display purposes only. This allows users to set an identifier that emphasizes uniqueness and predictability for API and config purposes but have a prettier friendly_name still show up in the UI. As an additional benefit, if a user wanted to change the friendly_name / display name (e.g. from “Kitchen Lightswitch” to “Kitchen Switch” or “Living Room Light”, or remove the friendly_name altogether), he or she could do so without needing to change existing automations or API calls. See aREST device below for an example.

Perhaps I need to reevaluate my config files and move all my friendly names to the customize component.


#15

Hi all,

there is a way to show values on base 60.

For example the sensor reports 1.74h while I prefer 1h 44m (show as value).

Thank you in advance


#16

Hi @Jokerigno
The state of the component has to be a number, the way this number is formatted has to do with home assistant and not the component itself.

However, I have added an attribute called value which is a nicely formatted version of the state. You can display it by clicking on the component card in the UI.

If you want to display this value without the need of a click, try using the Template sensor component with the following option:

value_template: '{{ states.sensor.my_sensor.attributes.value }}'

You can then hide the original history_stats component


#17

It works like a charm!

Thank you!


#18

Hy, Jokerigno

I try to change 1.74h while I prefer 1h 44m (show as value) but is dont work. Pls can you show your config.yaml.
thx a lot


#19

Voilà!

  • platform: history_stats
    name: Marco a casa oggi
    entity_id: device_tracker.xxxxxxx
    state: 'home’
    start: '{{ now().replace(hour=8).replace(minute=0).replace(second=0) }}'
    end: ‘{{ now() }}’

  • platform: template
    sensors:
    marcoacasaoggi:
    friendly_name: 'Marco a casa oggi’
    value_template: ‘{{ states.sensor.marco_a_casa_oggi.attributes.value }}’


#20

Thx a lot
Its work