[SOLVED] Rule when "changed to ON" doesn't execute

I want to turn on a light for 5 minutes when motion is detected while it’s dark.

Items:

Switch  Presence    "Presence"      {channel="deconz:presencesensor:36ac81a5:00158d0003059074010406:presence"}
Switch  Dark        "Dark"          {channel="deconz:lightsensor:36ac81a5:00158d0003059074010400:dark"}
Number  Brightness  "Brightness"    {channel="yeelight:stripe:0x0000000007c19a07:color"}

Rule:

var Timer motiontimer = null

rule "Motion sensor triggered"
when
    Item Presence changed to ON
then
  if (Dark === ON) {
    Brightness.sendCommand(75)
    motiontimer = createTimer(now.plusMinutes(5)) [|
    Brightness.sendCommand(0)]
  }
end

The rules file is loaded without any errors:

2019-08-28 23:11:20.939 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'BathroomLight.rules'

Dark is ON:
image

and Presence changes to ON:

2019-08-28 23:13:52.769 [vent.ItemStateChangedEvent] - Presence changed from ON to OFF

but the rule is not executed. What am I missing here?

I would suggest add some logging to spot your switches , or a manual switch to test your rule components, looks ok at a quick glance

In your rule, change to:

if (Dark.state === ON) {

Or just two == if Dark will never be null I believe. Also add an else clause, and log output to see if it’s always OFF for whatever reason

I think this is not correct try:

If(Dark.state == ON) {

And use == not ===

Let us know

Edit: oops too slow to write from smartphone…sorry chimera same post ^^

1 Like

Thanks guys. Amazing how much you forget in a few months if you are only at beginner level…

JFYI: Difference between == and ===:

if(a == b)  --> If a is the same value as b
if(a === b) --> If a is identical to b 

a and b are pointers to memory, so if the pointers point to the same memory cell, it will be identical. For example null is a special value (“no value”, to be exact). On the other hand, ON (and OFF, OPEN, CLOSED…) is a state in openHAB. So the === is not correct in this context.

3 Likes