How to Debug a Rule

I’ve seen a number of postings from users having problems with Rules who are not doing some of the minimal steps necessary to solve their problem. Here is a series of basic steps to perform when a Rule doesn’t work as expected.

This tutorial assumes the Rule is of correct syntax. Please use VSCode for Rules development people.

What

Your first goal in debugging a problem is to understand what it is currently doing. At this stage it doesn’t matter what you want it to do or what you expect it to do. What matters is what it is actually doing. Is the Rule even triggering? If it’s triggering, what lines of the Rule are actually executing? Is it

To figure out what the Rule is doing the easiest thing to do is to first look in openhab.log. Are there errors? If so you need to figure out what the errors mean and determine if they are the source of the problem. You might need to ask for help.

Next look in events.log. Are the Items receiving the updates or commands needed to trigger the Rule? If not then the problem is with the Item, Thing, or Binding configuration. If they all check out you might need to ask for help.

If everything indicates the Rule should be firing, verify that the Rule is indeed firing. Add a logging statement as the first line of code in the Rule. Watch openhab.log and verify the Rule is triggering.

Once you verify the Rule is triggering, interweave log statements as every other line of code so you know exactly what lines of code are executing.

Why

Now you know that the Rule is triggering and what lines of code are executing. Now we need to understand why.

Log out everything. Every variable and every Item’s state that is used in the Rule need to be logged out immediately before they are used in the Rule. This will tell you the state of everything.

Now you know the state of everything “execute” the Rule manually. If you can, walk through it in your head and perform all the steps mentally. If you can’t, do it on paper. There is a logic error somewhere and this will help you find where the computer is doing what you told it to do instead of what you wanted it to do.

If you need to, try simplifying the Rule. Remove conditions and loops and such until you get a Rule that works. Then gradually build the Rule back up, little by little until you find what breaks it or the whole Rule works.

If you still cannot get it to work as you want it, ask for help. When you ask, please provide all the details that you have gathered to this point so we too can walk through the code and mentally execute it and see the logic error.

Also be clear on what you are trying to accomplish. Avoid the XYProblem and be sure to tell us your overall goal with the Rule, not the specifics of how you are trying to debug this specific problem. There might be a better approach or there might be a problem with your overall approach which we cannot know without understanding your ultimate goal.

Conclusion

This process is not exhaustive. Depending on the nature of the problem there might be more analysis required. For example, Rules can interact with each other in unexpected ways. But these steps should help you diagnose and solve most simple Rule problems.

Assume Nothing
Verify Everything

3 Likes