Keeping track of my Heatpump's active state

I want to keep track of how often my heat pump runs in the day. I already have a history_stats sensor set up, but I noticed the state doesn’t behave like I thought.

My heat pump has off | heat_cool | heat | cool but that state doesn’t change with the activity of it. For example, if I turn the AC on and set it to 75, but the inside is 72 the state is cool because, well, that is what is set. It just isn’t active.

I traced what happens when it does turn on and I noticed there is an attribute change (below). My question is:

how can I track how long this is in a cool state with the attribute hvac_action = cooling?

        "new_state": {
            "entity_id": "climate.thermostat",
            "state": "cool",
            "attributes": {
                "hvac_modes": [
                    "cool",
                    "heat_cool",
                    "off",
                    "heat"
                ],
                # snip .......
                "hvac_action": "cooling",   # <-------
                # snip.....
            },
            "last_changed": "2022-07-26T17:52:44.592438+00:00",
            "last_updated": "2022-07-26T17:54:44.582804+00:00",
            "context": {
                "id": "01G8XTB676DD7CV817MX942FZC",
                "parent_id": null,
                "user_id": null
            }
        }

It seems to be an attribute of the entity.
There doesn’t seem to be a way to track an attribute so you need to template a sensor that is the attribute and then use the history stats on the new templated sensor.

Thanks. So I created a template sensor to be true | false depending on the attribute, and then modified my history_stat to track that sensor instead of my thermostat. Is this the best way?

- platform: template
  sensors:
    hvac_active:
      unique_id: hvac_active
      friendly_name: HVAC Active
      value_template: >
        {% if is_state_attr("climate.thermostat", "hvac_action", "cooling") -%}
          true
        {%- else -%}
          false
        {%- endif %}

- platform: history_stats
  name: HVAC usage today
  entity_id: sensor.hvac_active
  state: "true"
  type: time
  start: "{{ now().replace(hour=0, minute=0, second=0) }}"
  end: "{{ now() }}"

This should be enough, but it really doesn’t matter.

value_template: >
        {{ is_state_attr("climate.thermostat", "hvac_action", "cooling") }}

But yes that is the best as far as I know.
Would be cool if the history stats could list the states as attributes and give the respective time for each.

1 Like