PIR sensor, timer rule won't work as intended

.sitemap

Switch item=Office_Manual_Mode label="Office" icon="motion" mappings=[OFF="Off", ON="On"]

.items

Switch Sensor_Office_Motion   		"Office Motion"		{ channel="zway:zwayDevice:192_168_0_222:24:sensorMotion-ZWayVDev_zway_24-0-48-1"}

Switch Office_Manual_Mode
Dimmer Dimmer_Office_Proxy	        "Office Main Proxy"	{ expire="1m,command=OFF" }
Dimmer Dimmer_Office 		        "Office Main "		{ channel="zway:zwayDevice:192_168_0_222:2:switchMultilevel-ZWayVDev_zway_2-1-38"}

.rules - I added a couple of missing “end” and yes 0-99 is correct

rule "Manual Dimmer Action"
when
	Item Dimmer_Office changed
then
	if (Dimmer_Office > 0) {
		// physical button pressed and it changed to ON
		Office_Manual_Mode.sendCommand(ON) // turn on manual mode only 
	}
	if (Dimmer_Office == 0) {
		// physical button pressed and it changed to OFF
		Office_Manual_Mode.sendCommand(OFF) // turn on automatic mode
	}
end

rule "Office Motion"
when 
	Item Sensor_Office_Motion changed
then
	// we agreed to send the commands everytime, even if the state is the same
	if (Sensor_Office_Motion == ON) {
		Dimmer_Office_Proxy.sendCommand(99) // help me out: could be 100 also?
		logInfo("Office motion", "ON")
	}
	if (Sensor_Office_Motion != ON) {  // remember there are other states like UNDEF
		Dimmer_Office_Proxy.sendCommand(0)
		logInfo("Office motion", "NOT ON")
	}
end

rule "Office Light"
when 
	Item Dimmer_Office_Proxy changed
then
	if (Office_Manual_Mode != ON) {
		Dimmer_Office.sendCommand(Dimmer_Office_Proxy)
	}
end

logs - rule switch in UI set to on with light physically off. Light does not come on with sensor

14:39:59.538 [INFO ] [marthome.event.ItemStateChangedEvent] - Sensor_Office_Motion changed from ON to OFF
14:39:59.552 [INFO ] [smarthome.event.ItemCommandEvent    ] - Item 'Dimmer_Office_Proxy' received command 0
14:40:02.929 [INFO ] [marthome.event.ItemStateChangedEvent] - Sensor_Office_Motion changed from OFF to ON
14:40:02.952 [INFO ] [smarthome.event.ItemCommandEvent    ] - Item 'Dimmer_Office_Proxy' received command 0
14:40:02.958 [INFO ] [smarthome.model.script.Office motion] - NOT ON
14:41:03.382 [INFO ] [smarthome.event.ItemCommandEvent    ] - Item 'Dimmer_Office_Proxy' received command OFF
14:41:06.299 [INFO ] [marthome.event.ItemStateChangedEvent] - Sensor_Office_Motion changed from ON to OFF
14:41:06.325 [INFO ] [smarthome.model.script.Office motion] - NOT ON
14:41:06.338 [INFO ] [smarthome.event.ItemCommandEvent    ] - Item 'Dimmer_Office_Proxy' received command 0
14:41:11.033 [INFO ] [marthome.event.ItemStateChangedEvent] - Sensor_Office_Motion changed from OFF to ON
14:41:11.064 [INFO ] [smarthome.model.script.Office motion] - NOT ON
14:41:11.069 [INFO ] [smarthome.event.ItemCommandEvent    ] - Item 'Dimmer_Office_Proxy' received command 0
14:41:24.480 [WARN ] [io.openhabcloud.internal.CloudClient] - Jetty request 5841820 failed: null
14:42:11.668 [INFO ] [smarthome.event.ItemCommandEvent    ] - Item 'Dimmer_Office_Proxy' received command OFF
14:42:28.386 [INFO ] [marthome.event.ItemStateChangedEvent] - Sensor_Office_Motion changed from ON to OFF
14:42:28.421 [INFO ] [smarthome.event.ItemCommandEvent    ] - Item 'Dimmer_Office_Proxy' received command 0
14:42:28.418 [INFO ] [smarthome.model.script.Office motion] - NOT ON
14:42:33.892 [INFO ] [marthome.event.ItemStateChangedEvent] - Sensor_Office_Motion changed from OFF to ON
14:42:33.922 [INFO ] [smarthome.event.ItemCommandEvent    ] - Item 'Dimmer_Office_Proxy' received command 0
14:42:33.920 [INFO ] [smarthome.model.script.Office motion] - NOT ON
14:43:03.242 [INFO ] [marthome.event.ItemStateChangedEvent] - Sensor_Office_Motion changed from ON to OFF
14:43:03.267 [INFO ] [smarthome.model.script.Office motion] - NOT ON
14:43:03.268 [INFO ] [smarthome.event.ItemCommandEvent    ] - Item 'Dimmer_Office_Proxy' received command 0

when I switch the light on or off manually or using the UI I get an error:

14:46:33.629 [INFO ] [marthome.event.ItemStateChangedEvent] - Dimmer_Office changed from 0 to 99
14:46:33.645 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Manual Dimmer Action': An error occurred during the script execution: The name '<XFeatureCallImplCustom> > <XNumberLiteralImpl>' cannot be resolved to an item or type.

Thought you missed .state or something but tried:

Dimmer_Office.sendCommand(Dimmer_Office_Proxy.state)

and got the same error message

I’m so sorry. Two traps I always tap into:

  1. always use .state. Always!
  2. let the expire turn the item off

so, with this .sitemap is cool and .items also. But please Change the rules:

rule "Manual Dimmer Action"
when
	Item Dimmer_Office changed
then
	if (Dimmer_Office.state > 0) { // please add .state here
		// physical button pressed and it changed to ON
		Office_Manual_Mode.sendCommand(ON) // turn on manual mode only 
	}
	if (Dimmer_Office.state == 0) { // please add .state here
		// physical button pressed and it changed to OFF
		Office_Manual_Mode.sendCommand(OFF) // turn on automatic mode
	}
end

rule "Office Motion"
when 
	Item Sensor_Office_Motion changed
then
	// we agreed to send the commands everytime, even if the state is the same
	if (Sensor_Office_Motion.state == ON) { // please add .state here
		Dimmer_Office_Proxy.sendCommand(99) // help me out: could be 100 also?
		logInfo("Office motion", "ON")
	}
	if (Sensor_Office_Motion.state != ON) {  // please add .state here // remember there are other states like UNDEF
		// remove this, as expire will do it
		logInfo("Office motion", "NOT ON")
	}
end

rule "Office Light"
when 
	Item Dimmer_Office_Proxy changed
then
	if (Office_Manual_Mode.state != ON) { // please add .state here
		Dimmer_Office.sendCommand(Dimmer_Office_Proxy.state)
	}
end

It is not working and i cannot quite get my head around why but there is an error

// when I switch light off with rule toggle set to off
15:28:28.831 [INFO ] [marthome.event.ItemStateChangedEvent] - Dimmer_Office changed from 99 to 0
15:28:28.869 [INFO ] [smarthome.event.ItemCommandEvent    ] - Item 'Office_Manual_Mode' received command OFF
15:28:28.892 [INFO ] [smarthome.event.ItemCommandEvent    ] - Item 'Dimmer_Office' received command 0
15:28:28.897 [INFO ] [marthome.event.ItemStateChangedEvent] - Office_Manual_Mode changed from ON to OFF

//when I move in front of sensor with rule toggle set to off
15:29:20.698 [INFO ] [marthome.event.ItemStateChangedEvent] - Sensor_Office_Motion changed from OFF to ON
15:29:20.731 [INFO ] [smarthome.model.script.Office motion] - ON
15:29:20.739 [INFO ] [smarthome.event.ItemCommandEvent    ] - Item 'Dimmer_Office_Proxy' received command 99
15:29:20.757 [INFO ] [marthome.event.ItemStateChangedEvent] - Dimmer_Office_Proxy changed from 0 to 99
15:29:20.781 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Office Light': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.sendCommand(org.eclipse.smarthome.core.items.Item,java.lang.String) on instance: null
15:29:44.394 [INFO ] [marthome.event.ItemStateChangedEvent] - Sensor_Office_Motion changed from ON to OFF
15:29:44.422 [INFO ] [smarthome.model.script.Office motion] - NOT ON
//when I move in front of sensor when light is off and with rule toggle set to on
15:33:10.003 [INFO ] [marthome.event.ItemStateChangedEvent] - Sensor_Office_Motion changed from OFF to ON
15:33:10.019 [INFO ] [smarthome.model.script.Office motion] - ON
15:33:10.025 [INFO ] [smarthome.event.ItemCommandEvent    ] - Item 'Dimmer_Office_Proxy' received command 99
15:33:10.030 [INFO ] [marthome.event.ItemStateChangedEvent] - Dimmer_Office_Proxy changed from 0 to 99
15:33:33.362 [INFO ] [marthome.event.ItemStateChangedEvent] - Sensor_Office_Motion changed from ON to OFF
15:33:33.394 [INFO ] [smarthome.model.script.Office motion] - NOT ON

The sensor does not always seem to report to OH so I’m struggling to pin the issue down. Can you see anything obvious?

What I’m a bit concerned about is this one:

15:29:20.781 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Office Light': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.sendCommand(org.eclipse.smarthome.core.items.Item,java.lang.String) on instance: null

This one indicates, that Dimmer_Office_Proxy changed to NULL, but milliseconds after is was changed to 99…?

But after a day (sorry, wasn’t online yesterday) I also get some logical mismatch in what I recommended you…

  1. rule “Manual Dimmer Action” - it will also fire, if the rule “Office Ligh” changes the item. My bad!
  2. first case is fine (turning ligh off manually)
  3. second case is fine also (excep the ERROR, which should also turn on Dimmer_Ofice
  4. third case ist fine (no automatic reaction to Dimmer_Office while manual mode on)

So, we have two issues left:

  1. how to avoid the ERROR
  2. how to not go into manual mode, if automatic Dimmer_Office is triggered

ad 1)
please add a logInfo right before switching the state like this:

logInfo("Office Motion", "Dimmer_Office: " + Dimmer_Office.state + ", Dimmer_Office_Proxy: " + Dimmer_Office_Proxy.state)

It should tell us both states right before we try to change Dimmer_Office (be sure to check for small typos in your rule…)

ad 2)
what we’d like to achieve: trigger Dimmer_Office via automatic and keep manual mode on. A dirty way is to change Office_Manual_Mode to ON after some time (I guess 500ms should suffice, but one second won’t hurt also).

rule "Office Light"
when 
	Item Dimmer_Office_Proxy changed
then
	if (Office_Manual_Mode.state != ON) {
		Dimmer_Office.sendCommand(Dimmer_Office_Proxy.state)
		// this will also trigger Manual_mode to OFF, so we just change it back (0,5 should suffice, could also be 1)
		timer = createTimer(now.plusSecods(0.5) | 
			Office_Manual_Mode.sendCommand(OFF)
			timer = null
		)
	}
end

I should think, there are other cleaner ways to achieve this… :wink: Perhaps someone helps us out here…

This one is a completeley different story. Does the Z-Wave gateway also have some kind of log? So, please just do some tests on this and post the logs filtered on the Sensor, if somethings unclear…

Hi, I’ve tried all sorts but without luck.

This is my rule now and it is not working. It looks like it has something to do with the")" or “|” but I’m very new to this so I’m unsure what to do, I’ve tried replacing a few things but I still get an error.

I’ve added the log info too but I’m unsure if it is in the correct place

rule "Manual Dimmer Action"
when
	Item Dimmer_Office changed
then
	if (Dimmer_Office.state > 0) {
		// physical button pressed and it changed to ON
		logInfo("Office Motion", "Dimmer_Office: " + Dimmer_Office.state + ", Dimmer_Office_Proxy: " + Dimmer_Office_Proxy.state)
		Office_Manual_Mode.sendCommand(ON) // turn on manual mode only 
	}
	if (Dimmer_Office.state == 0) {
		// physical button pressed and it changed to OFF
		logInfo("Office Motion", "Dimmer_Office: " + Dimmer_Office.state + ", Dimmer_Office_Proxy: " + Dimmer_Office_Proxy.state)
		Office_Manual_Mode.sendCommand(OFF) // turn on automatic mode
	}
end

rule "Office Motion"
when 
	Item Sensor_Office_Motion changed
then
	if (Sensor_Office_Motion.state == ON) {
		Dimmer_Office_Proxy.sendCommand(99)
		logInfo("Office motion", "ON")
	}
	if (Sensor_Office_Motion.state != ON) { // remember there are other states like UNDEF
		logInfo("Office motion", "NOT ON")
	}
end

rule "Office Light"
when 
	Item Dimmer_Office_Proxy changed
then
	if (Office_Manual_Mode.state != ON) {
		Dimmer_Office.sendCommand(Dimmer_Office_Proxy.state)
		// this will also trigger Manual_mode to OFF, so we just change it back (0,5 should suffice, could also be 1)
		timer = createTimer(now.plusSecods(0.5) | 
			Office_Manual_Mode.sendCommand(OFF)
			timer = null
		)
	}
end

I’ve done a little experimenting in an effort to try to understand exactly what the rule is doing so I’ve added my own comments.

I think I fixed the error that was stopping the rule to run. . I still get that error:

10:41:20.452 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Office Light': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.sendCommand(org.eclipse.smarthome.core.items.Item,java.lang.String) on instance: null

My new rules file

rule "Manual Dimmer Action"
when
	Item Dimmer_Office changed
then
	if (Dimmer_Office.state > 0) {
		// physical button pressed and it changed to ON
		logInfo("Office Motion", "Dimmer_Office: " + Dimmer_Office.state + ", Dimmer_Office_Proxy: " + Dimmer_Office_Proxy.state)
		Office_Manual_Mode.sendCommand(ON) // turn on manual mode only 
	}
	if (Dimmer_Office.state == 0) {
		// physical button pressed and it changed to OFF
		logInfo("Office Motion", "Dimmer_Office: " + Dimmer_Office.state + ", Dimmer_Office_Proxy: " + Dimmer_Office_Proxy.state)
		Office_Manual_Mode.sendCommand(OFF) // turn on automatic mode
	}
end

rule "Office Motion"
when 
	Item Sensor_Office_Motion changed
then
	if (Sensor_Office_Motion.state == ON) {
		// when motion is detected set the proxy item to 99
		Dimmer_Office_Proxy.sendCommand(99)
		logInfo("Office motion", "ON")
	}
	if (Sensor_Office_Motion.state != ON) {
		// if the motion state is not on (UNDEF/OFF) then log
		logInfo("Office motion", "NOT ON")
	}
end

rule "Office Light"
when 
	Item Dimmer_Office_Proxy changed
then
	if (Office_Manual_Mode.state != ON) {
		// if Manual Mode is not on then send the state of the Proxy item to the physical light
		Dimmer_Office.sendCommand(Dimmer_Office_Proxy.state)
		// this will also trigger Manual_mode to OFF, so we just change it back (0,5 should suffice, could also be 1)
		timer = createTimer(now.plusSecods(0.5) [| 
		Office_Manual_Mode.sendCommand(OFF)
			timer = null
		])
	}
end

please try to make as much debug-logging as possible:

rule "Office Light"
when 
	Item Dimmer_Office_Proxy changed
then
	logInfo("Office Motion", "Rule Office Light triggered")
	if (Office_Manual_Mode.state != ON) {
		logInfo("Office Motion", "Rule Office Light: Manual Mode ON; Dimmer_Office: " + Dimmer_Office.state + ", Dimmer_Office_Proxy: " + Dimmer_Office_Proxy.state)
		// if Manual Mode is not on then send the state of the Proxy item to the physical light
		Dimmer_Office.sendCommand(Dimmer_Office_Proxy.state)
		// this will also trigger Manual_mode to OFF, so we just change it back (0,5 should suffice, could also be 1)
		timer = createTimer(now.plusSecods(1) [| 
			logInfo("Office Motion", "Waited for Manual_Mode to change it back")
			Office_Manual_Mode.sendCommand(OFF)
		])
	}
end

the log should tell us some more info now.

Thanks, here is the log:

11:04:00.481 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'sensors.rules'
11:04:59.675 [INFO ] [marthome.event.ItemStateChangedEvent] - Sensor_Office_Motion changed from OFF to ON
11:05:01.870 [INFO ] [smarthome.model.script.Office motion] - ON
11:05:01.869 [INFO ] [smarthome.event.ItemCommandEvent    ] - Item 'Dimmer_Office_Proxy' received command 99
11:05:01.886 [INFO ] [marthome.event.ItemStateChangedEvent] - Dimmer_Office_Proxy changed from 0 to 99
11:05:01.894 [INFO ] [smarthome.model.script.Office Motion] - Rule Office Light triggered
11:05:01.906 [INFO ] [smarthome.model.script.Office Motion] - Rule Office Light: Manual Mode ON; Dimmer_Office: 99, Dimmer_Office_Proxy: 99
11:05:01.914 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Office Light': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.sendCommand(org.eclipse.smarthome.core.items.Item,java.lang.String) on instance: null
11:05:21.464 [INFO ] [marthome.event.ItemStateChangedEvent] - Sensor_Office_Motion changed from ON to OFF
11:05:21.482 [INFO ] [smarthome.model.script.Office motion] - NOT ON
11:05:32.212 [INFO ] [smarthome.event.ItemCommandEvent    ] - Item 'Office_Manual_Mode' received command ON
11:05:32.247 [INFO ] [marthome.event.ItemStateChangedEvent] - Office_Manual_Mode changed from OFF to ON
11:05:36.934 [INFO ] [marthome.event.ItemStateChangedEvent] - Sensor_Office_Motion changed from OFF to ON
11:05:36.947 [INFO ] [smarthome.event.ItemCommandEvent    ] - Item 'Dimmer_Office_Proxy' received command 99
11:05:36.947 [INFO ] [smarthome.model.script.Office motion] - ON
11:05:53.401 [INFO ] [marthome.event.ItemStateChangedEvent] - Dimmer_Office changed from 99 to 0
11:05:53.418 [INFO ] [smarthome.model.script.Office Motion] - Dimmer_Office: 0, Dimmer_Office_Proxy: 99
11:05:53.426 [INFO ] [smarthome.event.ItemCommandEvent    ] - Item 'Office_Manual_Mode' received command OFF
11:05:53.456 [INFO ] [marthome.event.ItemStateChangedEvent] - Office_Manual_Mode changed from ON to OFF
11:06:37.394 [INFO ] [smarthome.event.ItemCommandEvent    ] - Item 'Dimmer_Office_Proxy' received command OFF
11:06:37.410 [INFO ] [marthome.event.ItemStateChangedEvent] - Dimmer_Office_Proxy changed from 99 to 0
11:06:37.424 [INFO ] [smarthome.model.script.Office Motion] - Rule Office Light triggered
11:06:37.441 [INFO ] [smarthome.model.script.Office Motion] - Rule Office Light: Manual Mode ON; Dimmer_Office: 0, Dimmer_Office_Proxy: 0
11:06:37.448 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Office Light': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.sendCommand(org.eclipse.smarthome.core.items.Item,java.lang.String) on instance: null
11:06:47.690 [INFO ] [marthome.event.ItemStateChangedEvent] - Dimmer_Office changed from 0 to 99
11:06:47.706 [INFO ] [smarthome.model.script.Office Motion] - Dimmer_Office: 99, Dimmer_Office_Proxy: 0
11:06:47.718 [INFO ] [smarthome.event.ItemCommandEvent    ] - Item 'Office_Manual_Mode' received command ON
11:06:47.732 [INFO ] [marthome.event.ItemStateChangedEvent] - Office_Manual_Mode changed from OFF to ON
11:07:09.156 [INFO ] [smarthome.event.ItemCommandEvent    ] - Item 'Office_Manual_Mode' received command OFF
11:07:09.175 [INFO ] [marthome.event.ItemStateChangedEvent] - Office_Manual_Mode changed from ON to OFF
11:07:09.573 [INFO ] [marthome.event.ItemStateChangedEvent] - Sensor_Office_Motion changed from ON to OFF
11:07:09.587 [INFO ] [smarthome.model.script.Office motion] - NOT ON
11:07:17.883 [INFO ] [marthome.event.ItemStateChangedEvent] - Sensor_Office_Motion changed from OFF to ON
11:07:17.902 [INFO ] [smarthome.event.ItemCommandEvent    ] - Item 'Dimmer_Office_Proxy' received command 99
11:07:17.903 [INFO ] [smarthome.model.script.Office motion] - ON
11:07:17.925 [INFO ] [marthome.event.ItemStateChangedEvent] - Dimmer_Office_Proxy changed from 0 to 99
11:07:17.931 [INFO ] [smarthome.model.script.Office Motion] - Rule Office Light triggered
11:07:17.944 [INFO ] [smarthome.model.script.Office Motion] - Rule Office Light: Manual Mode ON; Dimmer_Office: 99, Dimmer_Office_Proxy: 99
11:07:17.949 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Office Light': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.sendCommand(org.eclipse.smarthome.core.items.Item,java.lang.String) on instance: null

I’ve updated OpenHAB as a just in case and because I needed to do it anyway. I’ve also done some tweaking and Googling but I still have an error.:confused:

I wonder if it is anything to do with the following:

I have finally created a solution that works :slightly_smiling_face:. Posting here for others and also in case someone has a better solution.

.items

Switch Switch_Office_Rule_Mode	    "Rule ON/OFF Switch"  //This is what goes in the site map
Switch Sensor_Office_Motion   		"Motion Detector"	  { channel="zwave:device:c4c50440:node24:sensor_binary"}
Switch Switch_Office_Proxy	        "Proxy for Expire"	  { expire="5m,command=OFF" } //Expire binding required
Dimmer Dimmer_Office                "Dimmer"			  { channel="zwave:device:c4c50440:node2:switch_dimmer1"}

.rules

rule "Sensor motion detection"
// When the SENSOR detects motion send the command to the PROXY
	when
		Item Sensor_Office_Motion changed
	then
		//logInfo("SensorI", "Motion: " + Sensor_Office_Motion.state + ", Rule: " + Switch_Office_Rule_Mode.state)
		if (Sensor_Office_Motion.state != OFF && Switch_Office_Rule_Mode.state != OFF) {
				Switch_Office_Proxy.sendCommand(ON) 
				Dimmer_Office.sendCommand(100)
		}
	else
		logInfo("SensorE", "Motion: " + Sensor_Office_Motion.state + ", Rule: " + Switch_Office_Rule_Mode.state)
	end

rule "Proxy expires dimmer"
// When the PROXY receives the OFF command via the EXPIRE binding, send an OFF command to the DIMMER
	when
		Item Switch_Office_Proxy received command OFF
	then
		if (Dimmer_Office.state > 0) {
				Dimmer_Office.sendCommand(0)
		}
	end
1 Like

Should be Sensor_Office_Motion.state != OFF.

Otherwise, it looks good. Thanks for posting!

1 Like

Well spotted, thanks for taking the time to check it. I’ve edited the above.