Sensor for heating duration for one day or more

Hi everyone,
I tried to do a sensor for calculate heating duration through 24h or more, but I have a problem with time calculate. I did 2 sensors in my sensor.yaml page for heating active and history state and the sensor can detected but the display mentioned wrong time! as it started with 5.05 and doesn’t give me the real time duration for heating.
As you can see in the picture below it mentioned 7h while it heated only for 2 and half hours

This is what I did

and this is what I got


Can you please help me to correct the mistakes.
Thank you in advance

Please don’t post pictures of text. Paste your config and format it correctly for the forum.

That way people can copy and edit it to help you.

The history stats sensor interrogates the recorder database to do it’s calculations. So between midnight and now your heating device had already run for 5.5 hours. You then ran it for another 2 hours which means in total it had run for 7 hours between midnight and now.

First of all sorry for the code in picture form, I didn’t know about the codes. And thank you for the reply as you said the day after everything coming right with zero start. Thank you again and I will post the code for everyone to get benefit.

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

** Please change the bold font with your climate sensor

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

Then restart you home assistant
Then go to Helper and choose Utility Meter

And put the setting as shown bellow for weekly, daily, monthly up to you. The picture for weekly measure.

After that add a card(in my case I chose sensor card) with the name you gave, in my example the name is heat weekly

Hope this will help everyone

1 Like

Hi folks, few months I did a similar setup for my Hive thermostat. To track how long the heating downstairs/upstairs is running is easy, although, the lecture is tricky depending how I show the sensors:

‘/homeassistant/template.yaml’

## HVAC sensors

  - sensor:
      - name: HVAC Downstairs
        state: "{{ state_attr('climate.downstairs', 'hvac_action') }}"
        
  - sensor:
      - name: HVAC Upstairs
        state: "{{ state_attr('climate.upstairs', 'hvac_action') }}"
        
  - sensor:
      - name: HVAC Water
        state: "{{ state_attr('water_heater.downstairs', 'hvac_action') }}"

‘/homeassistant/sensor.yaml’

## HVAC sensors

- platform: history_stats
  name: Thermostat Heating Today Downstairs
  entity_id: sensor.hvac_downstairs
  state: 'heating'
  type: time
  start: "{{ now().replace(hour=0, minute=0, second=0) }}"
  end: "{{ now() }}"
  
- platform: history_stats
  name: Thermostat Heating Today Upstairs
  entity_id: sensor.hvac_upstairs
  state: 'heating'
  type: time
  start: "{{ now().replace(hour=0, minute=0, second=0) }}"
  end: "{{ now() }}"
  
- platform: history_stats
  name: Heating Today Water
  entity_id: sensor.hvac_water
  state: 'heating'
  type: time
  start: "{{ now().replace(hour=0, minute=0, second=0) }}"
  end: "{{ now() }}"

Duration of the heating is correct and accurate, now if i want to keep a record of the last week, I use “custom:mini-graph-card”

type: custom:mini-graph-card
name: Downstairs + Attic
icon: mdi:radiator
entities:
  - entity: sensor.thermostat_heating_today_downstairs
    show_state: true
    color: purple
hours_to_show: 168
lower_bound: 0
group_by: date
hour24: true
aggregate_func: max
show:
  labels: true
  graph: bar
  average: true
  extrema: true


Here, when the sensor is read in ‘custom:mini-graph-card’ the time is not represented correctly . For instance, in the first picture attached, Thermostat Heating Today Downstairs 1:56:24 the same sensor read by ‘custom:mini-graph-card’ shows 1.94h. It corresponds to the same amount of time, just different format.
Am I doing something wrong in my code?

If I represent the data with a different card, the representation is wrong as well. Surely, I’m missing something on my code.

header:
  title: Heating en marcha
  show: true
  show_states: true
type: custom:apexcharts-card
chart_type: donut
experimental:
  color_threshold: true
series:
  - entity: sensor.thermostat_heating_today_downstairs
    name: Abajo
    color: rgb(154,205,50)
    color_threshold:
      - value: 0
        color: yellowgreen
    float_precision: 3
  - entity: sensor.thermostat_heating_today_upstairs
    name: Arriba
    color: rgb(250,0,0)
    color_threshold:
      - value: 0
        color: red
    float_precision: 3
apex_config:
  tooltip:
    x:
      show: true
      format: HH:mm

Related to this Thermostat, I am unable to track the duration for the hot water. I start to think is a limitation from Hive, as they don’t show this information on the native app. As you can see in the code, I use similar approach for getting this data, but its always shows 0.
Does anybody try to read the duration of the heating water on Hive?

1 Like

Hi mate, I can understand your frustrating about the time which is different format and makes the min 100 point while it should be 60.
I fount the solution with Apexchart card from HACS, you need to install it and restart HA.

After that just add new Apex card with these codes:

type: custom:apexcharts-card
experimental:
  color_threshold: true
graph_span: 7d
span:
  end: day
show:
  last_updated: true
header:
  show: true
  title: Heating daily
  show_states: true
  colorize_states: true
yaxis:
  - min: 0
    max: 12
    decimals: 0
    apex_config:
      tickAmount: 5
series:
  - entity: sensor.heating_today
    show:
      header_color_threshold: true
      extremas: true
      as_duration: hour
    type: column
    name: Duration
    group_by:
      func: last
      duration: 1d
    color_threshold:
      - value: 1
        color: dodgerblue
      - value: 3
        color: darkcyan
      - value: 5
        color: orange
      - value: 7
        color: darkred
      - value: 10
        color: red

And the above code for daily and it’s really nice with diffrent level color and the duration right( not like 1 hour and 95 min )


down I give you the codes for weekly and monthly

- type: custom:apexcharts-card
    experimental:
      color_threshold: true
    graph_span: 7d
    span:
      end: week
    show:
      last_updated: true
    header:
      show: true
      title: Heating weekly
      show_states: true
      colorize_states: true
    yaxis:
      - min: 0
        max: 24
        decimals: 0
        apex_config:
          tickAmount: 5
    series:
      - entity: sensor.heating_time_weekly
        show:
          header_color_threshold: true
          extremas: true
          as_duration: hour
        type: column
        name: Duration
        group_by:
          func: last
          duration: 1d
        color_threshold:
          - value: 5
            color: dodgerblue
          - value: 10
            color: darkcyan
          - value: 15
            color: orange
          - value: 20
            color: darkred
          - value: 25
            color: red
  - type: custom:apexcharts-card
    experimental:
      color_threshold: true
    graph_span: 31d
    span:
      end: week
    show:
      last_updated: true
    header:
      show: true
      title: Heating monthly
      show_states: true
      colorize_states: true
    yaxis:
      - min: 0
        max: 50
        decimals: 0
        apex_config:
          tickAmount: 5
    series:
      - entity: sensor.heating_time_monthly
        show:
          header_color_threshold: true
          extremas: true
          as_duration: hour
        type: column
        name: Duration
        group_by:
          func: last
          duration: 1d
        color_threshold:
          - value: 10
            color: dodgerblue
          - value: 15
            color: darkcyan
          - value: 20
            color: orange
          - value: 25
            color: darkred
          - value: 30
            color: red

With this code I found it better for results as it gives you even with sec.
Sure you can change the value color as you like

I hope this helping you :slight_smile:

1 Like

Please check the above solution

Thanks mate, yes it works indeed.
I was already using ApexChart card, so sharing your code my card came straightforward after changing the sensors.
Picture comparing the old and new card.

The time shows at the top is correct, and yes, its nice to have the days with different colours based on the consumption.
Interesting though, the time showed on the top has the right format time/hour, but at the bottom right, it gives me the same number that in my old card, 1.7, but it doesn’t bother me :sweat_smile:

For the weekly and monthly card, I need first to create the correspondent sensors.

Your code has helped, thanks @GoldenEyes.

1 Like

I’m glad you find it good, happy for you