When writing rules, whether they are simple ones with a single expression or more complex ones with multiple triggers and actions, it’s important to be able to check the behavior of your rule
and troubleshot it if needed.
The Rule Engine provides basic tools for debugging and monitoring your rules.
In this article we will guide you through these tools and explain how to use them efficiently.
The main debugging tools in the Rule Engine are:
rules
.console.log
method, which you can use within your JavaScript code.
More on this later.Before moving forward, check out the Getting Started guide. In this article, we’ll use the rule from that guide and simulate data output through device → data publish, as explained there.
To monitor the behavior of your rule, navigate to Rules → Tracing. Here’s a simple description of the UI:
Start Recording:
When you open the Tracing tab, it automatically fetches the most recent logs.
To continuously fetch new logs, click Start Recording.
You can stop recording at any time and review the output.
endpoint
that triggered the rule
.rule
execution from the rule page, meaning those that were not triggered by an endpoint
.Include Related Resources:
A single rule
usually consist of multiple actions
.
You can filter for a specific rule
and enable this option to include all logs from related actions
.
This option is available only when ‘Rule’ is selected as both the Entity Type and the Entity.
Basic Filters:
These filters help you find specific Rule Engine logs.
Here, you can filter by “Entity type” and “Entity”.
These specify the type and instance of what produced the trace, such as a Rule or an Action.
Click Start Recording, and if needed, filter by Entity type
and Entity
.
{
"temperature": 21
}
As shown in the screenshot, you should see the results:
In addition to the tracing feature, you can log data using console.log()
, just as you would in a typical JavaScript program, and view the output in the trace logs.
You may assume that you can use other JavaScript console methods like console.warn()
, console.error()
, or console.clear()
.
However, this is where confusion often arises.
IMPORTANT! In the Rule Engine, we do not have access to the standard JavaScript
console
object. Instead, a custom Javaconsole
object with a singlelog
method is provided.
This method has limitations:
- Accepts only strings: The
log
method accepts only strings, and the maximum allowed length is 10,000 characters.- Logging objects: If you log an object, it may not behave as expected. To ensure proper logging, it’s recommended to
JSON.stringify
any objects before logging them.
Note: This custom behavior applies only to the
console
andctx
, which are custom-injected Java objects. For all other objects and methods, JavaScript behaves as expected.
That’s the basic overview of how to debug Rule Engine scripts in Kaa platform. I hope this guide has been helpful in getting you started with debugging your rules.