If your persistence stategy for the item is everyChange, and you looked at your raw data, with the times converted so that they were human readable, it would look something like this…
Time | State |
---|---|
Sun Aug 05 2018 21:48:58 GMT-0400 (EDT) | OFF |
Sun Aug 05 2018 21:48:58 GMT-0400 (EDT) | ON |
Sun Aug 05 2018 21:45:22 GMT-0400 (EDT) | ON |
Sun Aug 05 2018 21:45:22 GMT-0400 (EDT) | OFF |
Sun Aug 05 2018 21:29:59 GMT-0400 (EDT) | OFF |
Sun Aug 05 2018 21:29:59 GMT-0400 (EDT) | ON |
Sun Aug 05 2018 21:26:01 GMT-0400 (EDT) | ON |
Sun Aug 05 2018 21:26:01 GMT-0400 (EDT) | OFF |
Sun Aug 05 2018 21:11:58 GMT-0400 (EDT) | OFF |
Sun Aug 05 2018 21:11:58 GMT-0400 (EDT) | ON |
Sun Aug 05 2018 21:08:16 GMT-0400 (EDT) | ON |
Now, I can’t think of any way to sum the differences of the ONs and OFFs through the current persistence extensions, so you’ll need to use the REST API to get the data, separate out the values, and add it up. Something like this…
val Integer dataPoints = Integer::parseInt(transform("JSONPATH", "$.datapoints", executeCommandLine("/bin/sh@@-c@@/usr/bin/curl -s http://localhost:8080/rest/persistence/items/DS_Kitchen_Motion",10000)))
val String data = executeCommandLine("/bin/sh@@-c@@/usr/bin/curl -s http://localhost:8080/rest/persistence/items/DS_Kitchen_Motion",10000)
val String firstValue = transform("JSONPATH", "$.data[" + (dataPoints - 1).toString + "].state", data)
var Number sumTimes = 0
for (var i = if (firstValue == "OFF") dataPoints - 1 else dataPoints - 3; i >= 2; i -= 4) {
sumTimes = sumTimes + Long::parseLong(transform("JSONPATH", "$.data[" + i.toString + "].time", data)) - Long::parseLong(transform("JSONPATH", "$.data[" + (i - 2).toString + "].time", data))
}
logDebug("Rules","Test: sumTimes=[{}], minutes=[{}]",sumTimes,(0.5 + (sumTimes / (1000 * 60))).intValue)
You will need to change the item in line 2 to one of your Switch items, and have the JSONPATH transform service installed. Then you could use the data in a rule or create an item to hold it.