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 %}