The addition of trigger-based template sensors has greatly reduced the need for automations and helpers to parse data and make it easily accessible. In Discord, I often see that users want to define a condition where, if false, the state of the template sensor will remain unchanged. This is often the case when graphing values, where a periodic zero value is unwanted, and an availability template would result in discontinuities.
I see all kinds of syntax errors as users attempt to do it with something like this via a template:
- trigger:
platform: state
entity_id: input_text.test
sensor:
- name: test
state: "{{ iif (trigger.to_state.state != 'unknown', trigger.to_state.state, states('sensor.test')) }}"
While it’s not a particularly difficult problem to solve, it’s not ideal for a fairly common task. First, since this
isn’t supported in trigger-based template sensors, it unfortunately requires the use of states('sensor.test')
to refer to the current value. Second, it requires some advanced templating that most users are not familiar with. Third, most users are unlikely to consider returning the current value of the sensor to leave the value unchanged, or to immediately know the syntax for doing so.
In contrast, adding a condition
block similar to an automation would make it simpler, more intuitive, and more consistent with other flows in HA:
- trigger:
platform: state
entity_id: input_text.test
condition:
not:
condition: state
entity_id: input_text.test
state: 'unknown'
sensor:
- name: test
state: "{{ trigger.to_state.state }}"
The condition block would be evaluated exactly as others in Home Assistant, where if the entire block evaluates to false, the state of the resulting sensor remains unchanged. Since the sensor didn’t actually change state, a graph of its value wouldn’t be affected by the trigger.
Simple versions of this could be handled in the trigger
with not_from:
and not_to:
, but that’s very limited compared to allowing for a condition
block.