How does a trigger for duration with condition work?

I have a dehumidifier on a smart socket, controlled by a hygrostat. If the dehumidifier is full, then it draws zero watts of power. I want to have an automation that sends a notification if the switch of the socket is on, and it’s drawing zero watts, for a duration of 10 minutes. That way, if somebody empties it before it is full (which will result in it drawing zero watts for a few minutes) there’s no notification.

I created an automation with a trigger when the power draw is < 1 watt for 10 minutes. This worked great before the dehumidifier was controlled by the hygrostat. Now I added the condition “when the switch is on”. However, I don’t know, and can’t find in the documentation, how the “for 10 minutes” condition of the trigger relates to the “switch must be on” condition.

Does anyone know how these interact? Do I need to repeat the “for 10 minutes” on the condition also?

My understanding is that when a trigger with a for condition trips, the logic goes something like this:

  • State crossed threshold
  • Start a timer for 10 minutes
  • During that 10 minutes, if the state crosses back across the threshold then abort
  • Otherwise, after the timer expires, trigger the automation

Is the condition also checked when the state crosses the threshold? If the condition stops being true at any time while the timer is running, does that abort the timer as well? Or does it check the condition only at the beginning and end of the timer?

Then, just for curiosity, what would happen if the condition had a 5 minute “for”? Would that mean it takes 15 minutes for the automation to run?

The automation will not trigger until the value has been less than 1 for a continuous 10 minutes. The condition will be checked only after that occurs.

Conditions are not evaluated until after a trigger occurs.

Thanks for the quick reply! Okay, so if I understood correctly, a 10 minute “for” on the trigger and a 5 minute “for” on the condition would mean the automation triggers 15 minutes after the state crosses the threshold.

Any advice on how to check if two things are true for 10 minutes?

Maybe using a trigger of “state below threshold” plus two conditions of “state below threshold for 10 minutes” and “switch on for 10 minutes” would get me the result I want?

No.

The condition does not wait.

It looks at the past history and checks if the switch has been in the required state for 5 minutes (continuously).

1 Like

Okay, that’s great, thanks. So then I could use “when power draw zero for 10 minutes” as the trigger and “switch on for 10 minutes” as the condition. Then both things would have to be true for the same 10 minute period for the automation to run. Did I get that right? Easy solution if so! :slight_smile:

Yes that is correct.

1 Like

Awesome, thanks a lot @tom_l, not the first time you’ve solved a home assistant problem for me, I appreciate it.

1 Like

Yes. But is that really the intended behavior? Even if both requirements happened at exactly the same time, HASS may not have registered them as such (state objects will not have the exact same timestamp). So you may be in a situation where power draw has been zero for 10 minutes, but the switch has “only” been on for say 9 minutes and 59 seconds, with the result that this automation will never run.

If what you really want is for the automation to run when both requirements have been true for 10 simultaneous minutes, regardless of which requirement may have been fulfilled “first”, then you need to add both of them as both a trigger and a condition.

@Mayhem_SWE Aye aye aye, now you got me thinking! Thanks for pointing out this potential issue.

The UI doesn’t offer the option to set a “for” on a threshold trigger, only for state triggers. But I assume I could add the same for syntax in yaml?

Then I guess I want the mode set to restart. That way if either of the triggers changes, the automation and the timer starts over.

Hmm, when I try to add a for to the threshold condition, I get an error. It looks like that’s not supported. The docs don’t list for as an optional key for a numeric_state condition. So in the condition when the socket has been on for 10 minutes, but power dropped to zero only 1 minute ago, the trigger would pass because it’s zero now.

I solved this for a whole group of devices with an extremely elaborate template described in this post, but that seems like overkill for this case. I was hoping there would be a simpler solution, but maybe I need to use the “trigger when template is true for 10 minutes” approach and check both things in the template.

Easier to say anything if you post your code. But AFAIK state and numeric_state triggers should both support for fully, but state conditions only support for on the entity state not an attribute. And yes unfortunately it looks like numeric_state conditions does not support for at all…

Which mode the automation is on only affects an automation once it reaches the actions. It has no effect whatsoever during the triggers or conditions phases.

Ah, that’s useful to know. Then I guess the mode doesn’t matter so long as I can figure out how to have the trigger and conditions work.

If the numeric_state condition doesn’t support a for parameter, then I can’t see how I could make this work except with a template as the trigger. The template trigger supports a for parameter so I can create a simple template that checks the two things and add a for condition to it.

Thanks @Mayhem_SWE for pointing out the issue with the previous approach, I missed that, and my automation wouldn’t have triggered under some conditions.