I recently extracted a small pattern from my Home Assistant YAML configuration that might be useful to others.
Self-parameterized template sensors
When writing template sensors for multiple rooms, it’s common to end up duplicating the same logic:
- name: Temperature Level Bedroom
state: >
{% set t = states('sensor.temperature_bedroom') | float(none) %}
{% if t is none %} unknown
{% elif t < 18 %} cold
{% elif t < 24 %} ok
{% elif t < 27 %} warm
{% else %} hot {% endif %}
- name: Temperature Level Living Room
state: >
{% set t = states('sensor.temperature_living_room') | float(none) %}
{% if t is none %} unknown
{% elif t < 18 %} cold
...
As soon as the logic becomes slightly more complex, this duplication becomes hard to maintain.
The pattern
Instead of hardcoding the source entity, the template derives it from its own entity_id:
entity name → determines dependencies → runs generic logic
This effectively turns the entity naming convention into configuration.
{% set room = this.entity_id | replace('sensor.temperature_', '') | replace('_level', '') %}
{% set t = states('sensor.temperature_' ~ room) | float(none) %}
{% if t is none %} unknown
{% elif t < 18 %} cold
{% elif t < 24 %} ok
{% elif t < 27 %} warm
{% else %} hot
{% endif %}
Combined with a YAML anchor, this logic is written once. The result:
sensor.temperature_bedroom_level
sensor.temperature_living_room_level
sensor.temperature_office_level
→ same logic, no duplication
Each additional room is just a trigger block referencing the anchor. The template itself never changes.
Why this matters
- Fix the logic once — it’s fixed everywhere
- Add a room without touching the template
- Consistent behavior guaranteed across the entire sensor family
The same approach works for any repeated template logic: CO₂ status, humidity comfort, power classification, diagnostics.
Trade-off
The naming convention becomes part of the configuration contract. If it breaks, the template silently targets wrong sources.
Full explanation and working example:
Curious if others are using similar self-parameterized patterns, or if there are alternative approaches to avoid template duplication.

