hey @rlkoshak - thanks for this excelent piece of work.
i wrapped my head around this for days now but i cant it get to work as intended.
i used the version with the “expire” binding:
mebe you can help here
what i have:
a hue motion sensor, 2 hue bulbs in one lamp
what it does:
switch on light when someone enters, switch off after 1min, even if there is still movement in the room
what i want to achieve:
when someone enters the room:
switch on light
as long as there is movement detected:
leave on the light
1min after there is no movement detected:
switch off light
///////////////////////////////////
// Vorzimmer
///////////////////////////////////
rule "Vorzimmer_bewegung"
when
Item HueMotionVZ received update ON
then
MotionVZ.sendCommand(ON)
end
rule "VZ_anwesenheit"
when
Item MotionVZ received update ON
then
AnwesenheitVZ.sendCommand(ON)
end
rule "VZ lichtsteuerung_ON"
when
Item AnwesenheitVZ received update ON
then
Vorzimmer.sendCommand(ON)
end
rule "VZ lichtsteuerung_OFF"
when
Item AnwesenheitVZ received update OFF
then
Vorzimmer.sendCommand(OFF)
end
First understand what your motion sensor does. Does it ever send OFF, or only ON? Does it keep sending events during continuous motion, or just one ON at the beginning?
I traced the behaviour of my motion sensores with grafana. There is a special add on for on off states. Now i see every retrigger event and in addition the switch state of the light. So i could verify that after the motion sensor is not firing any more my 5 minute timer works
When motion is detected state.presence is set to true . At that point, motion sensing stops for a timeout of around 30 seconds (when state.presence becomes false and motion sensing starts again).
taken from: reddit/r/Hue
Well, you don’t have state.presence true or false available to you in openHAB rules. You’ll want to look in event.log and see what it (your Item) actually does.
this is the log when im entering the room, and running in circles for about 2mins.
could it be that the hue-binding translates this to ON/OFF?
side-note: somehow indmidst the process the connection to the bridge gets lost somehow
2019-02-14 00:58:00.099 [vent.ItemStateChangedEvent] - hue_0107_1_18_presence changed from OFF to ON
2019-02-14 00:58:00.105 [vent.ItemStateChangedEvent] - HueMotionVZ changed from OFF to ON
2019-02-14 00:58:00.147 [vent.ItemStateChangedEvent] - hue_0107_1_18_last_updated changed from 2019-02-14T00:20:38.000+0100 to 2019-02-14T00:57:59.000+0100
2019-02-14 00:58:00.153 [me.event.ThingUpdatedEvent] - Thing 'hue:0107:1:18' has been updated.
2019-02-14 00:58:00.162 [ome.event.ItemCommandEvent] - Item 'MotionVZ' received command ON
2019-02-14 00:58:00.171 [vent.ItemStateChangedEvent] - MotionVZ changed from OFF to ON
2019-02-14 00:58:00.208 [ome.event.ItemCommandEvent] - Item 'AnwesenheitVZ' received command ON
2019-02-14 00:58:00.223 [vent.ItemStateChangedEvent] - AnwesenheitVZ changed from OFF to ON
2019-02-14 00:58:00.239 [ome.event.ItemCommandEvent] - Item 'Vorzimmer' received command ON
2019-02-14 00:58:00.244 [ome.event.ItemCommandEvent] - Item 'VZ_Deckenlampe1_switch' received command ON
2019-02-14 00:58:00.247 [ome.event.ItemCommandEvent] - Item 'VZ_Deckenlampe2_switch' received command ON
2019-02-14 00:58:00.257 [nt.ItemStatePredictedEvent] - VZ_Deckenlampe1_switch predicted to become ON
2019-02-14 00:58:00.262 [nt.ItemStatePredictedEvent] - VZ_Deckenlampe2_switch predicted to become ON
2019-02-14 00:58:00.265 [vent.ItemStateChangedEvent] - VZ_Deckenlampe1_switch changed from OFF to ON
2019-02-14 00:58:00.270 [vent.ItemStateChangedEvent] - VZ_Deckenlampe2_switch changed from OFF to ON
2019-02-14 00:58:04.074 [vent.ItemStateChangedEvent] - hue_0100_ecb5fa052730_1_brightness changed from 0 to 100
2019-02-14 00:58:04.644 [vent.ItemStateChangedEvent] - VZ_Deckenlampe1 changed from 0 to 100
2019-02-14 00:58:04.658 [vent.ItemStateChangedEvent] - VZ_Deckenlampe2 changed from 0 to 100
2019-02-14 00:58:04.914 [vent.ItemStateChangedEvent] - hue_0106_1_19_illuminance changed from 0.9997697679981565 lx to 26.121613543992066 lx
2019-02-14 00:58:04.937 [vent.ItemStateChangedEvent] - hue_0106_1_19_last_updated changed from 2019-02-14T00:55:35.000+0100 to 2019-02-14T00:58:04.000+0100
2019-02-14 00:58:04.940 [me.event.ThingUpdatedEvent] - Thing 'hue:0106:1:19' has been updated.
2019-02-14 00:59:00.408 [ome.event.ItemCommandEvent] - Item 'AnwesenheitVZ' received command OFF
2019-02-14 00:59:00.413 [ome.event.ItemCommandEvent] - Item 'MotionVZ' received command OFF
2019-02-14 00:59:00.430 [vent.ItemStateChangedEvent] - AnwesenheitVZ changed from ON to OFF
2019-02-14 00:59:00.434 [vent.ItemStateChangedEvent] - MotionVZ changed from ON to OFF
2019-02-14 00:59:00.456 [ome.event.ItemCommandEvent] - Item 'Vorzimmer' received command OFF
2019-02-14 00:59:00.461 [ome.event.ItemCommandEvent] - Item 'VZ_Deckenlampe1_switch' received command OFF
2019-02-14 00:59:00.465 [ome.event.ItemCommandEvent] - Item 'VZ_Deckenlampe2_switch' received command OFF
2019-02-14 00:59:00.474 [nt.ItemStatePredictedEvent] - VZ_Deckenlampe1_switch predicted to become OFF
2019-02-14 00:59:00.489 [nt.ItemStatePredictedEvent] - VZ_Deckenlampe2_switch predicted to become OFF
2019-02-14 00:59:00.494 [vent.ItemStateChangedEvent] - VZ_Deckenlampe1_switch changed from ON to OFF
2019-02-14 00:59:00.498 [vent.ItemStateChangedEvent] - VZ_Deckenlampe2_switch changed from ON to OFF
2019-02-14 00:59:04.184 [vent.ItemStateChangedEvent] - hue_0100_ecb5fa052730_1_brightness changed from 100 to 0
2019-02-14 00:59:04.773 [vent.ItemStateChangedEvent] - VZ_Deckenlampe1 changed from 100 to 0
2019-02-14 00:59:04.783 [vent.ItemStateChangedEvent] - VZ_Deckenlampe2 changed from 100 to 0
2019-02-14 00:59:04.881 [vent.ItemStateChangedEvent] - hue_0106_1_19_illuminance changed from 26.121613543992066 lx to 0.9997697679981565 lx
2019-02-14 00:59:04.898 [vent.ItemStateChangedEvent] - HueIlluVZ changed from 26.121613543992066 lx to 0.9997697679981565 lx
2019-02-14 00:59:04.914 [me.event.ThingUpdatedEvent] - Thing 'hue:0106:1:19' has been updated.
2019-02-14 00:59:04.920 [vent.ItemStateChangedEvent] - hue_0106_1_19_last_updated changed from 2019-02-14T00:58:04.000+0100 to 2019-02-14T00:59:04.000+0100
2019-02-14 01:00:44.502 [hingStatusInfoChangedEvent] - 'hue:0100:ecb5fa052730:6' changed from ONLINE to OFFLINE: Die Hue Bridge meldet, dass die Lampe nicht erreichbar ist.
2019-02-14 01:00:50.805 [vent.ItemStateChangedEvent] - hue_0107_1_18_presence changed from ON to OFF
2019-02-14 01:00:50.814 [vent.ItemStateChangedEvent] - HueMotionVZ changed from ON to OFF
2019-02-14 01:00:50.846 [vent.ItemStateChangedEvent] - hue_0107_1_18_last_updated changed from 2019-02-14T00:57:59.000+0100 to 2019-02-14T01:00:50.000+0100
2019-02-14 01:00:50.851 [me.event.ThingUpdatedEvent] - Thing 'hue:0107:1:18' has been updated.
2019-02-14 01:00:50.855 [vent.ItemStateChangedEvent] - hue_0302_ecb5fa052730_17_temperature changed from 15.69 °C to 15.83 °C
2019-02-14 01:00:50.858 [vent.ItemStateChangedEvent] - HueTempVZ changed from 15.69 to 15.83
2019-02-14 01:00:50.861 [vent.ItemStateChangedEvent] - hue_0302_ecb5fa052730_17_last_updated changed from 2019-02-14T00:55:51.000+0100 to 2019-02-14T01:00:50.000+0100
2019-02-14 01:00:50.909 [me.event.ThingUpdatedEvent] - Thing 'hue:0302:ecb5fa052730:17' has been updated.
2019-02-14 01:00:54.528 [hingStatusInfoChangedEvent] - 'hue:0100:ecb5fa052730:6' changed from OFFLINE: Die Hue Bridge meldet, dass die Lampe nicht erreichbar ist. to ONLINE
2019-02-14 01:01:44.609 [hingStatusInfoChangedEvent] - 'hue:0100:ecb5fa052730:6' changed from ONLINE to OFFLINE: Die Hue Bridge meldet, dass die Lampe nicht erreichbar ist.
2019-02-14 01:01:45.049 [hingStatusInfoChangedEvent] - 'hue:0100:1:white-bulb-1' changed from ONLINE to OFFLINE: Die Hue Bridge meldet, dass die Lampe nicht erreichbar ist.
2019-02-14 01:01:54.631 [hingStatusInfoChangedEvent] - 'hue:0100:ecb5fa052730:6' changed from OFFLINE: Die Hue Bridge meldet, dass die Lampe nicht erreichbar ist. to ONLINE
2019-02-14 01:01:55.075 [hingStatusInfoChangedEvent] - 'hue:0100:1:white-bulb-1' changed from OFFLINE: Die Hue Bridge meldet, dass die Lampe nicht erreichbar ist. to ONLINE
2019-02-14 01:02:24.702 [hingStatusInfoChangedEvent] - 'hue:0100:ecb5fa052730:6' changed from ONLINE to OFFLINE: Die Hue Bridge meldet, dass die Lampe nicht erreichbar ist.
2019-02-14 01:02:25.121 [hingStatusInfoChangedEvent] - 'hue:0100:1:white-bulb-1' changed from ONLINE to OFFLINE: Die Hue Bridge meldet, dass die Lampe nicht erreichbar ist.
2019-02-14 01:02:54.758 [hingStatusInfoChangedEvent] - 'hue:0100:ecb5fa052730:6' changed from OFFLINE: Die Hue Bridge meldet, dass die Lampe nicht erreichbar ist. to ONLINE
2019-02-14 01:02:55.177 [hingStatusInfoChangedEvent] - 'hue:0100:1:white-bulb-1' changed from OFFLINE: Die Hue Bridge meldet, dass die Lampe nicht erreichbar ist. to ONLINE
Yes, that’s what bindings are for, matching various different outside devices to a common set of openHAB models.
So, looking at your 3 second snippet of events.log HueMotionVZ is your motion sensor
2019-02-14 00:58:00.105 [vent.ItemStateChangedEvent] - HueMotionVZ changed from OFF to ON
2019-02-14 01:00:50.814 [vent.ItemStateChangedEvent] - HueMotionVZ changed from ON to OFF
We’ve got only one set of changes to ON and back to OFF in the space of 2 seconds. But let’s assume for now that your Item does get both ON and OFF - and importantly, will go ON/OFF at some future motion. Whether or not it waits 30 seconds before the next signal we don’t know.
You’ve got an Item AnwesenheitVZ with an expire of 1m set (you can make that longer once its working)
And you’ve got this requirement
when someone enters the room:
switch on light
as long as there is movement detected:
leave on the light
1min after there is no movement detected:
switch off light
All you need are those two Items, and two rules
rule "Vorzimmer_bewegung"
when
Item HueMotionVZ received update ON
then
AnwesenheitVZ.sendCommand(ON) // start or restart expire timer
if (Vorzimmer.state != ON) {
Vorzimmer.sendCommand(ON) // turn light on
}
end
rule "AnwesenheitVZ expired"
when
Item AnwesenheitVZ received command OFF
then
if (Vorzimmer.state == ON} {
Vorzimmer.sendCommand(OFF)
}
end
rule "Turn off the light after no motion in 5 mins"
when
Item LR_MotionSensor_A received update ON
then
LR_MotionTimer.sendCommand(ON)
end
rule "Turn off the light when the timer is off"
when
Item LR_MotionTimer received command OFF
then
logInfo("living-room", "Turning OFF the Light ...")
// Turn OFF the light
end
Looking at the logs I see that the timer received the command ON, but never the OFF one
16:21:46.929 [INFO ] [smarthome.event.ItemCommandEvent ] - Item 'LR_MotionTimer' received command ON
16:21:46.933 [INFO ] [smarthome.event.ItemStateChangedEvent] - LR_MotionTimer changed from NULL to ON
@rlkoshak thank you very much for your DP (and JSR223) series - I’m learning new things every day.
I have one question to “Comprehensive example” - python part, I think @when part should be :
@when("Member of gMotionDetectors changed to ON")
instead of
@when("Item MotionDetector1 received update ON")
edit: I forgot comment // use an appropriate value
I get an error on one of the rules, I copy pasted it, only replaced the item with mine and replaced the minute setting to 1, it looks like this:
var Timer occupancyTimer = null
rule "MotionDetector1 received ON"
when
Item Philips_motion_Occupancy_WC received update ON
then
if(occupancyTimer === null || occupancyTimer.hasTerminated()) {
occupancyTimer = createTimer(now.plusMinutes(1), [|
Philips_motion_Occupancy_WC.sendCommand(OFF)
occupancyTimer = null
])
}
else {
occupancyTimer.reschedule(now.plusMinutes(timeoutMinutes ))
}
end
When the sensor receives an ON update, the rule gets triggered but won’t be fired because there is an error: 2019-09-26 14:43:09.126 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'MotionDetector1 received ON': The name 'timeoutMinutes' cannot be resolved to an item or type; line 14, column 52, length 14
I tried some things but nothing solved it, any idea what I could do?
This means that the variable timeoutMinutes is not defined as a global variable in your rules file. If i read it correct yout timeout value should be one minute. Then just replace timeoutMinutes with a 1 or define the variable above your rules
val timeoutMinutes = 1
To use the variable or better constant replace your 1 minute with timeoutMinutes. If you now like to change the one minute to 2 minutes the just adjust the timeoutMinutes to 2
Aaah now I understand, I added the val string and it is working now thanks a lot! For another sensor, I want the same rule but it should only operate between a specific time range.
How can I merge a time cron dependence on the rule?
If statements inside the Rule. See Design Pattern: Time Of Day for examples on how to do time comparisons or, since this is something you will want to do in lots of Rules, a good way to centrally keep track of times of day.