I have a number of battery driven sensors where I have noted, that even if they monitor battery% - at some point they tend to die without a fair warning (i.e. battery less than 30%).
I have an “alive” datetime-stamping of all recieved data from the sensors like this;
rule "maindoor doorsensor alive"
when
Item maindoor_Sensor received update or
Item maindoor_PowerAlarm received update or
Item maindoor_TamperAlarm received update or
Item maindoor_Temperature received update or
Item maindoor_Battery received update
then
postUpdate(maindoor_LastAlive, new DateTimeType())
end
I would like to make a daily cron-job to verify that the (in the case above) maindoor_LastAlive was updated within the last 24 hours. This way I could set up an action to warn me through twitter, mail etc. - that one of the sensors went “offline”.
Any good tips how to do that in a rule ? (tried searching, but came out empty)
As I am using persistance, I decided to go for this:
rule "Check sensor alives"
when
Time cron "0 0 0/6 1/1 * ? *" // Every 6 hours (generated with http://www.cronmaker.com/
then
logInfo("SENSOR", "Start sensor 24h alive checks")
var DateTime dateTime
var SimpleDateFormat df = new SimpleDateFormat( "YYYY-MM-dd HH:mm:ss" )
var String Timestamp = df.format( new Date() )
dateTime = new DateTime((maindoor_LastAlive.state as DateTimeType).calendar.timeInMillis)
if (dateTime.plusMinutes(1440).isBefore(now)) { // Check if alive message was received within the last 24 hours
// ... do something
}
end
This seems to work fine for my purpose. Could perhaps be done more elegantly…?
rule "maindoor doorsensor alive"
when
Member of gMainDoor received update
then
maindoor_LastAlive.postUpdate(new DateTimeType())
maindoor_Timer.sendCommand(ON)
end
rule "Door sensor has not reported in 24 hours"
when
Item maindoor_Timer received command OFF
then
//do something
end
With some clever naming you can make the above Rule work for all of your sensors. See the link from Vincent for details.