How to inspect history_stats?

I’m looking to build an automation for my boiler switch, which involves checking its “on” durations/the last time it was “on” in the last X times. I’ve History Stats, but I always like to inspect things in the Template in Developer Tools. However, when I do:

{{ states.history_stats|length }}

I get 0. Which means this object doesn’t contain any members. How can I inspect it anyway?

History_stats is the integration, it creates sensors, so you need to insoect rhe sensors you created, there are now history_stats.xxx entities, that’s why your command shows 0.

Thanks @Burningstone, however I’m not sure I follow.

Do you mean if I have a device with id lumi_lumi_weather that exposes entity lumi_lumi_weather_temperature, I can do:

{{ states.history_stats.lumi_lumi_weather_temperature }}

or

{{ states.history_stats.lumi_lumi_weather }}

Because both return None.

No, again there is no domain called history_stats, there are no entities called history_stats.xxx.

states.history_stats will always be empty, you can not inspect the history of a device with the template editor.

What is your end goal?

I’m sorry I misunderstood what you explained, but I don’t understand your frustration, I guess your typo threw me off just a little bit?

there are now history_stats.xxx entities

I stated my end goal in the OP. I want to check history_stats, given device sensor.entity_id, I want to inspect its history to see if I should perform a certain action. I wanted to see what kind of data is available through the template editor - thanks for answering my question.

I’m not frustrated at all :slight_smile:

What typo?

Could you describe a specific use case? I’m sure there is a way to do what you want.

You wrote there are now history_stats.xxx, which made me look further in there. I guess you meant there are no

Anyway:

I have a smart boiler successfully exposed in HA. I want to create the following automation (for example):

If:

  1. I’m arriving home
  2. The temperature outside is below 15 Celsius.
  3. (This is where it’s relevant) The boiler hasn’t been on for more than 1 hour in the last 4 hours

Then:

Turn on the boiler.

I know this is probably possible, but having the ability to debug it would be worth its weight in gold.

Oh, I’m really sorry, I mean’t NO

I don’t understand what you want to debug here? Just click on the entity and you see the history for this entity. If you want more dou can also always use some SQL commands to extract the necessary data from the database.
Personally I use grafana to display history of my entities.

For this specific use case you can create a history_stats sensor for your boiler, with end = now and start = now - 4 hours and you should be good to go.

1 Like

It’s OK. I appreciate your help.

It’s always useful to see things in the editor than blindly writing in YAML files and hoping what I’m doing makes sense. It’s sometimes also useful for poking and discovering new features.

What if I want to check if it hasn’t been on for more than an hour in the last 4 hours?

I know grafana, it would be interesting if it opens new possibilities…

Create the history_stats sensor:

sensor:
  - platform: history_stats
    name: Boiler ON in last 4 hours
    entity_id: switch.xxxxx
    state: "on"
    type: time
    duration:
      hours: 4
    end: "{{ now() }}"

If the sensor is smaller 1 → boiler has not been ON for 1 hour in the last 4 hours.

1 Like

Thanks @Burningstone. Maybe I’m being slow but what this will create is basically a sensor that looks at the last 4 hours, and whose unit is hours? And THIS is the conditional I use in another automation?

Exactly, it shows the time in hours that the switch.xxx has been in the state ON in the last 4 hours.

Yes, something like

{{ states('sensor.boiler_on_in_last_4_hours') | float < 1 }}
1 Like

Appreciate it Burningstone, and actually what I get in states is exactly the kind of debugging I needed. Cheers!

1 Like

@Burningstone one last question: can I give the sensor a custom name that doesn’t depend on its “Name”, in the future I may want to change the durations and the title of this sensor, but I’d like it to keep its ID. Is that possible?

Yes, you can use customization.

Something like:

homeassistant:
  customize:
    sensor.xxxx:
      friendly_name: "Boiler on in last 4 hours"
      icon: mdi:water-boiler
1 Like

But in this case the “name” field in the sensor in your previous post still determines the ID. What I want is the opposite. Should I give it a name like “boiler_on_hours_last_x_hours” and just override in the UI or through customize as you show?

Yes, this will be the entity_id. This is the one you want to stay, because you use it in scripts/automations etc. Then customize the friendly name, which will be shown in the frontend, through the UI or as I showed above.

1 Like

Thanks @Burningstone! learned a lot here. Now just need to understand how I can manipulate the value reported (originally in hours) if I want to display minutes in a glance card…