I try to create an automation to set the fan speed of an air purifier based on air quality. The first two options in the script are done via the visual editor, then I copy pasted in the YAML editor and adjusted the quality value and fan speeds. However when I try to save it I get the following error:
Message malformed: must contain at least one of below, above.
I’m not sure what exactly this means. All conditions have an ‘above’. AFAIK the trigger doesn’t need one since it simply triggers on any change.
Now it should work as I expected first, right? Whenever the value of sensor.filter_wz_pm25 changes the automation fires and depending on the value the fan speed is set, right?
I just realized that ‘for: “00:10:00”’ is useless here because it only checks if the state stays changed for 10 minutes which is always true. What I’d like to achieve is to change the fan speed only if the quality goes above the threshold for at least 10 minutes to avoid switching back and forth if the value hovers around the threshold.
So I guess what I need to do is set a timer in the condition instead of the trigger. I tried this:
That google search result is wrong. Look at the docs for what is allowed for numeric_state. For is not a valid option.
You will need to rework your automation to use numeric state in the triggers using multiple tirggerIds as well as both above AND below (your automation as it is will currently match all conditions if the state is above 5).
That’s not exactly how the for option works in a State Trigger.
The entity’s value must first change to a new value, then that new value must remain unchanged for the time period specified by for, then the trigger occurs.
In other words, the for’s countdown only begins when the entity’s value changes.
In addition to what ShadowFist explained, be careful not to make the following common mistake (condition makes a requirement that cannot be immediately fulfilled).
Trigger: When the sensor’s value changes
Condition: Confirm the new value is greater than 50 for 10 minutes.
Assume the sensor’s initial value is 48. It changes to 52 and triggers the automation. For how long has it been 52? Merely a few microseconds so it will not fulfill the Condition’s requirement of exceeding 10 minutes.
The sensor’s value will need to continue increasing, triggering the automation with each increase, (and never decreasing below 50) for at least the next 10 minutes before the Condition is finally met.
But that’s what I want. If goes above 50, stays above 50 for 5 minutes and then goes back below 50, then I don’t want to change the fan speed. If it goes from 48 to 52 and after 10 minutes is still above 50, then I want to increase the fan speed.
Might just be me misunderstanding.
But doesn’t it go from top to bottom and if it finds one true condition does the action and then stops until the next trigger?
So for a value of 40 it checks the first condition, that’s false, then the second condition, that’s true so speed is set to 66% and the automation ends.
I have the automation running for a while now and it’s doing exactly that. I just haven’t figured out yet how to set the higher speed of the value is over the threshold for a set amount of time.
I would not bet money this is always the case. All it takes is a race condition or simply one condition being dragged before the other in the UI, and you’ll be spending days troubleshooting.
Read up on what I mentioned earlier. If you do, you’ll be able to write a bulletproof automation, plus you’ll be able to use for: in the numeric state as a trigger.
A Condition doesn’t wait for something to happen. It checks if the entity’s value meets the requirements at the moment the test is performed.
If your automation uses a simple State Trigger to detect the sensor’s changes, it will trigger the moment it detects a state-change. Assume it’s initially 48 then changes to 52. The automation proceeds to evaluate the Condition you had proposed:
goes above 50, stays above 50 for 5 minutes
The Condition will not be met because the value just changed from 48 to 52. At the moment the test is performed, the value has been above 50 for mere microseconds, not 5 minutes. The Condition doesn’t wait for the value to remain above 50 for 5 minutes, it’s looking for the value to have already been above 50 for the past 5 minutes.
If you use the for option in a trigger, it behaves differently. It will wait for the new value to be above/below/unchanged (which one of those three depends on how the State or Numeric State Trigger is configured).
But if I just check the value with a trigger I would need one automation for every threshold. Or at least I haven’t seen anything to have three different triggers in one automation, each calling its corresponding action.
I can’t follow here. The moment it goes from 48 to 52 a test is performed and the condition isn’t met because the value is 52 but a second ago it was 48. That’s expected. 10 minutes pass, the value is still over 50, say 55, and didn’t drop since it has gone up from 48. A test is performed, the value is above 50, it was above 50 for the last 10 minutes, the condition is met and the action fires.
The condition is checked once immediately after the trigger and evaluated. It does not hang around for 10 minutes waiting.
If the condition is true the actions run and the automation ends. If it is false the actions do not run and the automation ends. 10 minutes later nothing happens.
You absolutely can have three numeric state triggers, each with a 10 minute for time. You can even run different actions depending on which trigger occurs if you want.
The automation triggers every time the quality changes which is every time HA gets an update from the purifier. The value fluctuates continuously running the automation over and over again, no?
But I do get why this is bad practice. It relies on the value fluctuating and it wouldn’t work with a binary sensor for example. Now that I know you can run a specific action depending on which trigger fires I know how to this better and more reliable. Thanks.
Thanks. I wouldn’t have though if just using an ID as the value for the fan speed. I guess usually you would use the ID for a choose function in the action?
If the value just changed (triggering the automation), then any condition looking for the value to be static for the past 10 minutes will be false and the actions will never occur.
It’s not. Changing to any value above 50 will trigger the automation. However changing from a value that is already above 50 to a higher value will not. Instead of “Numeric State Trigger” a more descriptive name for this trigger would be “Threshold trigger”, cross the threshold and it triggers. None of which has to do with the fact that conditions are only evaluated once after the trigger fires.