Like most I have a number of Motion, Door & Window sensors. As I started out I created rules to cover each device or group of devices. Now that I’ve expanded my list of sensors around the house I’ve begun to work on improving my rules. The first of them is below.
Rather than creating loads of rules that are essentially the same I decided to standardise where I could. So all of my Item are now in the form of LOCATION _ PRODUCT TYPE _ FUNCTION. An example being: Landing_SP3102_Motion. The rule below works great and give me the results I was looking for. What I’d like to now add is a counter for each device, as per my commented lines in the rule below. What I need help with is how do I do this dynamically, similar to how I update the Last Activation item but in reverse?
Also I’d like to read your comments on where I could improve this rule or whether this is useful.
val String filename = "Sensors.rules"
var myTimer = null
val int vTimeout = 10
rule "Which Sensor?"
when
Member of gSensors changed
then
val vLocation = triggeringItem.name.split("_").get(0)
val vDevice = triggeringItem.name.split("_").get(1)
val vType = triggeringItem.name.split("_").get(2)
val vStatus = triggeringItem.state
var vItem = vLocation + "_" + vDevice + "_v" + vType
var vActivation = vLocation + "_" + vType + "_Last_Activation"
var vDate = new DateTimeType
if(vStatus == OFF && vType == "Motion") {
myTimer = createTimer(now.plusSeconds(vTimeout)) [|
if(myTimer !== null) {
if(vStatus == ON && vType == "Motion") return;
}
myTimer = null
]
}
sendCommand(vItem, vStatus.toString)
sendCommand(vActivation, vDate.toString)
logInfo(filename, "Sensor Status: {} {} Sensor has changed to {}",vLocation ,vType ,vStatus.toString)
// val counter = (Cloakroom_Door_Counter.state as Number) + 1
// Cloakroom_Door_Counter.sendCommand(counter)
end
Thanks for your reply @Burzin_Sumariwalla, I think you’ve misunderstood what I was asking. I’m looking to create Cloakroom_Door_Counter dynamically from the item that my rule is processing at the time.
What I want to do in the opposite, I want to obtain the current state of an item but creating the item dynamically.
So if for example my rule above is triggered by the Kitchen motion sensor I then want to retrieve the counter status from the item Kitchen_Motion_Counter.