For Loop Template With an if Statement?

Hey,

So I’m trying to configure something quite simple, but I seem to be missing something.

I want to adjust the temperature on all my thermostats when I come/leave home.

I used to hardcode all the thermostats in If statements, such as:

But I think templating this would be a lot cleaner. Something along the lines of:

service: >
  {% for thermostat in ["climate.work_room_thermostat", "climate.living_room_thermostat", "climate.bedroom_thermostat"] %}
    {% if states(thermostat) == "heat" %}
      climate.set_temperature
    {% endif %}
  {% endfor %}
data_template:
  entity_id: "{{ thermostat }}"
  temperature: "{{ home_temp }}"

Or

service: climate.set_temperature
data_template:
  entity_id: >
    {% for thermostat in ["climate.work_room_thermostat", "climate.living_room_thermostat"] %}
      {% if states(thermostat) == "heat" %}
        {{ thermostat }},
      {% endif %}
    {% endfor %}
  temperature: "{{ home_temp }}"

But I keep getting: not a valid value for dictionary value @ data['entity_id']. Got None, when trying to manually run the service.

Anyone got any ideas?

2024-01-20 12:07:03.900 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'thermostat' is undefined when rendering '{{ thermostat }}'
2024-01-20 12:07:03.900 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'away_temp' is undefined when rendering '{{ home_temp }}'

Am I crazy? The temp variables are clearly defined:

And so is the thermostat one, in the template:

^ Working correctly, only the living room one is set to heat.

Okay, I solved the variable issue by creating helper input_number(s). Certainly a better way to go since I can control those from my dashboard, but something still seems to be bugging the “thermostat” variable.

you can template this, but you’re doing it wrong.

- repeat:
    for_each: >
      {% set climates = ["climate.work_room_thermostat", "climate.living_room_thermostat", "climate.bedroom_thermostat"] %}
      {{ climates | select('states', 'eq', 'heat') | list }}
    sequence:
    - service: climate.set_temperature
      target:
        entity_id: "{{ repeat.item }}"
      data:
        temperature: "{{ home_temp }}"

God bless your soul.

For some reason,
select('states', 'eq', 'heat')
Did not work for me, but
select('is_state', 'heat') did.

Thanks!

Yeah, mistake on my part

1 Like

I have also created 3 input booleans, which correlate to each of the 3 thermostats. Their names are

  • thermostat_idle_work_room
  • thermostat_idle_bedroom
  • thermostat_idle_living_room

climate.living_room_thermostat should be impacted by input_boolean.thermostat_idle_living_room, etc.

Any ideas how can I integrate a check within my repeat sequence, that checks if those input booleans are also off? So for the sequence the proceed, the corresponding input boolean needs to be off.

I can’t seem to get this to work:

repeat:
  for_each: >
    {% set climates = [
      {"thermostat": "climate.work_room_thermostat", "room_clear_boolean": "input_boolean.thermostat_idle_work_room"},
      {"thermostat": "climate.bedroom_thermostat", "room_clear_boolean": "input_boolean.thermostat_idle_bedroom"},
      {"thermostat": "climate.living_room_thermostat", "room_clear_boolean": "input_boolean.thermostat_idle_living_room"}
    ] %}
    {{ climates | selectattr('thermostat', 'in', states | map(attribute='entity_id') | list) | select('is_state', 'heat') | select('room_clear_boolean', '==', 'off') | map(attribute='thermostat') | list }}
  sequence:
    - service: climate.set_temperature
      target:
        entity_id: "{{ repeat.item }}"
      data:
        temperature: "{{ states('input_number.climate_temperature_home') }}"
alias: Set Home Temperature on Thermostats
{{ climates | selectattr('thermostat', 'is_state', 'heat') | selectattr('room_clear_boolean', 'is_state', 'off') | map(attribute='thermostat') | list }}
1 Like

That was it!!!