Template sensor: Possibility of templating the state trigger

I try to implement a kind of blueprint for a bench of template sensors dependant on HA integration sensors.

To be open for different hardware and different naming concentions I’ve defined global variables via a custom template 032-emhass.jinja in which I’m mapping the entity names used by the template sensors to the real entity names as defined by the different integrations.

My condensed 032-emhass.jinja custom_template (the real file is 20x bigger):

{%- macro globalVariables() %}
    {# 1 x Huawei SUN2000-6KTL-M1 (High Current Version) #}
    {# 1 x Huawei LUNA2000-10-S0 #}
    {# 1 x Huawei LUNA2000-5-S0 #}
    {{- {
            'inverter_rated_power': 6000.0,
            'inverter_maximum_active_power': 6600.0,
            'batteries_rated_capacity': 15.0,
            'batteries_rated_charge_energy': 17.250,
            'batteries_rated_discharge_power': 6600.0,
            'batteries_rated_charge_power': 7500.0,
            'batteries_soc_setpoint_low': 5.0,
            'batteries_soc_setpoint_neutral': 60.0,
            'batteries_soc_setpoint_high': 75.0,
            'epexspot_quantile_setpoint_low': 0.15,
            'epexspot_quantile_setpoint_neutral': 0.33,
            'epexspot_quantile_setpoint_high': 0.96,

            'inv_input_power': 'sensor.inverter_input_power',
            'inv_active_power': 'sensor.inverter_active_power',
            'grid_active_power': 'sensor.power_meter_active_power',
        }|to_json -}}
{%- endmacro %}

This approach is working very well by using below state triggers:

template:
  - trigger:
      - platform: state
        entity_id:
          - sensor.inverter_input_power
          - sensor.inverter_active_power
        not_from:
          - "unknown"
          - "unavailable"
          - "none"
        not_to:
          - "unknown"
          - "unavailable"
          - "none"
    action:
      - variables:
          var: >-
            {% from '032-emhass.jinja' import globalVariables %}
            {{ globalVariables()|from_json }}
          inv_input_power: "{{ states(var.inv_input_power)|int(0) }}"
          inv_active_power: "{{ states(var.inv_active_power)|int(0) }}"
          grid_active_power: "{{ states(var.grid_active_power)|int(0) }}"
          grid_export_power: "{{ max(0, grid_active_power) }}"
          grid_import_power: "{{ -min(0, grid_active_power) }}"
          house_power: "{{ inv_active_power - grid_active_power }}"
          house_power_with_correction: >-
            {% if house_power > 0 %}
              {{ house_power }}
            {% else %}
              {{ states(var.house_power)|float(0) }}
            {% endif %}
    sensor:
      #
      #  Sensor for House power
      # - Correction to avoid negative load
      #
      - name: house_consumption_power
        unique_id: house_consumption_power
        #friendly_name: "Power meter House power"
        state_class: measurement
        device_class: power
        unit_of_measurement: "W"
        icon: mdi:home-lightning-bolt
        state: "{{ house_power_with_correction }}"

Is there any possibility of templating the state trigger?

  - trigger:
      - platform: state
        entity_id:
          - sensor.inverter_input_power
          - sensor.inverter_active_power

I’ve tried the following but as the value template will only stay to true this doesn’t work:

  - trigger:
      - platform: template
        value_template: "{% if has_value('sensor.inverter_input_power') %}true{% endif %}"

Any help or workarround is appreaciated - Thanks in advance!

That depends, when do you want to trigger (i.e. what do you want the template to do)?

The template actions and updating of template sensors should be excecuted by all state changes as defined below:

  - trigger:
      - platform: state
        entity_id:
          - sensor.inverter_input_power
          - sensor.inverter_active_power
        not_from:
          - "unknown"
          - "unavailable"
          - "none"
        not_to:
          - "unknown"
          - "unavailable"
          - "none"

My vision:

  - trigger:
      - platform: state
        entity_id:
          - var.inv_input_power
          - var.inv_active_power
        not_from:
          - "unknown"
          - "unavailable"
          - "none"
        not_to:
          - "unknown"
          - "unavailable"
          - "none"