Generic_thermostat in hvac_mode: heating time counter

I want to create a sensor that counts the overall time my generic_thermostat spends in heating mode. I found the hvac_action attribute, which changes between off, idle and heating. I would like to count the time it spends in heating, because I want to create an utility meter out of this.

is_state_attr('climate.heater', 'hvac_action', 'heating')

This returns true, when my heater heats, and false every time it is not heating.

You probably want to use the history_stats integration to do this, I do something very similar to monitor how long my boiler relay (binary_sensor.13_129802_active) is on:

sensor:
  - platform: history_stats
    name: Boiler CH Time
    entity_id: binary_sensor.13_129802_active
    state: "on"
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"

However I think you’ll need to create a template sensor first, to represent your thermostat’s hvac_action value as a binary sensor, and then the history stats sensor will refer to this.

Why I would like an incrementing sensor, is I could use it with the Utility Meter, like a real utility meter that never resets.

Does this history_stats reset?

It resets every day (that’s what the template in the start defines). I have a statistics graph on my dashboard that shows the last 7 days’ values:

chart_type: bar
period: day
type: statistics-graph
entities:
  - sensor.boiler_ch_time
stat_types:
  - max
days_to_show: 7
hide_legend: true
title: CH Time

I think the utility meter helper might be able to handle values that reset, but a utility meter is your end goal, there’s probably a more efficient way to achieve this than having lots of extra intermediate entities, but it’s beyond my knowledge I’m afraid.