Can template sensors be 'scripted' or 'macro-tised' in any way?

I have 32 quite long template sensors which are almost identical.
Is it possible to ‘script’ or ‘macro-tise’ them in any way?
(I have to make a change and don’t want to have to make it 32 times!)

For example, this is an over-simplified case, (full example below)

sensor:
  - platform: template
    sensors:
      irrigation_cycle1_zone1_actual_duration_in_seconds:
        friendly_name: Cycle 1 Zone 1 Actual Duration In Seconds
        value_template: >
          {% set duration_secs = states('input_number.irrigation_cycle1_zone1_duration') | float * 60 %}
          {{ duration }}

In all 32 sensors, in the template, only the number of the cycle and the number of the zone change e.g. [cycle1, cycle2] and [zone1, zone2 ... zone16]

In an ideal world I’d like to do this (which obviously won’t work)

sensor:
  - platform: template
    sensors:
      irrigation_cycle1_zone1_actual_duration_in_seconds:
        friendly_name: Cycle 1 Zone 1 Actual Duration In Seconds
        value_template: >
          {% set cycle= '1' %}
          {% set zone = '1' %}
          !include sensor_template.yaml
          {{ duration }}

where sensor_template.yaml would be
{% set duration_secs = states('input_number.irrigation_cycle1_zone1_duration') | float * 60 %}

I doubt what I want to do is possible but I thought I’d ask.

Afterthought - Is it possible to reference in the template, the object_id of the sensor being templated? If so I could at least create all 32 templates the same and then edit one of them and copy and paste the rest.


This is an example of one of my actual sensors:

sensor:
  - platform: template
    sensors:
      irrigation_cycle1_zone1_actual_duration_in_seconds:
        friendly_name: Cycle 1 Zone 1 Actual Duration In Seconds
        value_template: >
          {% set duration_secs = states('input_number.irrigation_cycle1_zone1_duration') | float * 60 %}
          {% set min_duration_secs = states('input_number.irrigation_weather_adjusted_minimum_duration_in_seconds') | float %}
          {% set max_duration_secs = states('input_number.irrigation_weather_adjusted_maximum_duration_in_minutes') | float * 60 %}
          {% set today = ['mon','tue','wed','thu','fri','sat','sun'][now().weekday()] %}

          {# Adjust for rainfall #}
          {% if is_state('input_boolean.irrigation_cycle1_adjust_for_rainfall', 'on') %}
            {% set duration_secs = duration_secs * states('input_number.irrigation_rainfall_multiplier') | float %}
          {% endif %}

          {# Adjust for temperature #}
          {% if is_state('input_boolean.irrigation_cycle1_adjust_for_temperature', 'on') %}
            {% set duration_secs = duration_secs * states('input_number.irrigation_temp_multiplier') | float %}
          {% endif %}

          {% if is_state('input_boolean.irrigation_cycle1_adjust_for_rainfall', 'on') or
                is_state('input_boolean.irrigation_cycle1_adjust_for_temperature', 'on') %}
            {% if duration_secs > max_duration_secs %}
              {% set duration_secs = max_duration_secs %}
            {% elif duration_secs < min_duration_secs %}
              {% set duration_secs = min_duration_secs %}
            {% endif %}
          {% endif %}

          {% if is_state('input_boolean.irrigation_cycle1_zone1_every_day', 'on') or
                is_state('input_boolean.irrigation_cycle1_zone1_' ~ today, 'on') %}
            {% if is_state('input_boolean.irrigation_testing_mode', 'on') %}
              {{ (duration_secs / 60) | round() }}
            {% else %}
              {{ duration_secs | round() }}
            {% endif %}
          {% else %}
            0
          {% endif %}

Lovelace Gen should be able to do it:

I thought of that but isn’t that just for use in the Lovelace config?

Doh. Of course it is.

YAML anchors?

I had the exact same ‘doh’ moment when I thought about using it!

Check this out:

I think they only work within the ‘block’? i’e each sensor.

Will do, thanks.

No, I still think anchors only work within the ‘block’ (sorry I can’t think what the actual yaml name is, ‘map’ maybe?)

don’t make them individual sensors, make them an attribute. Then treat your zones & cycles as list attributes. Your template sensors will drop down to ~1 line for the value template, just grabbing the attribute & index.

1 Like

EDIT: I think the answer is yes.


This is a good idea. I’ve been playing with it quite successfully but I have quite a big package to retrofit it into.

One question,

Does a sensor listen for changes to entities used in the attribute_template the same way as it does for entities in the value_template or does it only update attributes when the state changes?