Hello, I am newbie into the homeassistant as well as programming, but I have made some simple thing in my home automation and now I want to check if temperature sensor is not "Stucked" at same value. I try to use counting average of values inside array. But I keep failing to get the sum of values which results in reading the value as "NaN"
the code is here
[{"id":"ec5e2d42a7a7d10c","type":"group","z":"9612339af2b7a062","style":{"stroke":"#999999","stroke-opacity":"1","fill":"none","fill-opacity":"1","label":true,"label-position":"nw","color":"#a4a4a4"},"nodes":["d6c798ea44439013","3f9a57148997b2b5","4b9957a836089d27","9ceed4fa7b44f754","221b4840bbaa2af2","8a96f49405a2d131","70468ddeb3e03c94","14a4389d84d19d5d","d3342f56cda85414","2207a872a0db52f2","a767b2af1fd07b22","02f3b4631ed8305f"],"x":-6,"y":819,"w":1092,"h":488},{"id":"d6c798ea44439013","type":"api-call-service","z":"9612339af2b7a062","g":"ec5e2d42a7a7d10c","name":"notifikace na mobil","server":"35bd0ff1.ef0de","version":5,"debugenabled":false,"domain":"notify","service":"mobile_app_p30_pro_martin","areaId":[],"deviceId":[],"entityId":[],"data":"{\"message\":\"{{payload}} má asi nefunkční měření v čase {{cas}}\",\"title\":\"{{title}}\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"all","x":890,"y":1000,"wires":[[]]},{"id":"3f9a57148997b2b5","type":"moment","z":"9612339af2b7a062","g":"ec5e2d42a7a7d10c","name":"","topic":"","input":"cas","inputType":"msg","inTz":"Europe/Prague","adjAmount":"0","adjType":"hours","adjDir":"add","format":"HH:mm:ss","locale":"C","output":"cas","outputType":"msg","outTz":"Europe/Prague","x":960,"y":940,"wires":[["d6c798ea44439013"]]},{"id":"4b9957a836089d27","type":"comment","z":"9612339af2b7a062","g":"ec5e2d42a7a7d10c","name":"Kontrola funkčnosti čidla teploty","info":"","x":150,"y":860,"wires":[]},{"id":"9ceed4fa7b44f754","type":"inject","z":"9612339af2b7a062","g":"ec5e2d42a7a7d10c","name":"12 min","props":[{"p":"cas","v":"","vt":"date"},{"p":"title","v":"Nefunkční teploměr","vt":"str"}],"repeat":"720","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":120,"y":940,"wires":[["221b4840bbaa2af2"]]},{"id":"221b4840bbaa2af2","type":"change","z":"9612339af2b7a062","g":"ec5e2d42a7a7d10c","name":"teplota bojler?","rules":[{"t":"set","p":"bojler","pt":"msg","to":"tep_bojler","tot":"global"},{"t":"set","p":"pocitadlo","pt":"msg","to":"1","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"Bojler","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":940,"wires":[["d3342f56cda85414"]]},{"id":"8a96f49405a2d131","type":"link out","z":"9612339af2b7a062","g":"ec5e2d42a7a7d10c","name":"link out 3","mode":"link","links":["4230cb89beb71088","be25093ffe136f54"],"x":755,"y":940,"wires":[]},{"id":"70468ddeb3e03c94","type":"link in","z":"9612339af2b7a062","g":"ec5e2d42a7a7d10c","name":"link in 3","links":["56bbb26dcbfb594d"],"x":815,"y":940,"wires":[["3f9a57148997b2b5"]]},{"id":"14a4389d84d19d5d","type":"inject","z":"9612339af2b7a062","g":"ec5e2d42a7a7d10c","name":"reset počítadla o půlnoci","props":[{"p":"topic","vt":"str"}],"repeat":"","crontab":"00 00 * * *","once":false,"onceDelay":0.1,"topic":"reset","x":170,"y":1000,"wires":[["221b4840bbaa2af2"]]},{"id":"d3342f56cda85414","type":"function","z":"9612339af2b7a062","g":"ec5e2d42a7a7d10c","name":"function 3","func":"var myArray = context.get(\"myArray\");\nvar maxdelka = 5;\nvar soucet = context.get(\"sum\") || 1;\nvar i = context.get(\"i\") || 1 ;\n\nif (!myArray) {\n console.log(\"Set the array on context memory\");\n context.set(\"myArray\", []);\n myArray = context.get(\"myArray\");\n \n}\n\nmyArray.push(msg.bojler);\n\nif ( i <= maxdelka ) { \n i++;\n soucet += myArray[i];\n \n} \nelse {\n i++ ;\n myArray.splice(0 , 1);\n soucet += myArray[i];\n}\n\nmsg.prumer = soucet / myArray.length;\ncontext.set(\"sum\", soucet);\nmsg.soucet = soucet;\n\nmsg.delka = myArray.length;\nmsg.bojler = myArray;\n\ncontext.set(\"i\", i);\nmsg.i = i;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":940,"wires":[["a767b2af1fd07b22","2207a872a0db52f2"]]},{"id":"2207a872a0db52f2","type":"debug","z":"9612339af2b7a062","g":"ec5e2d42a7a7d10c","name":"debug 34","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":660,"y":1020,"wires":[]},{"id":"a767b2af1fd07b22","type":"switch","z":"9612339af2b7a062","g":"ec5e2d42a7a7d10c","name":"bojler <> průměr","property":"prumer","propertyType":"msg","rules":[{"t":"eq","v":"tep_bojler","vt":"global"}],"checkall":"true","repair":false,"outputs":1,"x":640,"y":940,"wires":[["8a96f49405a2d131"]]},{"id":"02f3b4631ed8305f","type":"group","z":"9612339af2b7a062","g":"ec5e2d42a7a7d10c","style":{"stroke":"#999999","stroke-opacity":"1","fill":"none","fill-opacity":"1","label":true,"label-position":"nw","color":"#a4a4a4"},"nodes":["b73222750ed45624","951f1ae8562c96c2","678faf3c98fdeacc","4230cb89beb71088","56bbb26dcbfb594d","e6c8689b81fa0af3"],"x":314,"y":1079,"w":492,"h":202},{"id":"b73222750ed45624","type":"switch","z":"9612339af2b7a062","g":"02f3b4631ed8305f","name":"počet >5","property":"pocitadlo","propertyType":"msg","rules":[{"t":"gt","v":"5","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":640,"y":1180,"wires":[["678faf3c98fdeacc","56bbb26dcbfb594d"]]},{"id":"951f1ae8562c96c2","type":"function","z":"9612339af2b7a062","g":"02f3b4631ed8305f","name":"Counter","func":"// Define Variable and get stored flow value\n// if there is no stored value, then the value is 0\nvar counter = flow.get(\"counter\") || 0\n\n// Check the msg.topic property\n// If the topic is equal to reset then set the counter to 0\n// else add 1 to the counter value\nif (msg.topic == \"reset\") {\n counter = 1\n} else {\n counter ++\n}\n\n// save the counter value in the flow variable\nflow.set(\"counter\", counter)\n\n// set msg.payload to counter value\nmsg.pocitadlo = counter\n\n// return msg Object\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":480,"y":1180,"wires":[["b73222750ed45624"]]},{"id":"678faf3c98fdeacc","type":"change","z":"9612339af2b7a062","g":"02f3b4631ed8305f","name":"reset","rules":[{"t":"set","p":"topic","pt":"msg","to":"reset","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":1240,"wires":[["951f1ae8562c96c2"]]},{"id":"4230cb89beb71088","type":"link in","z":"9612339af2b7a062","g":"02f3b4631ed8305f","name":"link in 2","links":["8a96f49405a2d131"],"x":355,"y":1180,"wires":[["951f1ae8562c96c2"]]},{"id":"56bbb26dcbfb594d","type":"link out","z":"9612339af2b7a062","g":"02f3b4631ed8305f","name":"link out 2","mode":"link","links":["70468ddeb3e03c94"],"x":765,"y":1180,"wires":[]},{"id":"e6c8689b81fa0af3","type":"comment","z":"9612339af2b7a062","g":"02f3b4631ed8305f","name":"počítadlo do 5ti","info":"","x":540,"y":1120,"wires":[]},{"id":"35bd0ff1.ef0de","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]