I need to check that the sensor is armed first, so I would imagine this is my rule then but it doesnt like
the state check for _Armed
16:01:20.775 [ERROR] [untime.internal.engine.RuleEngineImpl] - Rule 'Motion activated Lights using CCTV Cameras': The name '_armed' cannot be resolved to an item or type; line 13, column 9, length 6
rule "Motion activated Lights using CCTV Cameras"
when
Member of gMotionSensors received command ON
then
logInfo("Motion.rules", triggeringItem.name + " detected motion.")
if (vTimeOfDay.state == "EVENING" || vTimeOfDay.state == "NIGHT" || vTimeOfDay.state == "DAY") {
val source = triggeringItem.name.split("_").get(0)
val _Armed = gMotionSensors.members.findFirst[ armed | armed.name == source +"_Armed" ]
logInfo("Motion.rules", _Armed + " Sensor")
val _Light = gAllLights.members.findFirst[ light | light.name == source +"_Light" ]
logInfo("Motion.rules", _Light + " Light")
}
if (_Armed.state == ON){
sendCommand(_Light,ON)
createTimer(now.plusSeconds(60)) [ | {sendCommand(_Light,OFF)}]
}
end
The Motion sensor files the rule. The _Armed, switch item allows me to turn the rule on/off by arming/disarming the camera motion sensor Otherwise the lights will go on/off constantly when motion is detected.
If we are home, we will disengage the Arming, and that will not trigger the lights
Sorry if that doesnt make sense
So I need the Armed item to check its state before I send the command on for the light
16:21:30.935 [INFO ] [del.core.internal.ModelRepositoryImpl] - Refreshing model 'motion.rules'
16:21:33.265 [INFO ] [smarthome.event.ItemCommandEvent ] - Item 'NorthWestDrive_Motion' received command ON
16:21:33.307 [INFO ] [e.smarthome.model.script.Motion.rules] - NorthWestDrive_Motion detected motion.
16:21:33.310 [INFO ] [e.smarthome.model.script.Motion.rules] - NorthWestDrive_Armed (Type=SwitchItem, State=ON, Label=, Category=null, Groups=[gMotionSensors]) Sensor
16:21:33.311 [INFO ] [e.smarthome.model.script.Motion.rules] - NorthWestDrive_Light (Type=SwitchItem, State=OFF, Label=North West Exterior, Category=null, Groups=[gLights_Random, gAllLights, gInsideLights]) Light
16:21:33.313 [INFO ] [smarthome.event.ItemCommandEvent ] - Item 'NorthWestDrive_Light' received command ON
16:21:33.314 [INFO ] [arthome.event.ItemStatePredictedEvent] - NorthWestDrive_Light predicted to become ON
16:21:33.325 [INFO ] [smarthome.event.ItemStateChangedEvent] - NorthWestDrive_Light changed from OFF to ON
16:21:33.330 [INFO ] [home.event.GroupItemStateChangedEvent] - gAllLights changed from OFF to ON through NorthWestDrive_Light
16:21:33.331 [INFO ] [home.event.GroupItemStateChangedEvent] - gInsideLights changed from OFF to ON through NorthWestDrive_Light
I don’t think this got covered.
We’re exploiting the methods (functions) of an openHAB group Item here.
gMotionSensors.members
Groups have a bunch of methods to operate on group members.
That’s a bit vague, we need a specific operation.
gMotionSensors.members.findFirst[ ... ]
This is a more specific - find me the first member Item of this group.
Of course, “first” itself is vague - alphabetic? age? value?
findFirst wants you to provide some sorting criterion in the square brackets and will give you back the first thing that matches.
gMotionSensors.members.findFirst[ cam | ... ]
This is non-obvious.
We’re asking it to look through group members, which are Items. But we don’t know what these Items are called.
The cam bit in this example is a placeholder, referring to each Item in turn as we examine them. Call it what you like, this_one say.
gMotionSensors.members.findFirst[ cam | cam.name == xxx]
We need to do more than just refer to each Item, we are trying to do some kind of matching for the findFirst part.
The part after the | is our criterion. If our placeholder Item’s name matches some variable xxx - that’s the test.
item.name is a string, so our target xxx would need to be a string too.
We could do a similar search on item.state, for an example.
The name matching part here is using a string made up from the contents of a variable Source with a bit added on, the string "_Camera".
If we find anything that matches, we return it into a variable we’ve just created named _Sensor
Not that this _Sensor is an existing Item, one we’ve selected from a group. We can do anything with this that we can do with the “real” Item, like sendCommand.
Overall, the purpose here is to get hold of a real Item when we only have it’s name as a string.