I need some help with one of my helpers because I currently just can’t wrap my head around how to fix the problem I have.
I have a threshold sensor helper that I use in an automation to switch heating between using our ACs and our combi boiler based on wether or not I have a power surplus coming from our PV system. Pretty simple helper, upper limit with a hysteresis to prevent the automation from switching too often in short succession.
My problem now is that the entity coming from the PV system that the threshold sensor uses as its base becomes unavailable about 2 or 3 dozen times a day for only a couple of seconds each. Which in turn means that the threshold sensor “becomes unknown” during these events.
I cannot figure out to filter out the state of “unknown” out of the threshold sensor helper. Today it actually happened that the helper was in state “on”, became “unknown” for almost exactly 30 seconds, and came back as “off”. Therefore, my automation didn’t catch it as I set the trigger for only firing when state goes from “on” to “off” or vice versa. Dropping the “from” is not desirable, because then the trigger would also fire everytime the threshold sensor comes from “unknown”.
I assume it can be solved by turning my threshold helper into a template, but I don’t even know where to start with that one.
Add the below as a Value Template in the Condition (And If) section
{{ trigger.from_state.state not in ['unknown', 'unavailable'] }}
Alternatively, switch to Yaml mode for your automation and replace the from with not_from Like the below example from the docs:
automation:
triggers:
- trigger: state
entity_id: vacuum.test
not_from:
- "unknown"
- "unavailable"
to: "on"
Unfortunately, not_to and not_from aren’t available in the visual editor - you have to switch to yaml mode to add them.
Note that the above suggestions are a workaround to deal with the symptom, rather than the cause of the issue. You should really be looking into fixing why your PV entity is going unavailable.
While this is elegant and I definately learned something I didn’t know here, I ended up implementing this solution: switch from a threshold sensor helper to a template binary sensor:
{% if states('sensor.pv_all_generators_power')|is_number %}
{% if states('sensor.pv_all_generators_power')|float <= 1750 %}
off
{% elif states('sensor.pv_all_generators_power')|float >= 2250 %}
on
{% else %}
{{ this.state }}
{% endif %}
{% else %}
{{ this.state }}
{% endif %}
Yep, it’s doing exactly what I want it to do: stay off until it reaches 2250W and stay on until it drops below 1750W. So basically the same as a threshold sensor helper with upper limit of 2000W and hysteresis of 250W.