Am I using private boolean properly?

Hi All

This is my second attempt to write this post (The first one got way too long) -- I'll try to be concise:

Problem: I am trying to use private booleans to trigger a rule after a delay and it does not appear to work.

In truth the way I am using the private boolean serves two purposes. Keep the rule from running 'too fast' (I have either a 15 minutes or 30 minute delay depending on fan speed) and that works well. What doesn't work is when the private boolean goes to 'true' it doesn't seem to trigger the rule.

I know this because I can see it in the data. The plot below shows the fan speed and temperature, as you can see the fan speed only changes with temperature and the private boolean never seems to trigger it (for instance that long run between 1:05 and 2:15 should have only lasted 30 minutes before the private boolean turned 'true' but the rule didn't fire again until 2:15 when the temperature nudged down a degree

My rule is below -- it is for controlling whole house fan (it's an Airscape fan -- I love them and they have local internet control!)

I think (?) I am using private booleans properly but it doesn't seem to be able to re-trigger a rule like I expected. Am I doing something wrong?

You can't trigger a rule with its own Private Boolean set by the rule. Another rule can set it, to cause a trigger. In effect, you're trying to cause the rule to rerun itself. You need to rethink how this should work.

Argh....

So just to be clear: there is no way to have a rule re-run itself in some future time? I guess I get that restriction from a re-entrance /infinite loop/race condition concern point of view but it's too bad -- I was pretty pleased with the elegance of this rule.

Anyway -- would an alternative be to use some sort of loop with a long delay? (this means the rule may be active for hours).

On a related note, I assume a rule can only be running once?
Meaning if I modify this rule to run over the course of hours to accomplish my goal, the rule system doesn't re-trigger the rule (it's sort of blocked until my instance completes if that makes sense) -- I've tried to avoid long running rules because I'm not entirely sure how the systems handles them.

Rules aren't "active" at all in that sense. When there is any sort of delay, including the delay of repeat loops, the rule creates a scheduled job and exits. It's quite common for rules to be "active for hours" in that sense.

Also, consider using Wait for Condition with a timeout...

Just a side issue on this one. Am I correct in thinking that if a repeat loop contained within an IF block is running. Then the subsequent IF's won't run until the loop has fully executed all of its cycles? Many thanks :ok_hand:

It doesn't matter where the repeat loop is, nothing after it will run until it completes. You do need to guard against multiple simultaneous triggering of the rule if you have a repeat inside an IF-THEN.

You can use Private Boolean to prevent multiple executions of the rule. If it is false at the beginning of the rule, exit the rule (Private Boolean starts out true). Then set it to false; set it to true at the end of the rule.

4 Likes

Awesome Bruce, awesome! Thanks for the advice. Easy when you know how :+1:

Iā€™ve seen this stated a few times in posts, but I donā€™t find this statement anywhere in the Documentation (I could have missed it, but, then again, Iā€™m legally blind).

Could someone please add a statement to this effect in the Documentation?

Also missing is a statement regarding things that could cause each Ruleā€™s Private Boolean to change (other than Set Private Booleans statements). For example, is the Private Boolean state preserved across Backup/Soft Reset/Restore? What about a clean Hub Shutdown or Restart?

Iā€™m sure such things are readily apparent to you because you wrote the code, but itā€™s a bit, um, opaque to those of us without access to the source.

Private Boolean can be set by the rule that it belongs to, and by other rules that reference it. Its value is true when a rule is created, and then takes its course as rule execution proceeds.

All application settings and state are preserved across reboots and generally across platform updates. However, if a database is restored, settings and state, including Private Boolean, will have whatever value that existed at the time the database was backed up.

Will fix the documentation.

3 Likes