Is it possible to loop over every climate entity given by a target selector (could be an area, entity list or device)? I use the target already in service calls, but I need to get/set the current temperature value of the climate entities. Unfortunately there is no service for this.
Is this selector in a script or blueprint?
Post whatever you are using.
The selector is used in a blueprint.
blueprint:
input:
target:
name: Target
description: Contains several climate entities
selector:
target:
entity:
domain: climate
trigger:
any trigger is fine here
action:
- service: climate.set_hvac_mode
target: !input target
data:
hvac_mode: "off"
That’s a pretty vague example… You can use a Repeat For Each action to iterate over the entities contained in your target input by assigning the input to a script variable and using that to define the list of repeat items:
blueprint:
input:
target:
name: Target
description: Contains several climate entities
selector:
target:
entity:
domain: climate
variables:
my_targets: !input target
trigger:
any trigger is fine here
action:
- repeat:
for_each: "{{ my_targets.entity_id }}"
sequence:
- if:
- condition: template
value_template: "{{ is_state(repeat.item, 'heat') }}"
then:
- service: climate.set_hvac_mode
target: "{{ repeat.item }}"
data:
hvac_mode: "off"
First of all thanks for your help! I did not want to post the full 400 lines blueprint, so I extracted the questionable part.
I think you did not got my question right. Sorry, probably my explanation was too vague. The code i provided is what I already have and what is working. There is no modification necessary, because the service accepts a target anyways.
I try to read the current_temperature attribute for every climate entity provided by the target. What I am struggling with is that target can be either an entity, a list of entities, an area or a device. So getting all climate entities needs to be done differently than having them provided in a list already… I know how to get them for every different type of target, but:
How can I identify what type of target is provided by the user?
You can use the keys()
dictionary method to get a list of the target types that were used.
{{ my_targets.keys() }}
But, if the target type is left so that any type can be selected it is possible to pick more than one, so I think you would probably be best off converting to a common type and combining:
{% set area_based = (my_targets.area_id | map('area_entities') | list) | sum(start=[]) %}
{% set device_based = (my_targets.device_id | map('device_entities') | list) | sum(start=[]) %}
{{ (my_targets.entity_id + area_based + device_based) | select('match', 'climate.') | unique | list }}
EDIT: Updated the template to flatten list using sum()
EDIT: That only half worked…
This seems like a mess, but it also seems to work .
{% set entity_based = my_targets.entity_id if my_targets.entity_id is list else [my_targets.entity_id] %}
{%- set area_based = (my_targets.area_id | map('area_entities') | list) | sum(start=[])
if my_targets.area_id is list else area_entities(my_targets.area_id) %}
{%- set device_based = (my_targets.device_id | map('device_entities') | list) | sum(start=[])
if my_targets.device_id is list else device_entities(my_targets.device_id) %}
{{- (entity_based + area_based + device_based) | select('match', 'climate.') | unique | list }}
Thanks @Didgeridrew, I have been struggeling to figure out how to use the target selector. This solution pointed me in the right direction
Based on your snippet I made some generic scripts that help me get a list of entities from a target selector. I shared it to the community here: Get entities from target selector - Share your Projects! - Home Assistant Community