A Template Sensor is created at startup based on whatever you had specified in its configuration. First the YAML is processed followed by the Jinja2 templates. To “parameterize” would require some sort of pre-processor for the Jinja2 processor (i.e. it doesn’t exist here so, no, you can’t “parameterize” it).
Based on what I’ve seen in your example, I suggest you consider creating a Template Binary Sensor (its meant for simply displaying on/off states).
template:
- binary_sensor:
- name: Area Family Room
state: "{{ area_entities('Family Room') | select('match', 'light') | select('is_state', 'on') | list | count > 0 }}"
attributes:
lights_count: "{{ area_entities('Family Room') | select('match', 'light') | list | count }}"
lights_on: "{{ area_entities('Family Room') | select('match', 'light') | select('is_state', 'on') | list | count }}"
shades_count: "{{ area_entities('Family Room') | select('match', 'cover') | list | count }}"
shades_open: "{{ area_entities('Family Room') | select('match', 'cover') | select('is_state', 'open') | list | count }}"
In addition, there’s no need for the templates to begin by filtering entire domains (like states.light
and states.cover
). Start with area_entities
for the desired area and then use match
for the desired domain.
BTW, you may have noticed there’s a fair bit of ‘template duplication’ and it would be nice if Template Sensors supported variables
like automation and scripts. If you like that idea, consider voting for the following Feature Request:
EDIT
Here’s a little trick I just tried and it appears to work. It might save you a few keystrokes when creating Template Binary Sensors for each area in your home.
Instead of explicitly specifying the area’s name in each template, use the self-referential this.name
For example, if the Template Binary Sensor’s name is simply the area name, like Family
, then the value of this.name
will be Family
wherever used in the template.
template:
- binary_sensor:
- name: Family
state: "{{ area_entities(this.name) | select('match', 'light') | select('is_state', 'on') | list | count > 0 }}"
attributes:
lights_count: "{{ area_entities(this.name) | select('match', 'light') | list | count }}"
lights_on: "{{ area_entities(this.name) | select('match', 'light') | select('is_state', 'on') | list | count }}"
shades_count: "{{ area_entities(this.name) | select('match', 'cover') | list | count }}"
shades_open: "{{ area_entities(this.name) | select('match', 'cover') | select('is_state', 'open') | list | count }}"
This makes the configuration a bit more generic and can be copy-pasted, to create another Template Binary Sensor, with minimal editing (just change the value of name
).
If you want the value of name
to be more descriptive, like Area Family Room
then you’ll need a bit more than merely this.name
because you’ll have to extract the string Family
out of Area Family Room
. For example:
template:
- binary_sensor:
- name: Area Family Room
state: "{{ area_entities(this.name[5:-5]) | select('match', 'light') | select('is_state', 'on') | list | count > 0 }}"
attributes:
lights_count: "{{ area_entities(this.name[5:-5]) | select('match', 'light') | list | count }}"
lights_on: "{{ area_entities(this.name[5:-5]) | select('match', 'light') | select('is_state', 'on') | list | count }}"
shades_count: "{{ area_entities(this.name[5:-5]) | select('match', 'cover') | list | count }}"
shades_open: "{{ area_entities(this.name[5:-5]) | select('match', 'cover') | select('is_state', 'open') | list | count }}"