// Bezieht Onlinezeit %/24h aus History-Daten und speichert sie stündlich in Datenpunkten. Vorher muss AddShellyStatesToHistory ausgeführt worden sein. Sendet eine Benachrichtigung bei weniger als 95%- run(); schedule('{"time":{"start":"00:00","end":"23:59","mode":"hours","interval":1},"period":{"days":1}}', async function () { run(); }); function run() { $('[id=shelly.0.*.online]').each(function(id, i) { setOnlinePercentage(id); }); } function setOnlinePercentage(onlineId) { var name = getState(onlineId.replace("online", "name")).val; var objectIdOutput = "0_userdata.0.ShellyDebug.OnlineTime." + name; createState(objectIdOutput, true, { "name": name, "type": "number", "desc": "", "read": true, "write": true, "unit": "%" }); addHistoryToState(objectIdOutput); var end = Date.now(); sendTo('history.0', 'getHistory', { id: onlineId, options: { start: end - 3600000*24, end: end, aggregate: 'none', count: 99999999 } }, function (result) { //console.log("#Results: " + result.result.length); var numberGood = 0; var numberBad = 0; for (var i = 0; i < result.result.length; i++) { if (result.result[i].val == true) { numberGood++; } else if (result.result[i].val == false) { numberBad++; } else { //console.log(result.result[i]); } } //console.log("#good: " + numberGood); //console.log("#bad: " + numberBad); var percentageGood = numberGood / (numberGood + numberBad) * 100 percentageGood = Math.round(percentageGood * 1000) / 1000; log(name + ": " + percentageGood + "%"); let stateBefore = getState(objectIdOutput).val; if (stateBefore >= 95 && percentageGood < 95) { sendTo("telegram", "send", { text: "Der Shelly " + name + " hat innerhalb der vergangenen 24 Stunden eine Onlinezeit von nur " + percentageGood + "%." }); } setState(objectIdOutput, percentageGood, true); }); } function addHistoryToState(state) { let hist = { "history.0": { "enabled": true, "aliasId": "", "debounceTime": 0, "blockTime": 0, "changesOnly": true, "changesRelogInterval": 1800, "changesMinDelta": "0", "ignoreBelowNumber": "", "disableSkippedValueLogging": false, "retention": "31536000", "customRetentionDuration": 365, "maxLength": 48, "enableDebugLogs": false, "debounce": "0" }}; let obj = getObject(state); let custom = obj.common.custom; let combined = { ...custom, ...hist }; log(state); log("custom: " + JSON.stringify(custom)); log("new: " + JSON.stringify(hist)); log("combined: " + JSON.stringify(combined)); obj.common.custom = combined; setObject(state, obj); // shows error but is no error }