I have this giant trigger-based sensor for my freezers. I have a bit of false positive issues that I want to trace to which issue caused it to trigger the alert. I want to set the attribute to a key of issue but I want to clear that issue attribute once the alert is false. How do I self reference the state of this entity when the template renders?
The main part I am focused on is the last part of this template sensor which is this part. Should i be using the is_state() or this.state?
Correction. Added bool filter as suggested by Didgeridrew. For some reason, the true/false values in an if-then-else are treated as strings instead of booleans.
Damn well you are a wizard harry. I’m going to test it tomorrow as my SO is asleep and can’t wake her up again lol or else i will have other alerts going off lol
OK so it didn’t work entirely well and i’m not sure why.
When i turn off the switch that powers the freezer i see issue: switch_off, but when i turn it back on it clears the issue so binary_sensor is false now, but issue now says switch_on which is odd since your if statement is testing if mode is true from what I can tell.
I had tested a simplified version of your Trigger-based Template Binary Sensor. It contained just enough to confirm that a variable defined in variables is computed after the trigger occurs and its value is accessible in state and attributes. In other words, it produces an updated value that’s accessible where needed for your application. (Compare that to this.state. It contains the entity’s state value before the trigger occurs; it’s the entity’s previous state)
If the end-result is that it’s not behaving as expected, then you’ll need to examine the state and issue templates.
For example, this.state represents the entity’s state value before the trigger occurred. Any template that uses it is checking the entity’s previous state. In other words or this.state == 'on' checks if the entity was on before the entity was triggered (not the computed new state but its previously computed state).
I don’t know if that’s responsible for the unexpected behavior you have observed, but it’s the kind of thing you need to check in the templates.
To make it easier to debug, I suggest you temporarily expose all the values, used in the templates, as attributes.
Your suggestion to cast the value with bool made me second-guess myself; is a variable’s type, such as boolean, preserved when used elsewhere in the Trigger-based Template entity?
I performed the following experiment.
I defined four variables in a Trigger-based Template Sensor.
Tried an Immediate If in your example and it returns booleans.
variables:
mode: |
{{ iif(now().minute is even, true, false) }}
It appears that native typing follows slightly different rules for values reported by an if-then-else. Unless the values are explicitly wrapped in double braces, true/false are reported as strings.
EDIT
I am beginning to suspect native typing may have a bug. If the same if-then-else is used to return numbers (i.e. replace true/false with 88/77, it correctly reports the values as integers (not strings).
mode: |
{% if now().minute is even %}
88
{% else %}
77
{% endif %}
Alsi confirmed it correctly reports [88] as a list. It’s true/false that it doesn’t automatically cast as boolean. That seems like an oversight.
I reported the issue and the observed behavior is normal.
When using if-else in a template, Home Assistant’s native typing feature recognizes True/False to represent booleans and true/false to represent strings.
In other words, if the text is in titlecase then it is understood to represent a boolean whereas if it’s in lowercase then it’s a string.
In the following example, the resulting value’s type will be string.