Get all AC units that have been on for some time

I’m trying to send a notification if any AC unit has been on for ex. 5 hours.
I created a helper of type Group with my 4 units, but that doesn’t help. I could turn on unit A for 2 hours, then turn on B and turn off A, then wait 1 hour. At this point the helper would report being on for 3 hours even though the units by themselves haven’t been on for more than 2 hours.

So I tried with this template:

{% set on_units = states.climate
| selectattr("state", "ne", "off")
| map(attribute="entity_id")
| list() %}
{% for entity in on_units %}
{% if (as_timestamp(now()) - as_timestamp(states[entity].last_changed))/60 > (60*5) %}
{{ states[entity].name }} has been on for {{ relative_time(states[entity].last_changed) }} (since {{ as_timestamp(states[entity].last_changed) | timestamp_custom("%H:%M on %Y-%m-%d") }})
{%- endif %}
{%- endfor %}

This works, but how do I use it as a condition?
Ex. I’d like to add an automation that runs every 30 minutes, checks if any unit has been on for > 5 hours and if so it sends me a notification.

I see that I can use a template as a condition, but how do i make it “return” True/False?

Thanks!

Go to Developer tools → States, find any one of your climate entities and confirm that when it is actively cooling, its state value is on.

In my case, its state is cool which means it is available to perform cooling but a separate attribute named hvac_action indicates whether it is actually cooling or idle (not cooling).

In this screenshot, it is actively cooling because the value of hvac_action is cooling.

Check the value of the hvac_modes attribute. If it’s simply on, off then you’re correct and on means that it is actively cooling.

Thank you, but this is not what I asked, I can determine whether they’re on or off correctly. I need to send a notification if they’ve been on for 5 hours.

I can help with the notification but before I do that I need confirmation that your climate entity has only two possible values for hvac_modes.

I have all these:

hvac_modes: fan_only, dry, cool, heat, heat_cool, off

This is why I’m checking for state not equal off

What I was trying to impress upon you is that “state not equal off” means the device is in one of its other modes ( fan_only, dry, cool, heat, heat_cool) but it doesn’t mean it is actively cooling (or heating).

So if your goal is to simply know when it’s “not off for 5 hours” then your template is fine. However, if you want to know when it’s “working for 5 hours”, then the template must be modified to check the value of attributes.hvac_action and not state.

The following Template Condition calculates the number of climate entities that are “not off for 5 hours” and checks if the quantity is greater than 0. The template’s result is a boolean value (true/false).

alias: example
trigger:
  - platform: time_pattern
    minutes: '/30'
condition:
  - condition: template
    value_template: >
      {% set ns = namespace(qty=0) %}
      {% for entity in states.climate | selectattr("state", "ne", "off") | map(attribute="entity_id") 
        if now() - states[entity].last_changed > timedelta(minutes=5) %}
      {% set ns.qty = ns.qty + 1 %}
      {% endfor %}
      {{ ns.qty > 0 }}
action:
  ... your actions ...

Thank you very much, this works perfectly for my needs.
Thank you also for the clarification about hvac_action. It’s actually behaving in a weird way in my home: when the units are off, it’s off, when I turn them on, the attribute completely disappears. See screenshot.

The units are all pretty recent Daikin. I can’t see it now, but in the default card I can sometimes see idle or cooling so it’s getting the info from somewhere…
Do you know what’s going on?

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.


I’m unfamiliar with the specifics of Daikin devices.

My HVAC unit has an auto mode. When set to auto, the hvac_action attribute is displayed and its value is either idle, heating, or cooling, depending on the HVAC unit’s current activity.

I checked the Daikin integration’s documentation and there’s nothing mentioned about the behavior of hvac_action.

I also checked the Github Core repository for open Issues (bugs) concerning the Daikin integration and found no reports about hvac_action disappearing from view. Perhaps no one has ever noticed this odd behavior (or it’s considered to be normal for this device).

Maybe the hvac_action attribute appears only when the device is actively operating (heating or cooling). However, that’s just a guess; I don’t know why the hvac_action attribute isn’t always displayed for your Daikin device. It seems like a bug to me.

Thank you, I’ll look around in the project’s Github page. I added the Solution tag.

1 Like