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
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.
require 'openhab'
rule "Bathroom night lights ON/OFF" do
changed Bathroom_Presence_Switch, to: ON
run { Bathroom_FloorLight_switch.on for: 2.minutes }
end
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: