Tracking Heating Time with Templates

I am attempting to create a sensor that acts as a time accumulator for when my Mysa baseboard thermostats turn on - sadly despite the manufacturer tracking this they don’t make it available over the Homekit integration :upside_down_face: .

I found this topic that seems to offer a straightforward means of doing so.

What I did:
I create my own series of template sensors to go in sensors.yaml according to the linked thread.
I also verified that value_template are pulling heating/none/idle for each thermostat.

Template sensors:

### Heating time tracking ###
# TODO: Integrate with utilitiy meter integration
# refs: 
# https://community.home-assistant.io/t/sensor-for-how-long-was-heating-on-today/343701/2
# https://www.home-assistant.io/integrations/utility_meter/
# Upstairs Bedroom
- platform: template
  sensors:
    hvac_activity:
      friendly_name: Upstairs Bedroom HVAC Activity
      value_template: "{{ state_attr('climate.mysa_2087b4_thermostat', 'hvac_action') }}"
        
- platform: history_stats
  name: Upstairs Bedroom Heating Time
  entity_id: sensor.upstairs_bedroom_hvac_activity
  state: 'heating'
  type: time
  start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
  end: '{{ now() }}'
####################

# Dizz Office
- platform: template
  sensors:
    hvac_activity:
      friendly_name: Dizz Office HVAC Activity
      value_template: "{{ state_attr('climate.mysa_037b2c_thermostat', 'hvac_action') }}"
        
# template:
#   - sensor:
#       - name: Office HVAC Activity
#         state: "{{ state_attr('climate.mysa_037b2c_thermostat', 'hvac_action') }}"
        
        
- platform: history_stats
  name: Office Heating Time
  entity_id: sensor.office_hvac_activity
  state: 'heating'
  type: time
  start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
  end: '{{ now() }}'
####################

# Living Room
- platform: template
  sensors:
    hvac_activity:
      friendly_name: Living Room HVAC Activity
      value_template: "{{ state_attr('climate.living_room', 'hvac_action') }}"
        
- platform: history_stats
  name: Living Room Heating Time
  entity_id: sensor.living_room_hvac_activity
  state: 'heating'
  type: time
  start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
  end: '{{ now() }}'
  ####################

Problem:
Only the top template works - as in it’s the only one that actually seems to be accumulating time. The other two while they exist as sensors are stuck at 0. I’ve let them run for 24 hours so they sound have accumulated something.

I cannot figure out why.

Additional Oddities:

  1. The “new” template sensor syntax that is referenced in the thread just flat out does not work for me. It creates errors in the config file.

  2. Despite having renamed all three thermostats to something friendlier it has only propagated to the living room thermostat for reasons I cannot determine.
    If I inspect any of them in the Dev tools I can see that they have a friendly_name set and yet the climate. entity only reflects that name for the living room thermostat.

All the template sensors have the same entity id hvac_activity (despite having different friendly names). They will likely be numbered _2, _3, … to deduplicate them but they are not what you are trying to get history of.

Probably because you have entered them in the same file where all your other sensor entities are defined.

The new format for Template Sensors (and all other Template entities) belongs under the template: key and not under the sensor: key.

Leave all the History Stats sensors where they are but remove the Template Sensor configurations employing the legacy format. Create the Template Sensors using modern format in your configuration.yaml file under the template: key. However, if you already have this in your configuration.yaml file:

template: !include templates.yaml

then create the Template Sensors in the templates.yaml file. Ensure the first line in templates.yaml does not contain the template: key.

Ooooh, I’m a dingus. Alright, I see I’ve neither fully internalized yaml nor HA’s internal structure.

Okay now I have a templates.yaml with an !include in configuration.yaml that looks like this:

- sensor:
  - name: "Upstairs Bedroom HVAC Activity"
    state: "{{ state_attr('climate.mysa_2087b4_thermostat', 'hvac_action') }}"

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

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

Seems to be working now.
Thank you to the both of you for the assistance and to Taras for the original thread/method as well!

I usually use a Reiman sum feeding a utility meter. This way I can get current day, daily, weekly and monthly usage. In your example you’d be accumulating hours.

If you know the wattage of the heaters then it’s easy to use the same pattern to accumulate energy usage by radiator. I do this for the zones of my heating system, though instead of accumulating watts I’m accumulating gallons of oil. Either way the pattern is the same for accumulating hours, watts, gallons, etc. I use hour accumulation to track filter usage and generator runtime.

1 Like