Files
ThingsBoard/Code Snippets/at_midnight.json
2025-10-16 17:01:21 -05:00

29 lines
1.3 KiB
JSON

{
"type": "SCRIPT",
"name": "At Midnight",
"debugSettings": {
"failuresEnabled": true,
"allEnabled": false,
"allEnabledUntil": 1759268749155
},
"configurationVersion": 0,
"configuration": {
"type": "SCRIPT",
"arguments": {
"week_volume": {
"refEntityKey": {
"key": "week_volume",
"type": "TS_ROLLING"
},
"limit": 202,
"timeWindow": 86400000
}
},
"expression": "var MS_IN_DAY = 24 * 60 * 60 * 1000;\n\nfunction mostRecentMidnight(referenceTs, offsetMinutes) {\n // “Floor” to midnight:\n var offsetMs = offsetMinutes * 60 * 1000;\n var localTs = referenceTs + offsetMs;\n var localMidnight = Math.floor(localTs / MS_IN_DAY) * MS_IN_DAY;\n return localMidnight - offsetMs;\n}\nfunction findValueAtMidnight(midnightTs){\n foreach (v : week_volume) { \n if (v.ts === midnightTs){\n return v.value;\n }\n }\n}\nvar tzOffset = -5 * 60;\nvar midnightTs = mostRecentMidnight(week_volume.timeWindow.endTs, tzOffset); // or timeWindow.endTs\nvar valueAtMidnight = findValueAtMidnight(midnightTs);\n\nreturn {\n \"week_volume_at_midnight\": valueAtMidnight\n};",
"output": {
"name": "",
"type": "TIME_SERIES",
"decimalsByDefault": 2
}
}
}