[SOLVED] My First Rule: Any advice?

  • Platform information:

    • Hardware: AMD Ryzen Threadripper 1920X /32GB Ram
    • OS: Windows 10 Pro
    • Java Runtime Environment:Zulu 8
    • openHAB version: 2.4.0
  • Issue of the topic: I am trying to write my first rule, and have not been successful thus far. Nothing happens at all… Im just trying to get a relay to turn OFF when the motion alarm on the multisensor is tripped, and ON when the motion alarm resets to ok (have configured the return to OK interval for the motion alarm to be 10 seconds). I’m hoping to get some insight into whats going on, whether it be some sort of configuration issue or if I have made a mistake with syntax, or maybe something else. I can see the value for the motion sensor changing in Habmin when I wave my hand over it, and can manually command the relay to turn on and off through Habmin. There are no other rules in my rules folder, and all of the experimental rules that I have written in the Paper UI rule engine are disabled.

rule "Relay Control OFF"
when Item ZWaveNode002_MotionAlarm changed from OFF to ON
then
Item ZWaveNode003ZL7432InWallDualRelaySwitch_Switch1.sendCommand(OFF)
//logInfo("OFF", "Off command sent")
end

rule "Relay Control ON"
when Item ZWaveNode002_MotionAlarm changed from ON to OFF
then
Item ZWaveNode003ZL7432InWallDualRelaySwitch_Switch1.sendCommand(ON)
//logInfo("ON", "On command sent")
end

Items configuration related to the issue: ZWaveNode002_MotionAlarm is a switch type item, as is ZWaveNode003ZL742InWallDualRelaySwitch1

Hardware: Aeotech multisensor ZW100-A, Dual channel in wall relay ZL7432.

edit: I should also mention that this rule is saved as a .rules file (Alarm_Switch.rules) in the rules folder in the openHab directory, and that openHab has been running whenever I have tried to test the rule.

Shouldn’t that be when Item ZWaveNode002_MotionAlarm.status changed from OFF to ON ?
Do you see the status changing in the logs?

I have changed the code to look for ZWaveNode002_MotionAlarm.status, but I still dont see any commands being sent to the relay in the log files, or any indication that the rule is doing anything.

Edit: I can see the status of the motion alarm changing in the log files

Actually adding .status was incorrect.
What are your Item definitions? I would guess something like this: ( since you did not give yout Thing informtion, I used an example)

Switch ZWaveNode002_MotionAlarm {channel="zwave:device:16b83f77166:node2:alarm_motion"}

Make sure openHAB can load your xxx.rules file correctly.
There will be a message about it, good or bad, in openhab.log when you edit/save the file.

Make sure your rules don’t throw errors when you expect them to run. Again, look in openhab.log

At least take steps to find if if the rule gets started

rule "Relay Control OFF"
when
   Item ZWaveNode002_MotionAlarm changed from OFF to ON
then
   logInfo("test", "Off rule starting")
   ZWaveNode003ZL7432InWallDualRelaySwitch_Switch1.sendCommand(OFF)
   logInfo("OFF", "Off command sent")
end

Note that I have also removed the surplus Item keyword from the rule body, that is for the trigger section not the body. This would I’m sure have complained in your openhab.log

Note that your trigger conditions are very specific; changed OFF to ON will not be triggered by a change from NULL to ON for example.
That might be exactly what you want, or you could use changed to ON to generalize it a bit.

Ok, taking the superfluous Item keyboard from the rulebody does the trick! Did not realize that was not needed, thanks all for the help! Also had initially not realized that I needed to set up a seperate items file in the items folder (had originally just configured items in paper UI and Habmin).

Hi i did not read your issue as i dont know to write rules

but first thing that came into my head when you said my first rule
was this post, i am not using code for rules , but in the post below
you will get your head around, how write very good code based on patterns already wreitten by some genuis :slight_smile:

another thing that came to my head is node red, as you said this is your first rule, so you dont need to do any migration just start…
if you are Code noob like me you will find it very useful, i advise spending some time learing node red

good luck

You don’t have to use xxx.items files. Items are Items, whether you create them by file or by PaperUI.

That would be our Rich

3 Likes

In the docs, first examples:

2 Likes