Triggering automation if certain domain entities change state

Hello all!
I need to have a certain automation for the events when entities from domain climate change state and have troubles to do so. Mainly, I have tested two equivilent approaches:

  1. state platfor: if any entity changes state I check it’s domain in actions section. It looks like this:

trigger:
- platform: state
entity_id: all

For this piece of code I have an error: " Message malformed: Entity ID all is an invalid entity id for dictionary value @ data[‘entity_id’] " though it was copied from here: https://www.home-assistant.io/docs/automation/examples/

  1. Another approach is the do template trigger, cheching triggers domain:

platform: template
value_template: ‘{{ trigger.domain == “climate” }}’

But this code does not seem to do anything - just never triggers…

Can anyone suggest anything on this situation?
Thanks!

Maybe use multiple entity state triggers:

trigger:
   - platform: state
     entity_id: your_entity_name
   - platform: state
     entity_id: your_second_entity_name

and then add another trigger with next entity_id…
I mean triggers are OR
Best, JR

That’s the whole point of automation - not to list all entities explicitly.

What I do, is implementing most general climate controller for each room. You may have AC, humidifier, heat floors and radiators to change the climate inside single room, but they never work togather (exept last 2). In the winter you never use cooling, and never use heat floors during the summer. I have 2 temp thresholds to determine 3 seasons: summer, spring/fall, winter. And I have bool flags for what is allowed and what is not during each season - cooling, heating_ac, heating_water … Each climate entity supposed to have its own termostate in HA, but depending on the season, you should use only one, which is valid for current season, not 3 or 4 which actually are registered in each room. So you do simple automation which does the following:

  1. Shows you valid termostate for current season only - it is easy.
  2. Changes in state of valid termostates only are accepted.

To implement 2 I need the automation in the topic…
Templating would not only save time, but allow me not to bother when I add climate entity to the system…

1 Like

You can trigger on the “state_changed” event.

This should allow you to see any climate entity and work with it.

Exactly this instance I’ve been experimenting with this approach. I amd doing “state_changed” event triggering, but cannot access old_state, new_state objects. Do you know what is the syntax for that? trigger.old_state.domain yelds no result…

You have access to entity_id. So maybe something like this (untested):

{{ trigger.event.data.entity_id.startswith('climate') }}

… should get you most of the way there.

Alternately, try PyScript which will let you avoid all of that template syntax entirely and just use Python for this.

It works, sometimes. I made it notify.persistent_notify me the entity_id if domain is climate. Not only it sent me messages for ~20% of tested events, but eat 20% of CPU as well: from 0.7% to 20%… My gues is that listening to every state_changed event using script costs rescoureces…

It depends on how that script (Automation or PyScript) is written. The key is, filter as much out in the trigger as you can, and then exit out of the script as soon as you can possibly determine you have nothing to do.

If you share your entire automation or pyscript, I’m sure we can find ways to improve performance.

Yep, you are right!..

I’ve achieved the result following your path, but in slightly different direction, listening to call_service events, which have the domain inside, since HVAC modes are set by means of the climate services. It works fast and reliable, atleast so far, and I do not have to list all the termostates in the automation triggers and conditions, which does save a lot of effort.

The proper solution is always the simplest:

trigger:
platform: event
event_type: call_service
event_data:
domain: climate

2 Likes