I am new to openHAB and trying to set up my first automation rules, but I have run into some issues.
Platform information:
Hardware: Raspberry Pi 4B (4GB RAM, 32GB SD Card)
OS: Raspberry Pi OS (Buster)
Java Runtime Environment: OpenJDK 11.0.10
openHAB version: 3.4.0 stable
Issue:
I am trying to configure a simple rule that triggers my smart lights when a motion sensor detects movement, but the rule is not being executed. The motion sensor seems to be sending data correctly, and the lights can be controlled manually through the UI, but the automation doesn’t seem to kick in.
sitemap demo label="Main Menu"
{
Switch item=Light_Switch
Text item=Motion_Sensor
}
Rules code:
rule "Turn on Light when Motion Detected"
when
Item Motion_Sensor changed from OFF to ON
then
Light_Switch.sendCommand(ON)
end
Logs:
2024-10-22 14:35:01.987 [INFO ] [smarthome.model.script.Rules ] - Turn on Light when Motion Detected triggered
2024-10-22 14:35:01.987 [WARN ] [org.openhab.core.model.script ] - Failed to execute rule: null
I also check this: https://community.openhab.org/t/help-with-setup-rules-in-openhab-for-momentary-buttonsqlikview But I have not found any solution. Could anyone guide me about this? I am not sure if I’m missing something obvious here. Could it be an issue with the syntax in the rule, or is there some additional configuration I need for the Z-Wave devices to work properly with automation?
Since you are just getting started I recommend against Rules DSL. As a language it ls quirky and not as capable as the other languages. If you are not proficient with programming I recommend Blockly. If you are I recommend JS Scripting or jRuby. Any of these will be as easy to use as Rules DSL but be much more capable.
For something like this, a simple UI rule is also an option.
I also recommend upgrading to OH 4 which includes a ton of additions and improvements to rules, especially Blockly.
As for the error, is this code in a .rules file or a managed rule (i.e. in the UI)? If the latter, the syntax is wrong. The rule and triggers are defined through the UI and the script action should just be:
Light_Switch.sendCommand(ON)
If the former, this might be something added in OH 4, but I expected to see the rule UID in the error message which, for Rules DSL is the name of the file and the position of the rule in that file, (e.g. the ID for the third rule in the file foo.rules will be foo-3).
It’s been too long now, I will be limited in what I can do to support OH 3. Thankfully upgrading to OH 4 is relatively painless, particularly if you’ve no rules to migrate. Depending on your add-ons you may not need to do anything at all except upgrade. The new upgradeTool might handle adjusting for all the breaking changes.
I have similar DSL rules that work fine. The only differences that I see between our rules is that I don’t have spaces in the rule’s name and I use “changed to ON” instead of “changed from OFF to ON” in the when clause but I would not expect either of these to be the issue. You might try adding some logging info to see if that sheds some light on the issue. Something like the following (untested).
rule LightOnMotion
when
Item Motion_Sensor changed from OFF to ON
then
logInfo("LightOnMotion","Rule running")
logInfo("LightOnMotion","Motion sensor previousState = {}, newState = {}", previousState, newState)
Light_Switch.sendCommand(ON)
logInfo("LightOnMotion","Rule finished")
end
On the motion, depending on the device it may be sending a notification and not triggering the sensor_binary. I’d try the alarm channel. Also sometimes there is a parameter that defines. This is for a Zooz ZSE18 parameter 19. What is your motion device?