Sensor for 'how long was heating on today'?

Hey all, I’m sure this is simple but I can’t quite get my head round it.

I have a climate/thermostat entity. It’s ‘state’ is always ‘heat’ whether or not it’s actually ‘heating’ or not. (Probably the way the Nest works, dunno…)

It has an attribute of ‘hvac_action: off’ which changes to ‘hvac_action: heating’ when it’s actually heating.

I want to create a timer based on how long the ‘hvac_action: heating’ has been on for the day. I realise I need to create a sensor in configuration.YAML. Happy to do this. But I can only work out how to track a ‘state’ - which won’t work for a Nest thermostat - as I said, it is always saying ‘heat’.

What can I ‘add’ to my YAML (below) that will track an attribute of an entity (in this case ‘hvac_action:’) ?

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

Thanks,
Matt

2 Likes

Create a Template Sensor using either the new format:

template:
  - sensor:
      - name: HVAC Activity
        state: "{{ state_attr('climate.living_room', 'hvac_action') }}"

or the old format:

sensors:
  - platform: template
    sensors:
      hvac_activity:
        friendly_name: 'HVAC Activity'
        value_template: "{{ state_attr('climate.living_room', 'hvac_action') }}"

Reference the Template Sensor in the History Stats Sensor:

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

Once you get that working, you might consider using it with the Utility Meter integration so that it can record long-term stats for daily/weekly/monthly/yearly heating hours. For example:

utility_meter:
  heating_time_daily:
    source: sensor.heating_today
    name: Heating Time Daily
    cycle: daily
  heating_time_weekly:
    source: sensor.heating_today
    name: Heating Time Weekly
    cycle: weekly
  heating_time_monthly:
    source: sensor.heating_today
    name: Heating Time Monthly
    cycle: monthly
  heating_time_yearly:
    source: sensor.heating_today
    name: Heating Time Yearly
    cycle: yearly
12 Likes

Absolute legend. Clear, concise, and even coded for me.

Thank you very much. Appreciate it. Always learning.

/salute

:+1:t3:

1 Like

Taras, may I ask you which purge_keep_days period is recommended for 1-year-data? 31 I think?

If you use the Utility Meter integration, its sensor data is stored within the database’s statistics table which is exempt from the regular automatic purging cycle.

All other data is purged according to purge_keep_days (default is 10 days).
Setting it to 31 means data older than 31 days is purged.

Thank you for your quick reply. Since September I’m monitoring the tv runtime by using the model above (history_stats, 4 utility_meter-sensors and notify.FILE). And I want to get sure, that the data can be collected for one year. As I wasn’t aware of the fact, that the data is stored in separate ways I set the purge_keep_days period to 31 to save at least the data for every month.

Hi Guys,
any chance you could take a look at same problem here, cann;t get it working…

Thanks to everyone on this thread as I’ve managed to get this working for all my panel heaters. Does anyone know how I can convert the time value into kWH? Each of my heaters are 1000W so should the math is pretty easy…just not sure how to do this in HA?

With templates you can create a new energy sensor which is based on a calculation.

Hey! Sorry for jumping on this thread… I’m trying to do the same but can’t figure this out! I’m a bit of a newbie with Home Assistant and coding, etc.

I’ve got the first box of code working with my Templates page on Home Assistant and my own thermostat entity but I’m not sure how you reference the template sensor in history stats? Is that on the same page as templates - do I put that code with my entities in that page?

Sorry for what is probably a really obvious question!

Post the configuration for the entities you have created.

The snippet above is missing sensor:
( History Stats - Home Assistant )

Here’s mine.

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

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

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

1 Like

Not sure why this isn’t working for me, damn.

CONFIGURATION.YAML:

template:
  - sensor:
      - name: HVAC Activity
        state: "{{ state_attr('states.climate.3family_room', 'hvac_action') }}"

SENSOR.YAML:

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


Because your Template Sensor contains an invalid entity_id in the state_attr() function.

Change this:

        state: "{{ state_attr('states.climate.3family_room', 'hvac_action') }}"

To this:

        state: "{{ state_attr('climate.3family_room', 'hvac_action') }}"
2 Likes

I think that fixed it! Thanks.

I wonder if anyone can help?

I have a template sensor set up with id sensor.kitchen_heating_mode and another for sensor.b1_heating_mode. That is running fine.

in my configuration.yaml I have added the following:

sensor:
  - platform: history_stats
    name: "B1 heating today"
    entity_id: sensor.b1_heating_mode
    state: "heating"
    type: time
    start: "{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}"
    end: "{{ now() }}"
    unique_id: "b1_heating_history_today"
  - platform: history_stats
    name: "Kitchen heating today"
    entity_id: sensor.kitchen_heating_mode
    state: "heating"
    type: time
    start: "{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}"
    end: "{{ now() }}"
    unique_id: "kitchen_heating_history_today"

But after saving and reloading HA config those new entities don’t show up anywhere. If I try to create a utility helper I don’t have the option to select those history_stats entities. Am I doing something wrong?

Edit: in case anyone else had the same issue, I was being dumb… Didn’t realise you had to do a full restart not just a YAML reload.

Could you post your configuration for the card you are using for this? Would like to do something similar!