Hourly history stats

I currently have the following to give me daily heating stats

  - platform: history_stats
    name: Heating ON Today
    entity_id: sensor.thermostat_state
    state: 'heating'
    type: time
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'

How can i change this to give me hourly stats, 12-1, 1-2 etc so I can graph it like the Netatmo graph showing how many minutes each hour the heating is ON

remove that to get the past hour

To get the hours of the day…

Midnight to 1 am…

    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now().replace(hour=1).replace(minute=0).replace(second=0) }}'

1 am to 2 am…

    start: '{{ now().replace(hour=1).replace(minute=0).replace(second=0) }}'
    end: '{{ now().replace(hour=2).replace(minute=0).replace(second=0) }}'

etc

1 Like

Thanks, is there any quick way of doing hourly without specifying 24 start and finish times? I’d like a graph something like this one on the Netatmo cloud

Did you find a solution for this ?
I’m looking exactly for the same function :slight_smile: I would like to display the time the heating was on per hour in grafana… the history state sensor is a good start but I’m really looking for the per hour option.

No, I never got this sorted

Hi Colfin22,

I found a solution to track how long the heating was on every hour.

I have the history_stats sensor like you and they convert it to minutes with a template sensor.
Like this :

  - platform: template
    sensors:
      heating_history_stats_woonkamer_minutes:
        friendly_name: "Heating on today woonkamer minutes"
        unit_of_measurement: 'min'
        value_template: "{{ states('sensor.heating_history_stats_woonkamer') | float * 60 | round() }}"

Then I track them in a utility_meter per hour this show the number of minutes the heating was on every hour.

utility_meter:
  heating_on_history_woonkamer:
    source: sensor.heating_history_stats_woonkamer_minutes 
    cycle: hourly

Next step is to create a nice grafana graph but I’m still figuring that one out :slight_smile:

Maybe this could already help you achieve what you are looking for.

Kind regards,

3 Likes

@colfin22 @petro jfi, instead of

  {{ now().replace(hour=0).replace(minute=0).replace(second=0) }}

you can use

{{ now().replace(hour=0, minute=0, second=0) }} 

:wink:

that hasn’t always worked :wink:

what do you mean? it’s even not HA.

it wasn’t properly extended to jinja before :wink:

Like your thinking bro

I be doing that way when i get off shift

If you use influxdb and grafana, you can do this easily… and you can store/analyse several years worth of data on a normal hard drive without having slow downs. I know that isn’t ‘history stats’. So OT, but figured I’d mention it as a solution to see the desired graph.

A hint… use field(value)/integral/cumulative_sum/math(/1000) on the heat or cool entity to see cumulative on time in seconds (influx sums over milliseconds)… /60000 for minutes, /3600000 for hours. This is also useful for graphing kW-hr from power data, and other integral type graphs you may want.

How do you get the graphs in influx/grafana without the history graphs ? I have both running but wasn’t able to get the hourly data from the climate sensors.

History and influxdb are totally independent of each other afaik. History graphs usually depend on the stock sqlite database, or mariadb. Influxdb and grafana can work without any of those configured.

Edit… here’s a pic of my influxdb notes if they help :wink:

Hey I just played in grafana and see the useful state under the entity “thermostat_mode”, in the field “hvac_action_str”. Unfortunately this isn’t binary data, but a string (heat/cool/idle/off).

My heater has been turned on and off, and cycled between idle and heat a few times today. Unfortunately when it is turned on/off it spits out a 2 or a 3… normal idle/heat shows binary (1=heat, 0=idle). So those other values are screwing up the integral. Perhaps we could prod the ha dev’s to sort of go back on the latest climate component changes, and bring back some separate cool/idle and heat/idle binary entities for us to use here.

Otherwise, yeah it looks like maybe a template may be needed after all to graph on times in grafana. Not the end of the world, but imho not as ideal as having the entities added with the component. :confused:

I played around with the dev tools and brewed up these 3 templates that work well in my configuration.yaml:

binary_sensor:
  - platform: template
    sensors:
      heater_status:
        friendly_name: "Heater"
        value_template: >-
          {{ is_state_attr('climate.thermostat_mode', 'hvac_action', 'heating') }}

  - platform: template
    sensors:
      cooler_status:
        friendly_name: "A/C"
        value_template: >-
          {{ is_state_attr('climate.thermostat_mode', 'hvac_action', 'cooling') }}

  - platform: template
    sensors:
      hvac_fan_status:
        friendly_name: "HVAC Fan"
        value_template: >-
          {{ is_state_attr('climate.thermostat_mode', 'fan_action', 'Running') }}

You can use these with grafana to easily get hours of usage, duty cycle, etc for all 3 parts of an HVAC system.

Enjoy :smiley:

1 Like

Those templates can be simplified to:

binary_sensor:
  - platform: template
    sensors:
      heater_status:
        friendly_name: "Furnace Status"
        value_template: "{{ is_state_attr('climate.thermostat_mode', 'hvac_action', 'heating') }}"

  - platform: template
    sensors:
      cooler_status:
        friendly_name: "Air Conditioner Status"
        value_template: "{{ is_state_attr('climate.thermostat_mode', 'hvac_action', 'cooling') }}"

  - platform: template
    sensors:
      hvac_fan_status:
        friendly_name: "HVAC Fan Status"
        value_template: "{{ not is_state_attr('climate.thermostat_mode', 'fan_action', 'Idle') }}"

The true or false evalualtion of the templates will be converted to on or off respectively for binary sensors, no need to specify this.

1 Like

I spent some time trying to get where the simplification lies… and I failed :wink:
Should we call it “a variation” or I missed something significant?

Looking good.

Could you also share you grafana graph and how you configured the please ?

Greetz

Many thanks guys. I’ve not gotten round to trying anything I’m too busy at work with Coronavirus preparations but it looks like something I can work with.