I could not figure out, how I can use an ISO8601 period of time for comparison, i.e. if an item update happened longer ago that xyzy seconds.
Here is what I tried so far:
rules.JSRule({
name: "Teakettle",
description: "Turn teakettle mains supply off as soon as thermostate switched off when water is boiling",
triggers: [
triggers.ItemStateChangeTrigger("SchukoTeaKettleSW_State", "OFF", "ON"),
triggers.ItemStateUpdateTrigger("SchukoTeaKettleSW_Energy_Counter")
],
execute: (event) => {
items["SchukoTeaKettleSW_Energy_Counter"].history.lastUpdate())
// returns timestamp of the last persistet value, not the last change
// 2023-10-15T20:27:00.210+02:00[Europe/Berlin]
//console.log("Object with Last Change Time", items.SchukoTeaKettleSW_Energy_Counter.history.previousState(true));
// Returns the complete object, however, the timestamp shows the last change of the item
/* {
"rawState": {},
"state": "0.1213 kWh",
"numericState": 0.1213,
"quantityState": {
"raw": {}
},
"timestamp": "2023-10-15T10:57:00.160+02:00[Europe/Berlin]"
*/
var lastChangeTime = items.SchukoTeaKettleSW_Energy_Counter.history.previousState(true).timestamp;
// console.log("Last Change Timestamp", lastChangeTime);
var currentTime = time.ZonedDateTime.now(); // local SYSTEM time
// console.log("local DateTime", currentTime);
var iso8601Time = time.Duration.between(lastChangeTime, currentTime);
// console.log("Duration ", iso8601Time);
// produces "Duration PT338H12M57.806S" Period of Time 338 hours, 12 Minutes ...
var uxTimestamp = new Date(iso8601Time).getTime() / 1000;
// console.log("UX Timestamp ", uxTimestamp);
},
tags: ["Power", "Safety"],
id: "TeakettleOff"
});
What I could write to the log was:
Last Change Timestamp 2023-10-15T10:57:00.160+02:00[Europe/Berlin]
local DateTime 2023-11-02T15:53:15.992+01:00[SYSTEM]
Duration PT437H56M15.832S
UX Timestamp NaN
Yet I am not used to work with ISO8601 periods of time. My question is, how could I determine if the duration between two events is e.g. longer than 15 seconds? Conversion to unix timestamp in milliseconds?
P.S. the log outputs are not exactly accurate because I recorded them over time while tinkering with the code but they should help to see the formats.