Hi,
I am attempting to setup several template sensors for air conditioner zones. There are 10 zones and I’d like to replicate10 sensors which are identical in structure but have information that is relevant to each zone. The entity/sensor has several attributes. The only thing that changes in each template definition is the zone name, which forms part of several of the entity names that the info is sourced from.
Is there a quick way of doing this?
I have looked at the macro function that is quite powerful, but from my experimentation is it can be used for a single data item i.e the state or attribute. What I’m looking at is defining a template for the whole entity (including attributes, icons, unique id etc.). Where I can pass the zone name as a parameter, say #zone, and it gets replaced through-out the syntax of the sensor config. Those of you familiar with C++ will know what I mean, but there is a pre-compiler step involved there.
Example config.
- name: "ac zone kitchen"
state: >
{% if (states('schedule.sleep_time')) %}
{% if (state_attr('climate.kitchen','current_temperature') | float >= state_attr('binary_sensor.ac_zone_sleep_time_temp_range_kitchen','upper') | float) %}
hot
{% elif (state_attr('climate.kitchen','current_temperature') | float <= state_attr('binary_sensor.ac_zone_sleep_time_temp_range_kitchen','lower') | float) %}
cold
{% else %}
normal
{% endif %}
{% else %}
{% if (state_attr('climate.kitchen','current_temperature') | float >= state_attr('binary_sensor.ac_zone_awake_time_temp_range_kitchen','upper') | float) %}
hot
{% elif (state_attr('climate.kitchen','current_temperature') | float <= state_attr('binary_sensor.ac_zone_awake_time_temp_range_kitchen','lower') | float) %}
cold
{% else %}
normal
{% endif %}
{% endif %}
icon: mdi:google-classroom
attributes:
current_temperature: >
{{ state_attr('climate.kitchen','current_temperature') | float }}
awake_time_temp_range_lower: >
{{ states.binary_sensor.ac_zone_awake_time_temp_range_kitchen.attributes.lower }}
awake_time_temp_range_upper: >
{{ states.binary_sensor.ac_zone_awake_time_temp_range_kitchen.attributes.upper }}
sleep_time_temp_range_lower: >
{{ states.binary_sensor.ac_zone_sleep_time_temp_range_kitchen.attributes.lower }}
sleep_time_temp_range_upper: >
{{ states.binary_sensor.ac_zone_sleep_time_temp_range_kitchen.attributes.upper }}
- name: "ac zone living"
state: >
{% if (states('schedule.sleep_time')) %}
{% if (state_attr('climate.living','current_temperature') | float >= state_attr('binary_sensor.ac_zone_sleep_time_temp_range_living','upper') | float) %}
hot
{% elif (state_attr('climate.living','current_temperature') | float <= state_attr('binary_sensor.ac_zone_sleep_time_temp_range_living','lower') | float) %}
cold
{% else %}
normal
{% endif %}
{% else %}
{% if (state_attr('climate.living','current_temperature') | float >= state_attr('binary_sensor.ac_zone_awake_time_temp_range_living','upper') | float) %}
hot
{% elif (state_attr('climate.living','current_temperature') | float <= state_attr('binary_sensor.ac_zone_awake_time_temp_range_living','lower') | float) %}
cold
{% else %}
normal
{% endif %}
{% endif %}
icon: mdi:google-classroom
attributes:
current_temperature: >
{{ state_attr('climate.living','current_temperature') | float }}
awake_time_temp_range_lower: >
{{ states.binary_sensor.ac_zone_awake_time_temp_range_living.attributes.lower }}
awake_time_temp_range_upper: >
{{ states.binary_sensor.ac_zone_awake_time_temp_range_living.attributes.upper }}
sleep_time_temp_range_lower: >
{{ states.binary_sensor.ac_zone_sleep_time_temp_range_living.attributes.lower }}
sleep_time_temp_range_upper: >
{{ states.binary_sensor.ac_zone_sleep_time_temp_range_living.attributes.upper }}
This is an example of two (of 10) zones - “kitchen” and “living”
I was able to use a macro for the “state”, which is quite handy as it is the most complex part. But would be great if I could replace text or even entities, for the whole template rather than each data item. This would make future maintenance easier as well.
I am a “tinkerer/hobbyist”. Any assistance on this is much appreciated - thanks in advance.