i’m using mysql for persistence and motion sensor that inputs values CLOSED and OPEN in database.
Now i need to get the time for last inserted CLOSED value and calculate time difference from now and that record to get
last motion detected?
Is that even possible?
Any suggestions?
Well, I don’t think there is a way to directly query the db for such values withing OH. However, you could easily create a rule for your motion sensor item when updated from OPEN to CLOSED. In there you can store the current timestamp in another item.
Contact motionSensor
DateTime motionSensorLastClosedDate
rule "last closed time"
when
Item motionSensor changed from OPEN to CLOSED
then
postUpdate(motionSensorLastClosedDate, new DateTimeType())
end
rule "last detected motion"
when
Item motionSensor changed from CLOSED to OPEN
then
// calculate the difference between now and motionSensorLastCLosedDate value
// do something with it
end
Now the above probably won’t work out of the box. I’m new to OH so there might be better ways, but this is what I would do at this point.
@vertom have a look at this post. It is working fine for my installation. If you have more then one persistence, you have to identify it with a second parameter: