I’ve got a bunch of door, window, temperature, and humidity sensors that I use rules to keep track of when they last updated. The scripts I use have the same rules with only the device names changing. This is getting hard to manage as I add more and more sensors. Here’s a peak at the one that handles temperature and humidity:
import org.openhab.core.library.types.*
rule "Update time/date for Temperature_Living_Update"
when
Item Temperature_Living received update
then
postUpdate(Temperature_Living_Update, new DateTimeType())
end
rule "Update time/date for Humidity_Living"
when
Item Humidity_Living received update
then
postUpdate(Humidity_Living_Update, new DateTimeType())
end
rule "Update time/date for Temperature_Master_Update"
when
Item Temperature_Master received update
then
postUpdate(Temperature_Master_Update, new DateTimeType())
end
rule "Update time/date for Humidity_Master"
when
Item Humidity_Master received update
then
postUpdate(Humidity_Master_Update, new DateTimeType())
end
rule "Update time/date for Temperature_Office_Update"
when
Item Temperature_Office received update
then
postUpdate(Temperature_Office_Update, new DateTimeType())
end
rule "Update time/date for Humidity_Office"
when
Item Humidity_Office received update
then
postUpdate(Humidity_Office_Update, new DateTimeType())
end
Item definitions looks like this:
/* ===================== Environmental ===================== */
Number Temperature_Living "LR Temperature [%.1f °F]" <temperature> (Living,Temperature,IndoorTemp) { mysensors="1;1;V_TEMP", imperihab="room:Living Room,label:Living Room Temperature,hygroId:Humidity_Living" }
DateTime Temperature_Living_Update "Last update [%1$tm/%1$td/%1$tY %1$tl:%1$tM %1$tp]" <status> (Living)
Number Humidity_Living "LR Humidity [%.1f %%]" <water> (Living,Humidity,IndoorHum) { mysensors="1;0;V_HUM" }
DateTime Humidity_Living_Update "Last update [%1$tm/%1$td/%1$tY %1$tl:%1$tM %1$tp]" <status> (Living)
Number Temperature_Master "Master Temperature [%.1f °F]" <temperature> (Master,Temperature,IndoorTemp) { mysensors="0;1;V_TEMP", imperihab="room:Master,label:Master Temperature,hygroId:Humidity_Master" }
DateTime Temperature_Master_Update "Last update [%1$tm/%1$td/%1$tY %1$tl:%1$tM %1$tp]" <status> (Master)
Number Humidity_Master "Master Humidity [%.1f %%]" <water> (Master,Humidity,IndoorHum) { mysensors="0;0;V_HUM" }
DateTime Humidity_Master_Update "Last update [%1$tm/%1$td/%1$tY %1$tl:%1$tM %1$tp]" <status> (Master)
Number Temperature_Office "Office Temperature [%.1f °F]" <temperature> (Office,Temperature) {mqtt="<[broker:/ESPTEMP1/DHTTest/Temperature:state:REGEX((.*))]"}
DateTime Temperature_Office_Update "Last update [%1$tm/%1$td/%1$tY %1$tl:%1$tM %1$tp]" <status> (Office)
Number Humidity_Office "Office Humidity [%.1f %%]" <water> (Office,Humidity) {mqtt="<[broker:/ESPTEMP1/DHTTest/Humidity:state:default]"}
DateTime Humidity_Office_Update "Last update [%1$tm/%1$td/%1$tY %1$tl:%1$tM %1$tp]" <status> (Office)
I did come across a post by @rlkoshak addressing this very thing:
I’d like to modify this for my purposes, but I’m having a heck of a time understanding how the .members.filter works. what would the ‘door|door.changedSince’ and ‘dt|dt.name’ be for my purpose? I’m interested in capturing all changes and not just when the door/window are opened but also when they are closed as well as any time temp/humidity is reported.
I’m guessing the rule for my temperature updates would be something like:
rule "A New Temperature Reported"
when
Item Temperature_Living received update or
Item Temperature_Master received update or
ItemTemperature_Office received update
then
// Get the temperature that changed
Temperature?.members.filter(Temperature|Temperature.changed.forEach[ Temperature |
// Do stuff
// Save the date/time for openings
Temperature?.members.filter(dt|dt.name == Temperature.name+"_Update").head.postUpdate(new DateTimeType)
]
end
Also is this line complete? It seems like it ends oddly:
gDoorSensors?.members.filter(door|door.changedSince(now.minusSeconds(1))).forEach[ door |
Can someone tell me how to set up the part that detects what member of the group has changed and the part that passes that (?) on to the part that updates the timestamp for that device?