Problem with an automation iterating multiple entities

I’m trying to make some kind of night eco solution for my thermostats, where I can select via a group of input_booleans which thermostats to alter the temperature on.
I just can’t wrap my head around how to do this.

I’ve tried with the code below, but HA won’t eat it :slight_smile:

alias: Heating Eco Start
trigger:
  - platform: template
    value_template: '{{ states.sensor.time.state == (states.input_datetime.heat_eco_start.attributes.timestamp | int | timestamp_custom("%H:%M", False)) }}'
action:
  - service: climate.set_temperature
    data_template: >-
      {% for entity_id in states.group.heat_control_group.attributes.entity_id if states(entity_id) == 'on' %}
        entity_id: entity_id
        value_template: >-
          {% if entity_id == "input_boolean.heat_diningroom" %}
            {{ states.climate.danfoss_z_thermostat_heating_1.attributes.temperature }}
          {% elif entity_id == "input_boolean.heat_bathroom" %}
            {{ states.climate.danfoss_z_thermostat_heating_1_2.attributes.temperature }}
          {% endif %}
      {% endfor %}

The main problem is you can’t define multiple YAML lines via a Jinja for loop. But you can provide the climate.set_temperature service a list of entity_id’s. I would do it something like this:

alias: Heating Eco Start
trigger:
  platform: template
  value_template: >
    {{ is_state('sensor.time',
                state_attr('input_datetime.heat_eco_start','timestamp')
                |timestamp_custom('%H:%M')) }}
action:
  service: climate.set_temperature
  data_template:
    entity_id: >
      {{ states.climate|selectattr('state','ne','off')
         |selectattr('entity_id','in',state_attr('group.heat_control_group','entity_id'))
         |map(attribute='entity_id')|list|string|replace('[','')|replace(']','') }}
    temperature: ECO_TEMP

This will create a comma separated list of climate entity_id’s whose states are not ‘off’ and that are in your group. It will set all these climate entities to a particular temperature (i.e., replace ECO_TEMP with whatever value you want to set them to.)

I see where you are going, but …

The problem is that the switch is a boolean for each thermostat, but the entity I need to set the temperature on is a climate entity.

Ie. input_boolean.heat_diningroom = climate.danfoss_z_thermostat_heating_1

Ok, that seems unusual. The climate devices I’m familiar with have a state that tells if they’re off, or if they’re on, what mode they’re in (e.g., heat, cool, etc.) Does your’s not work that way? What climate platform are you using?

It’s probably possible to build up an entity_id list using a for loop, but before going down that path I’d like to verify that indeed your climate device does not show on/off status in its state.

EDIT: Also, I should ask, in your original post, you seemed to be trying to set each climate device’s temperature setting to its current temperature setting, which wouldn’t actually do anything. Is that what you wanted? Or, to put that another way, when this runs, what temperature do you want to set the thermostats to?

It’s Danfoss Z-wave thermostats.

Udklip

Ok, thanks for the info. FWIW, to me, that doesn’t seem right. I mean, that climate platform doesn’t seem to be doing what it’s supposed to. It should be indicating the mode (off, heat, cool, etc.) as the state, but it’s not. Oh well.

So, let me see if I understand now. The on/off state of each system is shown/controlled via an input_boolean. Your group contains the input_booleans. For each input_boolean there is a corresponding climate entity that shows/controls the temperature set point.

If that’s right, then you were close in your OP. I think this is what you need:

alias: Heating Eco Start
trigger:
  platform: template
  value_template: >
    {{ is_state('sensor.time',
         state_attr('input_datetime.heat_eco_start', 'timestamp')
         |timestamp_custom('%H:%M', false)) }}
condition:
  condition: template
  value_template: >
    {{ states.input_boolean
       |selectattr('entity_id','in',
                   state_attr('group.heat_control_group','entity_id'))
       |selectattr('state','eq','on')|list|count > 0 }}
action:
  service: climate.set_temperature
  data_template:
    entity_id: >-
      {% set climate = {'heat_diningroom': 'danfoss_z_thermostat_heating_1',
                        'heat_bathroom': 'danfoss_z_thermostat_heating_1_2'} -%}
      {% for entity_id in state_attr('group.heat_control_group', 'entity_id')
           if is_state(entity_id, 'on') -%}
        {% if not loop.first %},{% endif %}climate.{{
          climate[entity_id.split('.')[1]] }}
      {%- endfor %}
    temperature: ECO_TEMP

I put the climate thingy on hold and started building a doorbell automation, where I have similar need to iterate a group.

It doesn’t work :frowning:

 - alias: Sound Doorbell
  trigger:
    - platform: state
      entity_id: input_boolean.doorbell_control_test
      to: 'on'
  condition:
    condition: template
    value_template: >
      {{ states.input_boolean
         |selectattr('entity_id','in',
                     state_attr('group.doorbell_sound_devices','entity_id'))
         |selectattr('state','eq','on')|list|count > 0 }}
  action:
    - service: media_player.play_media
      data_template:
        entity_id: >-
          {% set media_player = {'doorbell_sound_diningroom': 'diningroom',
                                 'doorbell_sound_livingroom': 'livingroom',
                                 'doorbell_sound_bedroom': 'bedroom'} -%}
          {% for entity_id in state_attr('group.doorbell_sound_devices', 'entity_id)
               if is_state(entity_id, 'on') -%}
            {% if not loop.first %},{% endif %}media_player.{{ media_player[entity_id.split('.')[1]] }}
          {%- endfor %}
        media_content_type: 'audio/mp3'
        media_content_id: "http://some.url/sounds/doorbell-001.mp3"

    - delay: 0:0:03
    - service: input_boolean.turn_off
      entity_id: input_boolean.doorbell_control_test

When doing a config check i get this:

Invalid config for [automation]: invalid template (TemplateSyntaxError: expected token ',', got 'on') for dictionary value @ data['action'][0]['data_template']['entity_id']. Got None.

Looks like

{% for entity_id in state_attr('group.doorbell_sound_devices', 'entity_id)

is missing a closing quote after the second entity_id. Should be:

{% for entity_id in state_attr('group.doorbell_sound_devices', 'entity_id')