History stat for an attribute from an Entity

Goal

Entity card that tells me how long my heating has been on ‘today, yesterday and this week’

My thermostat attribute won’t work with the history platform - see below
https://www.toptal.com/developers/hastebin/qinafariwu.yaml

because the attribute
HVAC_ACTION:
is what tells me when its in the state ‘‘Heat’’

How would I go about this

Create a template sensor that stores your current HVAC_ACTION. Use that in your History Stats.

1 Like

I actually was working on something like this last week. You should be able to modify it for your needs.

1 Like

Thanks both, I did think it be something like that but I was unsure how I made the HVAC_ a sensor… I’m very new to templating only currently doing it by copying others :slight_smile:

Added all my code just incase a future me is lurking here

This is what takes the attribute from drayton Wiser thermostat which stays (Idle, Heat, off)

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

Then we create 3 more sensors which use the ‘‘History_stat’’ to grab the time the sensors has been in ‘‘Heat’’ mode, bare in mind this will only start from when the sensor is made, and will be incorrect if HA went down.

# Thermostat Week
  - platform: history_stats
    name: Thermostat Heating This Week
    entity_id: sensor.hvac_activity
    state: 'heating'
    type: time
    start: "{{ as_timestamp( now().replace(hour=0, minute=0, second=0) ) - now().weekday() * 86400 }}"
    end: "{{ now() }}"
# Thermostat Day
  - platform: history_stats
    name: Thermostat Heating Today
    entity_id: sensor.hvac_activity
    state: 'heating'
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"
# Thermostat Yesterday
  - platform: history_stats
    name: Thermostat Heating Yesterday
    entity_id: sensor.hvac_activity
    state: 'heating'
    type: time
    end: "{{ now().replace(hour=0, minute=0, second=0) }}"
    duration:
     hours: 24

Not 100% if the yesterday is working yet as there is no data, will see tommorow :stuck_out_tongue:

Below is a picture of my dashboard (using mushroom cards)
The drayton Wiser is a brilliant option as its instant via HA as working locally unlike ‘‘Hive’’ integration
little bit more complicated to setup, but worth it

2 Likes

Thanks for this. I’ve added it for my thermostat so I can calculate run time vs. gas use. I also added it for my sump to track run time.
I had a similar setup that counted the # of runs each day. This is some nice added data.
One thing - since HA already keeps history, I don’t see a need for yesterday. You should be able to pull it from the history - no?