Conditions in Trigger-based Template Sensors

It would be great to be able to put conditions inside a trigger-based template sensor, so that whether the sensor updates could be conrolled by the state of something external.

Use case for me would be to e.g. have a sensor showing the first time that someone came home in the morning (triggered by presence, but only if the state is empty), or showing me the cost of electricity when my kettle is running, so that I can easily do some statistics on prices when I was using it, rather than at all times.

I support this request.

My use-case is to split a sensor into multiple sensors by means of a number of derived template sensors. This limits the number of external calls.

  # Template triggered P2000 sensors
  - trigger:
      - platform: template
        value_template: "{{ 'Ambulancediensten' in state_attr('sensor.p2000_zhz', 'discipline') }}"

    sensor:
      - unique_id: "p2000_ambulance"
        name:      "P2000 Ambulance"
        state:     "{{ states('sensor.p2000_zhz') }}"
        availability: "{{ (states('sensor.p2000_zhz') | selectattr('state', 'in', ['unavailable', 'unknown', 'none']) | list | count) == 0}}"

The present configuration only triggers on a change in the value_template. Therefore I would like to modify this into a state trigger with the present value_template as condition like this (shorthand notation):

  # Template triggered P2000 sensors
  - trigger:
      - platform: state
        entity_id: sensor.p2000_zhz

    conditions: "{{ 'Ambulancediensten' in state_attr('sensor.p2000_zhz', 'discipline') }}"

    sensor:
      - unique_id: "p2000_ambulance"
        name:      "P2000 Ambulance"
        state:     "{{ states('sensor.p2000_zhz') }}"
        availability: "{{ (states('sensor.p2000_zhz') | selectattr('state', 'in', ['unavailable', 'unknown', 'none']) | list | count) == 0}}"

My present ‘solution’ (work-around) is to check whether the sensor contains specific terms and store the result in a Boolean variable.

  # Template triggered P2000 sensors
  - trigger:
      - platform: state
        entity_id: sensor.p2000_zhz
        variables:
          ambulance: "{{ 'Ambulancediensten' in state_attr('sensor.p2000_zhz', 'discipline') }}"
          brandweer: "{{ 'Brandweerdiensten' in state_attr('sensor.p2000_zhz', 'discipline') }}"
          politie:   "{{ 'Politiediensten'   in state_attr('sensor.p2000_zhz', 'discipline') }}"

    sensor:
      - unique_id: "p2000_ambulance"
        name:      "P2000 Ambulance"
        state:     "{{ states('sensor.p2000_zhz') if ambulance }}"
        attributes:
          longitude:   "{{ state_attr('sensor.p2000_zhz', 'longitude')   if ambulance }}"
          latitude:    "{{ state_attr('sensor.p2000_zhz', 'latitude')    if ambulance }}"
          distance:    "{{ state_attr('sensor.p2000_zhz', 'distance')    if ambulance }}"
          capcode:     "{{ state_attr('sensor.p2000_zhz', 'capcode')     if ambulance }}"
          regio:       "{{ state_attr('sensor.p2000_zhz', 'regio')       if ambulance }}"
          regio name:  "{{ state_attr('sensor.p2000_zhz', 'regio name')  if ambulance }}"
          discipline:  "{{ state_attr('sensor.p2000_zhz', 'discipline')  if ambulance }}"
          attribution: "{{ state_attr('sensor.p2000_zhz', 'attribution') if ambulance }}"
          icon:        "{{ state_attr('sensor.p2000_zhz', 'icon')        if ambulance }}"
          time:        "{{ state_attr('sensor.p2000_zhz', 'time')        if ambulance }}"

However, while this does basically work, it throws a lot of ugly errors:

Logger: homeassistant
Source: helpers/trigger.py:73
First occurred: 20 april 2022 21:28:30 (20 occurrences)
Last logged: 00:22:30

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/trigger.py", line 73, in with_vars
    await action(run_variables, context)
TypeError: object NoneType can't be used in 'await' expression

Moreover, when an Boolean variable becomes False, the derived sensor becomes empty!
So template trigger conditions would still be very welcome!

BTW anybody who knows a better way to copy all those attributes?

this seems to be the same request as Add condition for trigger-based template sensors

2 Likes