I would like to trigger an automation created from a blueprint on the change of an attribute of any one of a list of entities provided.
The use case is the control of thermostats. I want to trigger when the set temperature of any thermostat in a list (in domain climate), provided as an input to the blueprint, changes (by someone turning the knob on the thermostat).
Solution 1 is to write template sensors like
- sensor:
### Master bedroom west trv set temperature
- sensor:
- name: Master bedroom west trv set temperature
unique_id: 2933bd4a-49c2-f565-bd67-fc3127fa0345
unit_of_measurement: 'C'
icon: mdi:thermometer
state: "{{ state_attr('climate.master_bedroom_west_trv', 'temperature') }}"
### Master bedroom east trv set temperature
- sensor:
- name: Master bedroom east trv set temperature
unique_id: 518c074d-c4e2-0bf9-a359-fc12ea6eeee0
unit_of_measurement: 'C'
icon: mdi:thermometer
state: "{{ state_attr('climate.master_bedroom_east_trv', 'temperature') }}"
And that works fine but it is a little annoying that I have to provide the blueprint with a list of template sensors in addition to the list of the thermostats that it already has.
blueprint:
name: "Heating X"
description: bla bla
domain: automation
input:
thermostat_controls:
name: DEVICE ENTITY - Thermostat control
description: One or more thermostat entities that are to be controlled by this automation
selector:
entity:
filter:
domain: climate
multiple: true
thermostat_set_temperature_sensors:
name: DEVICE ENTITY - Thermostat set temperature
description: The (template) sensors that read the set temperature from the specified thermostats
selector:
entity:
filter:
domain: sensor
multiple: true
[...]
trigger:
# Change in any one of the thermostat set temperatures
- platform: state
entity_id: !input thermostat_set_temperature_sensors
for:
seconds: 10 # do not trigger while value is changing
id: set_temperature_change
Solution 2 (attempted) was to write a template trigger that tests all the attributes. I was hoping that there is an implied trigger if any are changed, but it does not work. Is there an error in the code or in my assumption that a change in an attribute will give a True result (as it apparently does for entities)
blueprint:
name: "Heating X2" # temporary name for testing
description: bla bla bla
domain: automation
[...]
input:
thermostat_controls:
name: DEVICE ENTITY - Thermostat control (mandatory)
description: One or more thermostat entities that are to be controlled by this automation
selector:
entity:
filter:
domain: climate
multiple: true
variables:
local_thermostat_controls: !input thermostat_controls
[...]
trigger:
# Change in any one of the thermostat set temperatures
- platform: template
value_template: >
{% for thermostat in local_thermostat_controls %}
{% not state_attr(thermostat,'temperature') | float == states(local_required_temperature) | float %}
{% endfor %}
for:
seconds: 10 # do not trigger while value is changing
id: set_temperature_change
[...]
Solution 3 (attempted) was to write a template trigger that tests all the attributes against the last value it was set to, the ârequired temperatureâ, which is held in an input_number helper whose entity is an input to the blueprint. The following code yields True or False correctly when tested in the developer tools template editor, but when pasted into the blueprint does not cause a trigger. Is the theory wrong or the code?
blueprint:
name: "Heating X2" # temporary name for testing
description: bla bla bla
domain: automation
[...]
input:
thermostat_controls:
name: DEVICE ENTITY - Thermostat control (mandatory)
description: One or more thermostat entities that are to be controlled by this automation
selector:
entity:
filter:
domain: climate
multiple: true
required_temperature:
name: HELPER - Required temperature (mandatory)
description: The global variable (helper) to hold the required temperature
selector:
entity:
filter:
domain: input_number
variables:
local_thermostat_controls: !input thermostat_controls
local_required_temperature: !input required_temperature
[...]
trigger:
# Change in any one of the thermostat set temperatures
- platform: template
value_template: >
{% set changed_temperatures = namespace(total=0) %}
{% for thermostat in local_thermostat_controls %}
{% if not state_attr(thermostat,'temperature') | float == states(local_required_temperature) | float %}
{% set changed_temperatures.total = changed_temperatures.total + 1 %}
{% endif %}
{% endfor %}
{{ (changed_temperatures.total > 0) }}
for:
seconds: 10 # do not trigger while value is changing
id: set_temperature_change
[...]
Otherwise, is there another solution whereby I can trigger the automation by the change of value of a temperature of any of the list of thermostats?