Noob Templating

I’m quite new to home assistant and trying to create a template to use to check through all my thermostats, and check if the current temperature is below the required temperature. I have customised the entities so that each zone of the house is defined and I use this to determine call for heat per zone.

I seem to be having an issue with setting the call_for_heat variable within my if statement

{% set heating_zone = 'chapel' %}
{% set call_for_heat = 'false' %}
{% for state in states.climate | selectattr('attributes.heating_zone', '==', heating_zone) %}

  {% if state.attributes.current_temperature | float <= state.attributes.temperature | float %}
    {% set call_for_heat = 'true' %}
    {{state.name}} ({{state.attributes.heating_zone}}) - Call for heat: True
  {% else %}
    {{state.name}} ({{state.attributes.heating_zone}}) - Call for heat: False
  {% endif %}
{% endfor %}
Overall Call for Heat: {{ call_for_heat }}

Here’s my output:

 Girls Thermostat (chapel) - Call for heat: False
  


  
    Daniel Thermostat (chapel) - Call for heat: False
  


  
    
    My Room Thermostat (chapel) - Call for heat: True
  


  
    Joshua Thermostat (chapel) - Call for heat: False
  


  
    Room 10 Thermostat (chapel) - Call for heat: False
  


  
    Gaming Room Thermostat (chapel) - Call for heat: False
  

Overall Call for Heat: false

As you can see one of the thermostats is calling for heat, but I get false output…

Could I ask why you are doing this with a template and not an automation?

You need a namespace to get the values out of the loop:

{% set heating_zone = 'chapel' %}
{% set ns = namespace(call_for_heat = [false]) %}
{% for state in states.climate | selectattr('attributes.heating_zone', '==', heating_zone) | selectattr('attributes.temperature', 'defined') %}
    {% set ns.call_for_heat = ns.call_for_heat + 
    [ state.attributes.current_temperature | float(0) 
    <= state.attributes.temperature | float(0) ] %}
{% endfor %}
Overall Call for Heat: {{ true in ns.call_for_heat }}
1 Like

Good question, not sure…

Figured it would be good to see which heating zones are calling for heat. Next step will be to do automation to figure out some way to schedule times for specific radiators to go on.

Then finally, the plan is to control the Nests for the 2 boilers, to turn heating on/off by increasing the temperature (assuming we’re not out of the house)

I am also going to have the system message me when the kids increase the temperature in their rooms, and default back to the usual temp the day after… etc

It’s going to be a tricky one with likely several automations, this is the first step to work out which zones are calling for heat… next I need to work out if the call for heat is legitimate and realistic based on default temps and timings for each room

Thanks, really appreciate this I will try this, I wasn’t aware of the namespace requirement. I think the logic is better too, as the if statement is not required if I do it like this

Appreciate the tips!

I’m sorry, I left that out because I assumed you had put it in for purposes of showing us there was an issue… You can include the name by altering the earlier template a little bit:

{%- set heating_zone = 'chapel' %}
{%- set ns = namespace(call_for_heat = []) %}
{%- for state in states.climate | selectattr('attributes.heating_zone', '==', heating_zone) | selectattr('attributes.temperature', 'defined') %}
{%- set ns.call_for_heat = ns.call_for_heat + 
   [ [state.name, state.attributes.current_temperature | float(0) 
   <= state.attributes.temperature | float(0)] ] %}
{%- endfor %}
Who's Calling for Heat: {{ ns.call_for_heat|selectattr(1, 'eq', true)|map(attribute=0) | list }}
Overall Call for Heat: {{ true in (ns.call_for_heat|map(attribute=1)| list) }}

Thanks will have a play