Move rule from *.rules file to UI

This rule works well in a .rules file. I have tried to move it to OH3 UI rules without success. I copied the If / Else part of the rule to the DSL script and added the object “Bathroom_Presence_Switch” to the When part. It does not work. The rule fires but the lights does not goes on.

rule "Bathroom night lights ON/OFF"

when
	Item Bathroom_Presence_Switch changed to ON
then	
	if (bathRoomLightTimer === null){
		Bathroom_FloorLight_switch.sendCommand(ON)
		bathRoomLightTimer = createTimer(now.plusMinutes(2)) [|
			bathRoomLightTimer = null
			Bathroom_FloorLight_switch.sendCommand(OFF)
		]
	} else {
		bathRoomLightTimer.cancel
		Bathroom_FloorLight_switch.sendCommand(ON)	
		bathRoomLightTimer=createTimer(now.plusMinutes(2)) [|
			bathRoomLightTimer = null
			Bathroom_FloorLight_switch.sendCommand(OFF)
		]	
	}		
end

Running OH 3.3.0.M1 on Rpi 4

I expect you have that defined elsewhere in your rules file? Unfortunately there is no ‘elsewhere’ to define ‘global’ variables in GUI rules. There’s no way round that in DSL rules. There are alternative near-equivalent approaches in other rules languages.

Ok, to bad. Any chans you can point me to some alternative?

May I suggest using jruby

Your rule above would translate to:

require 'openhab'

rule "Bathroom night lights ON/OFF" do
  changed Bathroom_Presence_Switch, to: ON
  run { Bathroom_FloorLight_switch.on for: 2.minutes }
end

Note you’d need to configure the jruby automation addon to install the openhab helper library, as per the installation steps: Installation | OpenHAB JRuby Script Library

Have a look into the blockly script action, where also a timer is available.

You can either use blockly in your rule, or reuse and adjust the code generated by blockly to write your own script

Hmmm, i must have missed some setting. Installed JRuby, added “openhab-scripting=~>4.0” to the JRuby settings (Ruby Gems), Restarted OH, added the rule script and trigger. I can see the rule fires but the “Bathroom_FloorLight_switch” item do not change state.

@maze sorry I didn’t get notification so I didn’t reply sooner.

Are you putting the rule example that I gave above inside a file? The file should be in <CONF>/automation/jsr223/ruby/personal/ and has a .rb extension, e.g. test.rb

If you just want to use the GUI to set up the rule + trigger (instead of using a rule file), then your rule body would just be:

require 'openhab'
Bathroom_FloorLight_switch.on for: 2.minutes

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.