I recently wrote an automation for controlling a two-speed kitchen fan depending on humidity. Here is what I had to write:
If the first threshold was reached, but not the second, turn on the first speed.
If the second threshold was reached, turn on the second speed.
If the second threshold is no longer active, but the first one is, turn on the first speed.
If the first threshold is no longer active, turn off the fan completely.
What I would like to write:
The first speed is turned on if and only if the first threshold is active and not the second.
The second speed is turned on if and only if the second threshold is active.
What the hell?!?
The event-driven method is powerful, but it’s tedious and can easily introduce errors. Declarative automations would be easier to write and easier to keep correct.
One detail I didn’t include: I wanted to have hysteresis, so that small changes in humidity did not make the fan go dancing. I didn’t find a way to do that with numeric state triggers, so I went with threshold sensors.
So alternative title for this post could be, “WTH no hysteresis for numeric state triggers?”. But actually the problem still stands, as I could want more complex automations than a simple numeric trigger. For example: turn on the fan if and only if the humidity threshold is reached or the particulate sensor threshold is reached or the fan switch is on.
Usually if I find myself writing complex triggers like that I stop and move the logic to one or more template sensors instead. That way triggers can be simple and state-driven rather then dependent on the exact order state changes occurred in.
So in your case you could create a template sensor that is “fan state” and capture your logic in there. Then the actual state of your fan state sensor would be off, low, high. This way your automation can just be based on this sensor:
Trigger when sensor.fan_state changes
If sensor.fan_state = ‘high’ set fan to high
if sensor.fan_state = ‘low’ set fan to low
otherwise set fan off
Hello there! I also found declarative way more simple for automation description, so I’ve made custom package for myself and even published it to community.
If you are interested - topic is here (but it hasn’t drawn much attention from community)