I’m not sure what you mean.
Here is a simple example script for using Jython to get the length of time in milliseconds that an Item was in each it’s states within a given time period. SwitchItems are tricky, but I think I got them working with this too. I have only done some simple testing, so let me know if you find any issues. Should be enough to play with… and much easier than the rules DSL.
import json
from java.sql import Timestamp
from core.actions import Exec
from core.log import logging, LOG_PREFIX
LOG = logging.getLogger("{}.TEST_2".format(LOG_PREFIX))
def get_PERSISTED_DATA(item_name, time_start, time_end):
PERSISTED_DATA = Exec.executeCommandLine("/bin/sh@@-c@@/usr/bin/curl -s -X GET --header \"Accept: application/json\" \"http://localhost:8080/rest/persistence/items/{}?starttime={}&endtime={}\"".format(item_name, time_start, time_end), 5000)
return json.loads(PERSISTED_DATA)
ITEM_NAME = "DS_Pantry_Contact"
LOG.warn("ITEM_NAME: {}".format(ITEM_NAME))
CURRENT_TIME = DateTimeType().zonedDateTime
# Use this for today's values
# RAW_PERSISTED_DATA = get_PERSISTED_DATA(ITEM_NAME, CURRENT_TIME, CURRENT_TIME.withHour(0).withMinute(0).withSecond(0).withNano(0))
# Use this for all of yesterday's values
RAW_PERSISTED_DATA = get_PERSISTED_DATA(ITEM_NAME, CURRENT_TIME.withHour(0).withMinute(0).withSecond(0).withNano(0), CURRENT_TIME.minusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0))
LOG.warn("RAW_PERSISTED_DATA: {}".format(RAW_PERSISTED_DATA))
PERSISTED_DATA = RAW_PERSISTED_DATA["data"]
LOG.warn("PERSISTED_DATA: {}".format(PERSISTED_DATA))
IS_SWITCH = itemRegistry.getItem(RAW_PERSISTED_DATA["name"]).type == "Switch"
#LOG.warn("IS_SWITCH: {}".format(IS_SWITCH)
# create dict where keys are each possible state
summations = {}
keys = list(set([historic_item["state"] for historic_item in PERSISTED_DATA]))
#LOG.warn("{}".format(keys))
for key in keys:
summations[key] = 0
for index in range(1, int(RAW_PERSISTED_DATA["datapoints"])):
#LOG.warn("{}".format(PERSISTED_DATA[index]))
if not IS_SWITCH or (IS_SWITCH and (PERSISTED_DATA[index]["time"] != PERSISTED_DATA[index - 1]["time"]) and (PERSISTED_DATA[index]["state"] == PERSISTED_DATA[index - 1]["state"])):
time_start = Timestamp(PERSISTED_DATA[index - 1]["time"]).getTime()
time_end = Timestamp(PERSISTED_DATA[index]["time"]).getTime()
summations[PERSISTED_DATA[index]["state"]] += time_end - time_start
LOG.warn("summations: {}".format(summations))
The logs look like…
2020-05-19 18:01:34.775 [WARN ] [jython.TEST_2] - ITEM_NAME: Mode
2020-05-19 18:01:34.827 [WARN ] [jython.TEST_2] - RAW_PERSISTED_DATA: {u'datapoints': u'5', u'data': [{u'time': 1589713200000L, u'state': u'Morning'}, {u'time': 1589720400000L, u'state': u'Day'}, {u'time': 1589752500000L, u'state': u'Evening'}, {u'time': 1589763600000L, u'state': u'Night'}, {u'time': 1589770800000L, u'state': u'Late'}], u'name': u'Mode'}
2020-05-19 18:01:34.827 [WARN ] [jython.TEST_2] - PERSISTED_DATA: [{u'time': 1589713200000L, u'state': u'Morning'}, {u'time': 1589720400000L, u'state': u'Day'}, {u'time': 1589752500000L, u'state': u'Evening'}, {u'time': 1589763600000L, u'state': u'Night'}, {u'time': 1589770800000L, u'state': u'Late'}]
2020-05-19 18:01:34.828 [WARN ] [jython.TEST_2] - summations: {u'Late': 7200000L, u'Evening': 32100000L, u'Night': 11100000L, u'Morning': 0, u'Day': 7200000L}
2020-05-19 18:01:49.983 [WARN ] [jython.TEST_2] - ITEM_NAME: US_Pantry_Contact
2020-05-19 18:01:50.035 [WARN ] [jython.TEST_2] - RAW_PERSISTED_DATA: {u'datapoints': u'3', u'data': [{u'time': 1589740735000L, u'state': u'ON'}, {u'time': 1589740763000L, u'state': u'ON'}, {u'time': 1589740763000L, u'state': u'OFF'}], u'name': u'US_Pantry_Contact'}
2020-05-19 18:01:50.036 [WARN ] [jython.TEST_2] - PERSISTED_DATA: [{u'time': 1589740735000L, u'state': u'ON'}, {u'time': 1589740763000L, u'state': u'ON'}, {u'time': 1589740763000L, u'state': u'OFF'}]
2020-05-19 18:01:50.036 [WARN ] [jython.TEST_2] - summations: {u'OFF': 0, u'ON': 28000L}
2020-05-19 18:01:55.038 [WARN ] [jython.TEST_2] - ITEM_NAME: DS_Pantry_Contact
2020-05-19 18:01:55.090 [WARN ] [jython.TEST_2] - RAW_PERSISTED_DATA: {u'datapoints': u'0', u'data': [], u'name': u'DS_Pantry_Contact'}
2020-05-19 18:01:55.091 [WARN ] [jython.TEST_2] - PERSISTED_DATA: []
2020-05-19 18:01:55.092 [WARN ] [jython.TEST_2] - summations: {}
Areas that need some attention:
- If there are no state changes in the time period given
- The first state change of the day
- If it is a SwitchItem and there is just one change in the time period given