Building a reusable template

In the spirit of DRY (Don’t repeat yourself), I’m looking to build a resusable sensor template. My suspicion is that this needs to be a custom component, but before I go down that route I want to ensure there isn’t another way.

I have the following repeated four times in my config, once for each member of the family. The only bit that changes is the proximity sensor (in this case proximity.ali_travel). Essentially, the sensor shows how far someone is from home, and changes the icon depending on at home, moving (away/towards home) or parked away from home.

So can this be packaged in such a way that it can be reused by just passing in the proximity sensor?

    ali_travel:
      friendly_name: Ali Travel
      value_template: >
        {% if states('proximity.ali_travel') == '0' -%}
          Home
        {%- else -%}
          {{ states('proximity.ali_travel') }} {{ state_attr('proximity.ali_travel', 'unit_of_measurement') }}
        {%- endif %}
      icon_template: >
        {% if states('proximity.ali_travel') == '0' -%}
        mdi:home-outline
        {%- elif state_attr('proximity.ali_travel', 'dir_of_travel') == 'stationary' -%}
        mdi:car-brake-parking
        {%- elif state_attr('proximity.ali_travel', 'dir_of_travel') == 'towards' -%}
          mdi:home-import-outline
        {%- elif state_attr('proximity.ali_travel', 'dir_of_travel') == 'away_from' -%}
        mdi:home-export-outline
        {%- else -%}
        mdi:car-off
        {%- endif %} 
1 Like

A Template Sensor’s YAML configuration doesn’t allow you to pass anything to it.

I realise that, that was why I was seeing if this could be moved into it’s own template somehow. I’m thinking my only option is to build a custom component.

There’s no native facility to template YAML configurations for Template Sensors.

FWIW, there’s a custom facility for Dashboard YAML configurations. Perhaps you can use that for inspiration while creating your custom component.

1 Like

You can try to do the same with blueprints and helpers maybe,

The issue remain the same, you will have duplicated automations.

Another option might be the use of AppDeamon

Neither are for creating Template Sensors (the example in the first post is a Template Sensor).

Reference: Blueprints

Sure it would be updating a sensor with an automation and not templating.

But there’s actually no integrates way to avoid repetition.

With repetitive configuration file, when there’s no way to avoid repetition I tend to generate my config file with a script, therefore even if the code repeat itself I do not repeat myself and edits are way faster.

So you can use dedicated yalm file generated programmatically

The request isn’t for updating a sensor.

The request is for a method to easily create multiple instances of a single Template Sensor configuration, each with only minor differences. Effectively, a means of templating YAML. There’s no native facility for that purpose.

1 Like

I ran into the same issue. As of HA 2023.4, macros work nicely to reduce code duplication in templates, be it with some minor drawbacks at the moment.

2 Likes