SveinHa Skrevet 22. november Skrevet 22. november (endret) Har hatt et litt for komplisert program for å finne de laveste strømpristimene (etter litt for mange strømprisleverandører som dukker opp for så å forsvinne igjen...) så nå har jeg skrevet om og koden er blitt delelig med andre... Det kan fremdeles se litt komplisert ut men flyten er stort sett ovenfra og ned men om en gjør endringer i f.eks. timevalg må en gå litt tilbake og oppdatere uten å lage en endeløs loop. Funksjonen er rimelig enkel: Strømpriser hentes fra hvakosterstrommen.no (med manuelt alternativ fra NordPool) kl 13:02 og gjentas hvert 5. minutt inntil strømdata er mottatt. En kan velge et antall billige timer en trenger (2 alternativer) og er en innenfor de valgte timer slås der på globale variabler: lavPris og lavLavPris som kan benyttes fritt til å styre f.eks bereder. Systemet forholder seg kun til totalpris inkl avgifter og strømstøtte siden det er den vi virkelig betaler men de andre prisene vises også for informasjon. Systemet lagrer strømpriser for 72 timer og har en "Planleggingsperiode" fra kl 22:00 til 22:00. Denne perioden er valgt siden nettleien faller kl 22:00 i ukedagene og det kan være greit å få med seg. Det hele vises grafisk på en rimelig oversiktlig måte (mener jeg...). Om/Når nettleien endrer seg må det endres i noden "Lagre nettleie" og aktiviseres med trykk på "timestamp". Pr i dag ligger nettleie inne med 0.7 og 0.6 for hhv dag og natt/helg. Flowen ser slik ut: Skjermbilde med betjening (i dette bildet er med noen få tilleggselementer som er viktig i mitt system men som ikke har noe med strømpris å gjøre): Ant lave timer og Ant LavLav timer er valg for hvor mange timer en vil ha. Listboksene under viser de utvalgte timene inkl strømpris for hver time. Over listboksene vises status: Om en er innenfor eller utenfor de valgte timene. De utvalgte timene vises i grafen som lilla. "Vis kun resttimer" er et valg for å fjerne timer som er "brukt" fra listboksene. Dette er kun et visuelt valg og påvirker ikke annen funksjon. Graf Blå er planleggingsperioden og også markert inneværende time. Graf Oransje er total strømpris som vi betaler og resten i grafen sier seg selv med etikettene øverst. Visning av "Vis kun resttimer" og overgang til ny planperiode: Visning av når nye prisdata ankommer kl 13:02: [{"id":"8a9326e69a043d08","type":"delay","z":"b468e89a030cb946","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"minute","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"allowrate":false,"outputs":1,"x":1040,"y":840,"wires":[["143924fdaab67cff","cb7159478bedaaf1"]]},{"id":"178c4f3e51c168f5","type":"function","z":"b468e89a030cb946","name":"Refresh Dashboard items","func":"var m1 = {reset:true};\nreturn [[m1,msg]];","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":850,"y":840,"wires":[["8a9326e69a043d08"]]},{"id":"19314273e31cb034","type":"group","z":"b468e89a030cb946","name":"Strømpris chart","style":{"fill":"#e3f3d3","label":true,"color":"#000000"},"nodes":["d45591226bb68efc","1366923163fe3133","cb7159478bedaaf1","08a9ac90d5a7da54"],"x":14,"y":549,"w":752,"h":122},{"id":"d45591226bb68efc","type":"ui_chart","z":"b468e89a030cb946","g":"19314273e31cb034","name":"","group":"a51799506567a0f3","order":2,"width":"0","height":"0","label":"Strømplan 24/72 timer.","chartType":"line","legend":"true","xformat":"dd HH","interpolate":"step","nodata":"wait","dot":false,"ymin":"","ymax":"","removeOlder":"48","removeOlderPoints":"100","removeOlderUnit":"3600","cutout":0,"useOneColor":true,"useUTC":false,"colors":["#ff7f0e","#62a0ea","#edd400","#f7f7f7","#98df8a","#62a0ea","#813d9c","#9467bd","#c5b0d5"],"outputs":1,"useDifferentColor":false,"className":"","x":640,"y":590,"wires":[["b6089f059556a469","45a6be97437e5578","143924fdaab67cff"]]},{"id":"1366923163fe3133","type":"inject","z":"b468e89a030cb946","g":"19314273e31cb034","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[]","payloadType":"json","x":590,"y":630,"wires":[["d45591226bb68efc"]]},{"id":"cb7159478bedaaf1","type":"function","z":"b468e89a030cb946","g":"19314273e31cb034","name":"Prepare for 72h chart","func":"let markPlanPer = flow.get(\"prisMax\") + 0.2 || 1.5; // Høyde på markør for planperiode og inneværende time\nlet markSelHours = markPlanPer * 0.08; // Høyde på markør for planperiode og inneværende time\nlet markCurrentHour = markPlanPer * 0.98;\n\nlet prisMax = -99;\nlet prisMin = 99;\nlet prisSnitt = 0;\nlet dt = new Date();\n//Ny periode kl 22:00: Flytt dato til over midnatt for å sette periodestart og slutt.\nif (dt.getHours() >= 22) dt = new Date(Number(dt) + 4*60*60*1000);\n\nlet dt2 = Number(new Date(dt.getFullYear() + \".\" + (dt.getMonth()+1) + \".\" + dt.getDate() + \" 00:00\"));\nlet dtPerStart = dt2 - 2*60*60*1000;\nlet dtPerEnd = dt2 + 22*60*60*1000;\n\n//node.warn(dtPerStart);\n//node.warn(dtPerEnd);\n\n// Resett dt til riktig tid.\ndt = new Date();\n\n\ndt.setDate(dt.getDate());\nlet currHour = dt.getHours();\nlet currDate = dt.getDate();\nlet currMonth = dt.getMonth() + 1; // Mnd er 0-11 i utgangspunktet\nlet currYear = dt.getFullYear();\nlet currPlanPerStart = new Date()\n\nlet timerValgtLav = []\ntimerValgtLav = flow.get(\"timerValgtLav\");\nlet timerValgtLavLav = []\ntimerValgtLavLav = flow.get(\"timerValgtLavLav\");\n\nlet pdt=0; //PrisDenneTime for kalkulasjoner\nlet msg2 = [[],[],[],[]];\nmsg2.payload = [{ \"series\": [\"Jonaspris\", \"\", \"Råpris+nettleie og avgifter\",\"Råpris\",\"Nettleie\",\"\",\"Lave priser\",\"LavLave priser\"], \"data\": [[],[],[],[],[],[],[],[]], \"labels\": []}];\nlet pris72 = flow.get(\"Prisdata72\");\nlet i = 0;\n\nif (pris72 != null) {\n for (i = 0; i < pris72.length; i++){\n pdt = Number(pris72[i].pris);\n var priceHourTimestamp = new Date(pris72[i].ts);//new Date(pris48[i].timestamp);\n msg2.payload[0].data[0][i] = {\"x\": Number(priceHourTimestamp), \"y\": Number(pris72[i].jonaspris).toFixed(2)};\n msg2.payload[0].data[2][i] = {\"x\": Number(priceHourTimestamp), \"y\": Number(pris72[i].pristot).toFixed(2)};\n msg2.payload[0].data[3][i] = {\"x\": Number(priceHourTimestamp), \"y\": Number(pris72[i].pris).toFixed(2)};\n msg2.payload[0].data[4][i] = {\"x\": Number(priceHourTimestamp), \"y\": Number(pris72[i].nettleie).toFixed(2)};\n\n // Add \"Now\" marker\n if (Number(currDate) === Number(priceHourTimestamp.getDate() ) && Number(currHour) === Number(priceHourTimestamp.getHours())){\n msg2.payload[0].data[1][i] = {\"x\": Number(priceHourTimestamp), \"y\": markCurrentHour};}\n else{\n msg2.payload[0].data[1][i] = {\"x\": Number(priceHourTimestamp), \"y\": 0};}\n \n // Add \"Planning period\" marker\n if (pris72[i].ts >= dtPerStart && pris72[i].ts < dtPerEnd){\n msg2.payload[0].data[5][i] = {\"x\": Number(priceHourTimestamp) + 1, \"y\": markPlanPer};\n // Finn max, min og gjennomsnittspris\n if (Number(pris72[i].jonaspris) < prisMin) prisMin = Number(pris72[i].jonaspris);\n if (Number(pris72[i].jonaspris) > prisMax) prisMax = Number(pris72[i].jonaspris);\n prisSnitt += Number(pris72[i].jonaspris);\n //node.warn(i);\n }\n else{\n msg2.payload[0].data[5][i] = {\"x\": Number(priceHourTimestamp) + 2, \"y\": 0};\n }\n\n // Add \"Selected low hours\" marker\n let phts = Number(new Date(priceHourTimestamp));\n// let phtsFormatted = phts.getFullYear() + \"-\" + (\"00\" + (phts.getMonth() + 1)).slice(-2) + \"-\" + (\"00\" + phts.getDate()).slice(-2) + \" \" + (\"00\" + phts.getHours()).slice(-2) + \":\" + (\"00\" + phts.getMinutes()).slice(-2);\n// let exists = timerValgtLav.some(c => c.timestamp === phtsFormatted); //\"2024-11-06 02:00\");\n let exists = timerValgtLav.some(c => c.ts === phts); //\"2024-11-06 02:00\");\n if (exists){\n // node.warn(\"På\");\n msg2.payload[0].data[6][i] = {\"x\": Number(priceHourTimestamp), \"y\": markSelHours};}\n else{\n // node.warn(\"Av\");\n msg2.payload[0].data[6][i] = {\"x\": Number(priceHourTimestamp), \"y\": 0};\n }\n\n // Add \"Selected LowLow hours\" marker\n phts = Number(new Date(priceHourTimestamp));\n// phtsFormatted = phts.getFullYear() + \"-\" + (\"00\" + (phts.getMonth() + 1)).slice(-2) + \"-\" + (\"00\" + phts.getDate()).slice(-2) + \" \" + (\"00\" + phts.getHours()).slice(-2) + \":\" + (\"00\" + phts.getMinutes()).slice(-2);\n exists = timerValgtLavLav.some(c => c.ts === phts); //\"2024-11-06 02:00\");\n// exists = timerValgtLavLav.some(c => c.timestamp === phtsFormatted); //\"2024-11-06 02:00\");\n //node.warn(exists);\n if (exists){\n // node.warn(\"På\");\n msg2.payload[0].data[7][i] = {\"x\": Number(priceHourTimestamp), \"y\": markSelHours*.6};}\n else{\n // node.warn(\"Av\");\n msg2.payload[0].data[7][i] = {\"x\": Number(priceHourTimestamp), \"y\": 0};\n }\n \n\n }\n // Legg til time # 73 for bedre lesbarhet\n priceHourTimestamp = Number(priceHourTimestamp) + (1000 * 60 * 60)\n //node.warn(Number(priceHourTimestamp));\n msg2.payload[0].data[0][i] = { \"x\": Number(priceHourTimestamp), \"y\": Number(pris72[i-1].jonaspris).toFixed(2)};\n msg2.payload[0].data[1][i] = { \"x\": Number(priceHourTimestamp), \"y\": 0};\n msg2.payload[0].data[2][i] = { \"x\": Number(priceHourTimestamp), \"y\": Number(pris72[i-1].pristot).toFixed(2)};\n msg2.payload[0].data[3][i] = { \"x\": Number(priceHourTimestamp), \"y\": Number(pris72[i-1].pris).toFixed(2)};\n msg2.payload[0].data[4][i] = { \"x\": Number(priceHourTimestamp), \"y\": Number(pris72[i-1].nettleie).toFixed(2)};\n msg2.payload[0].data[5][i] = { \"x\": Number(priceHourTimestamp), \"y\": 0 };\n msg2.payload[0].data[6][i] = { \"x\": Number(priceHourTimestamp), \"y\": 0 };\n msg2.payload[0].data[7][i] = { \"x\": Number(priceHourTimestamp), \"y\": 0 };\n}\n\nprisSnitt = prisSnitt/24;\nflow.set(\"prisMin\", prisMin);\nflow.set(\"prisMax\", prisMax);\nflow.set(\"prisSnitt\", prisSnitt);\n\n// Fjern unødvendige/gamle data fra chart\nlet msg3 = [[],[]]\nmsg3.payload = msg2.payload;\nfor (i = 0; i < 20; i++){\n for (let j = 0; j<=7;j++){\n msg3.payload[0].data[j].shift();\n }\n}\n\n//node.warn(msg3.payload);\n\nmsg.payload = msg3.payload;\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":590,"wires":[["d45591226bb68efc"]]},{"id":"08a9ac90d5a7da54","type":"inject","z":"b468e89a030cb946","g":"19314273e31cb034","name":"Refresh charts","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"0 0-23 * * *","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":590,"wires":[["cb7159478bedaaf1"]]},{"id":"a51799506567a0f3","type":"ui_group","name":"Charts","tab":"a73dc047b0befb9e","order":3,"disp":false,"width":"30","collapse":false,"className":""},{"id":"a73dc047b0befb9e","type":"ui_tab","name":"Prisgraf v2","icon":"euro_symbol","order":7,"disabled":false,"hidden":false},{"id":"bb1f6387ef45151f","type":"group","z":"b468e89a030cb946","name":"Tabell laveste timer for batterilading","style":{"fill":"#e3f3d3","label":true,"color":"#000000"},"nodes":["3ea6f148d6030a9f","45a6be97437e5578","f944761036305ed5","7cc2fd4a050bccdd","3f26c24b2f14e081","60c75de6263a8794","50456aa1e7ddea01"],"x":14,"y":679,"w":1042,"h":142},{"id":"3ea6f148d6030a9f","type":"ui_table","z":"b468e89a030cb946","g":"bb1f6387ef45151f","group":"e9e1f6f856a0ef75","name":"Laveste LavLav priser tbl","order":9,"width":"5","height":"10","columns":[{"field":"timestamp","title":"Time","width":"65%","align":"left","formatter":"plaintext","formatterParams":{"target":"_blank"}},{"field":"jonaspris","title":"Pris","width":"28%","align":"right","formatter":"plaintext","formatterParams":{"target":"_blank"}}],"outputs":0,"cts":false,"x":920,"y":780,"wires":[]},{"id":"45a6be97437e5578","type":"function","z":"b468e89a030cb946","g":"bb1f6387ef45151f","name":"Hent valgte pristimer","func":"let liste2 = flow.get(\"liste2\") || false // Vis kun resttimer i listbox Lav og LavLav\nlet pdLow = flow.get(\"lavpris\");\nlet nmax = flow.get(\"AntLavLavTimer\");\nlet pdSelected = []\nlet pdSelDisp = []\nlet i = 0;\n// Klon objekt for å bryte forbindelsen til opprinnelige data for ellers vil opprinnelige data endres\nlet newpdLow = RED.util.cloneMessage(pdLow); \nlet dt = new Date();\n\n\nfor (i = 0; i < nmax;i++){\n pdSelected.push(newpdLow[i]);\n}\nflow.set(\"timerValgtLavLav\", pdSelected);\n\n// Fjern foregående timer fra tabell dersom valgt\nfor (i = 0; i < nmax;i++){\n if (liste2){\n if (pdLow[i].ts >= (Number(dt) - 60*60*1000)){\n pdSelDisp.push(newpdLow[i]);\n }\n }else{\n pdSelDisp.push(newpdLow[i]);\n }\n//node.warn(pdLow[i].ts);\n//node.warn(Number(dt));\n\n}\n\n// Pynt dataene for tabellvisning\nfor (i = 0; i < pdSelDisp.length;i++){\n pdSelDisp[i].timestamp = new Date(pdSelDisp[i].ts).toLocaleString(\"nb-NO\").slice(0,-3) ;\n pdSelDisp[i].jonaspris = parseFloat(pdSelDisp[i].jonaspris).toFixed(2);\n}\n//node.warn(pdSelDisp),\nmsg.payload = pdSelDisp;\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":430,"y":780,"wires":[["60c75de6263a8794"]]},{"id":"f944761036305ed5","type":"ui_numeric","z":"b468e89a030cb946","g":"bb1f6387ef45151f","name":"","label":"Ant LavLav timer","tooltip":"Velg antall laveste pristimer for batterilading","group":"e9e1f6f856a0ef75","order":5,"width":"5","height":"1","wrap":false,"passthru":false,"topic":"topic","topicType":"msg","format":"{{value}}","min":"0","max":"24","step":1,"className":"","x":330,"y":720,"wires":[["3f26c24b2f14e081"]]},{"id":"7cc2fd4a050bccdd","type":"change","z":"b468e89a030cb946","g":"bb1f6387ef45151f","name":"get LavLav#","rules":[{"t":"set","p":"payload","pt":"msg","to":"AntLavLavTimer","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":110,"y":720,"wires":[["f944761036305ed5"]]},{"id":"3f26c24b2f14e081","type":"change","z":"b468e89a030cb946","g":"bb1f6387ef45151f","name":"set LavLav#","rules":[{"t":"set","p":"AntLavLavTimer","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":720,"wires":[["7cc2fd4a050bccdd","45a6be97437e5578","178c4f3e51c168f5"]]},{"id":"60c75de6263a8794","type":"sort","z":"b468e89a030cb946","g":"bb1f6387ef45151f","name":"","order":"ascending","as_num":false,"target":"payload","targetType":"msg","msgKey":"timestamp","msgKeyType":"jsonata","seqKey":"payload","seqKeyType":"msg","x":650,"y":780,"wires":[["3ea6f148d6030a9f"]]},{"id":"50456aa1e7ddea01","type":"inject","z":"b468e89a030cb946","g":"bb1f6387ef45151f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"300","crontab":"","once":true,"onceDelay":"10","topic":"","payload":"","payloadType":"date","x":150,"y":780,"wires":[["7cc2fd4a050bccdd","4bcea84cb9e1dcd0"]]},{"id":"e9e1f6f856a0ef75","type":"ui_group","name":"Tabeller","tab":"a73dc047b0befb9e","order":2,"disp":false,"width":"10","collapse":false,"className":""},{"id":"a6bf2d9c44840471","type":"group","z":"b468e89a030cb946","name":"Sett global.lavPris & lavLavPris for bruk over alt.","style":{"fill":"#e3f3d3","label":true,"color":"#000000"},"nodes":["c37c0f4b7371feab","143924fdaab67cff","6782814db0d4c03a","f4934fafce556690","d4e405705b11b8ea","32bbe8db8e28a1fb"],"x":14,"y":1009,"w":772,"h":172},{"id":"c37c0f4b7371feab","type":"cronplus","z":"b468e89a030cb946","g":"a6bf2d9c44840471","name":"","outputField":"payload","timeZone":"","storeName":"","commandResponseMsgOutput":"output1","defaultLocation":"","defaultLocationType":"default","outputs":1,"options":[{"name":"schedule1","topic":"topic1","payloadType":"default","payload":"","expressionType":"cron","expression":"0 0 * * * * *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"}],"x":120,"y":1080,"wires":[["143924fdaab67cff"]]},{"id":"143924fdaab67cff","type":"function","z":"b468e89a030cb946","g":"a6bf2d9c44840471","name":"Set global.lavPris & global.lavLavPris","func":"let msg2 = {}\nlet msg3 = {}\nlet msg4 = {}\nlet timerValgtLav = []\ntimerValgtLav = flow.get(\"timerValgtLav\");\nlet phts = new Date();\nlet phtsFormatted = phts.getFullYear() + \"-\" + (\"00\" + (phts.getMonth() + 1)).slice(-2) + \"-\" + (\"00\" + phts.getDate()).slice(-2) + \" \" + (\"00\" + phts.getHours()).slice(-2) + \":\" + (\"00\" + phts.getMinutes()).slice(-2);\nphtsFormatted = phtsFormatted.slice(0,-2) + \"00\";\nlet phts2 = Number(new Date(phtsFormatted));\nlet exists = timerValgtLav.some(c => c.ts === Number(phts2)); //\"2024-11-06 02:00\")\n//node.warn(exists);\n\nglobal.set(\"lavPris\", exists);\nif (exists)\n msg.payload = \"<font color=lime>Ok\";\nelse\n msg.payload = \"<font color=red>-----\";\n\n\nlet timerValgtLavLav = []\ntimerValgtLavLav = flow.get(\"timerValgtLavLav\");\nphts = new Date();\nphtsFormatted = phts.getFullYear() + \"-\" + (\"00\" + (phts.getMonth() + 1)).slice(-2) + \"-\" + (\"00\" + phts.getDate()).slice(-2) + \" \" + (\"00\" + phts.getHours()).slice(-2) + \":\" + (\"00\" + phts.getMinutes()).slice(-2);\nphtsFormatted = phtsFormatted.slice(0, -2) + \"00\";\nphts2 = Number(new Date(phtsFormatted));\nexists = timerValgtLavLav.some(c => c.ts === phts2); //\"2024-11-06 02:00\")\n//node.warn(exists);\n\nglobal.set(\"lavLavPris\", exists);\nif (exists)\n msg2.payload = \"<font color=lime>Ok\";\nelse\n msg2.payload = \"<font color=red>-----\";\n\n// Send prisdata til \"Solar\" for bruk i headline\nmsg4.topic = \"prisMin\";\nmsg4.payload = flow.get(\"prisMin\").toFixed(2);\nnode.send([null,null,null,msg4]);\nmsg4.topic = \"prisSnitt\";\nmsg4.payload = flow.get(\"prisSnitt\").toFixed(2);\nnode.send([null, null, null, msg4]);\nmsg4.topic = \"prisMax\";\nmsg4.payload = flow.get(\"prisMax\").toFixed(2);\nnode.send([null, null, null, msg4]);\n\nmsg3.payload = \"<font size=5 color=orange>Min:\" + flow.get(\"prisMin\").toFixed(2) + \"/sn:\" + flow.get(\"prisSnitt\").toFixed(2) + \"/max:\" + flow.get(\"prisMax\").toFixed(2);\nreturn [msg,msg2,msg3];","outputs":4,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":400,"y":1070,"wires":[["6782814db0d4c03a"],["f4934fafce556690"],["d4e405705b11b8ea"],["32bbe8db8e28a1fb"]]},{"id":"6782814db0d4c03a","type":"ui_text","z":"b468e89a030cb946","g":"a6bf2d9c44840471","group":"e9e1f6f856a0ef75","order":6,"width":"5","height":"1","name":"Vis Lav nå","label":"","format":"Nå: <font size = 6>{{msg.payload}}","layout":"row-left","className":"","style":false,"font":"","fontSize":16,"color":"#000000","x":670,"y":1050,"wires":[]},{"id":"f4934fafce556690","type":"ui_text","z":"b468e89a030cb946","g":"a6bf2d9c44840471","group":"e9e1f6f856a0ef75","order":7,"width":"5","height":"1","name":"Vis LavLav nå","label":"","format":"Nå: <font size = 6>{{msg.payload}}","layout":"row-left","className":"","style":false,"font":"","fontSize":16,"color":"#000000","x":680,"y":1080,"wires":[]},{"id":"d4e405705b11b8ea","type":"ui_text","z":"b468e89a030cb946","g":"a6bf2d9c44840471","group":"e9e1f6f856a0ef75","order":10,"width":0,"height":0,"name":"MinSnMax","label":"","format":"{{msg.payload}}","layout":"row-center","className":"","style":false,"font":"","fontSize":16,"color":"#000000","x":670,"y":1110,"wires":[]},{"id":"32bbe8db8e28a1fb","type":"link out","z":"b468e89a030cb946","g":"a6bf2d9c44840471","name":"Strompris_Out","mode":"link","links":["c6012e7bbe73f35f"],"x":615,"y":1140,"wires":[]},{"id":"5d782f6a3df9efea","type":"group","z":"b468e89a030cb946","name":"Tabell lave timer for direkte forbruk","style":{"fill":"#e3f3d3","label":true,"color":"#000000"},"nodes":["9486fc1a301c466d","b6089f059556a469","52e50cf1d1089267","4bcea84cb9e1dcd0","031c1a6834f7945d","7bf74d11170a6864"],"x":14,"y":859,"w":1022,"h":142},{"id":"9486fc1a301c466d","type":"ui_table","z":"b468e89a030cb946","g":"5d782f6a3df9efea","group":"e9e1f6f856a0ef75","name":"Laveste Lav priser tbl","order":8,"width":"5","height":"10","columns":[{"field":"timestamp","title":"Time","width":"65%","align":"left","formatter":"plaintext","formatterParams":{"target":"_blank"}},{"field":"jonaspris","title":"Pris","width":"28%","align":"right","formatter":"plaintext","formatterParams":{"target":"_blank"}}],"outputs":0,"cts":false,"x":910,"y":960,"wires":[]},{"id":"b6089f059556a469","type":"function","z":"b468e89a030cb946","g":"5d782f6a3df9efea","name":"Hent valgte pristimer","func":"let liste2 = flow.get(\"liste2\") || false // Vis kun resttimer i listbox Lav og LavLav\nlet pdLow = flow.get(\"lavpris\");\nlet nmax = flow.get(\"AntLavTimer\");\nlet pdSelected = []\nlet pdSelDisp = []\nlet i = 0;\n// Klon objekt for å bryte forbindelsen til opprinnelige data for ellers vil opprinnelige data endres\nlet newpdLow = RED.util.cloneMessage(pdLow);\n\nlet dt = new Date();\n\nfor (i = 0; i < nmax;i++){\n pdSelected.push(newpdLow[i]);\n}\n\nflow.set(\"timerValgtLav\", pdSelected);\n\n\n// Fjern foregående timer fra tabell dersom valgt\nfor (i = 0; i < nmax;i++){\n if (liste2){\n if (pdLow[i].ts >= (Number(dt) - 60*60*1000)){\n pdSelDisp.push(newpdLow[i]);\n }\n }else{\n pdSelDisp.push(newpdLow[i]);\n }\n//node.warn(pdLow[i].ts);\n//node.warn(Number(dt));\n\n}\n\n// Pynt dataene for tabellvisning\nfor (i = 0; i < pdSelDisp.length;i++){\n pdSelDisp[i].timestamp = new Date(pdSelDisp[i].ts).toLocaleString(\"nb-NO\").slice(0,-3) ;\n pdSelDisp[i].jonaspris = parseFloat(pdSelDisp[i].jonaspris).toFixed(2);\n}\n//node.warn(pdSelDisp),\nmsg.payload = pdSelDisp;\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":430,"y":960,"wires":[["7bf74d11170a6864"]]},{"id":"52e50cf1d1089267","type":"ui_numeric","z":"b468e89a030cb946","g":"5d782f6a3df9efea","name":"","label":"Ant lave timer","tooltip":"Velg antall lavpristimer for direkte forbruk","group":"e9e1f6f856a0ef75","order":4,"width":"5","height":"1","wrap":false,"passthru":false,"topic":"topic","topicType":"msg","format":"{{value}}","min":"0","max":"24","step":1,"className":"","x":320,"y":900,"wires":[["031c1a6834f7945d"]]},{"id":"4bcea84cb9e1dcd0","type":"change","z":"b468e89a030cb946","g":"5d782f6a3df9efea","name":"get Lav#","rules":[{"t":"set","p":"payload","pt":"msg","to":"AntLavTimer","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":100,"y":900,"wires":[["52e50cf1d1089267"]]},{"id":"031c1a6834f7945d","type":"change","z":"b468e89a030cb946","g":"5d782f6a3df9efea","name":"set Lav#","rules":[{"t":"set","p":"AntLavTimer","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":900,"wires":[["4bcea84cb9e1dcd0","b6089f059556a469","178c4f3e51c168f5"]]},{"id":"7bf74d11170a6864","type":"sort","z":"b468e89a030cb946","g":"5d782f6a3df9efea","name":"","order":"ascending","as_num":false,"target":"payload","targetType":"msg","msgKey":"timestamp","msgKeyType":"jsonata","seqKey":"payload","seqKeyType":"msg","x":650,"y":960,"wires":[["9486fc1a301c466d"]]},{"id":"f3eadd1725385808","type":"group","z":"b468e89a030cb946","name":"Innhenting og behandling av strømpriser.","style":{"label":true,"color":"#000000"},"nodes":["bed0051986a10b75","a14d52fac2cf7941","4ce0f27b155c6831","eecf1a11e64a8746"],"x":8,"y":13,"w":1058,"h":504},{"id":"bed0051986a10b75","type":"group","z":"b468e89a030cb946","g":"f3eadd1725385808","name":"Sorter priser etter laveste pris i tidsrommet kl 22:00-22:00","style":{"fill":"#e3f3d3","label":true,"color":"#000000"},"nodes":["f4c0539b6cc55b55","0a56154a01aa97c9","eda587539ada5352","36db9c6b1167cf77"],"x":514,"y":209,"w":392,"h":142},{"id":"f4c0539b6cc55b55","type":"inject","z":"b468e89a030cb946","g":"bed0051986a10b75","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"00 22 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":630,"y":250,"wires":[["0a56154a01aa97c9"]]},{"id":"0a56154a01aa97c9","type":"function","z":"b468e89a030cb946","g":"bed0051986a10b75","name":"Velg tidsperiode","func":"//flow.set(\"Prisdata48Work\", flow.get(\"Prisdata48\"));\nlet pd72 = flow.get(\"Prisdata72\") || []\nlet lowprice = []\nlet i = 0;\nlet pdDate = Number(new Date(pd72[22].ts));\nlet today = Number(new Date());\n//today += 6 * 60 * 60 * 1000;\n//node.warn(pdDate - today);\nlet dt = new Date();\n//Ny periode kl 22:00: Flytt dato til over midnatt for å sette periodestart og slutt.\nif (dt.getHours() >= 22) dt = new Date(Number(dt) + 4*60*60*1000);\nlet dt2 = Number(new Date(dt.getFullYear() + \".\" + (dt.getMonth()+1) + \".\" + dt.getDate() + \" 00:00\"));\n//node.warn(dt2);\n\n\nlet dt3 = new Date();\n\n//node.warn(dt3.getHours());\nif (pd72.length == 72) { // Enkel sjekk at fullt datasett er tilgjengelig\n if (dt3.getHours() < 22){\n for (i = 22; i <= 45; i++){ // Plukk ut aktuell tidsperiode\n //node.warn(pd72[i]);\n lowprice.push(pd72[i]);\n }\n }\n else{\n for (i = 46; i < 70; i++){ // Plukk ut aktuell tidsperiode fra kl 22:00 og utover til i morgen\n lowprice.push(pd72[i]);\n //node.warn(pd72[i]);\n }\n }\n \n}\nmsg.payload = lowprice;\nreturn msg; \n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":800,"y":270,"wires":[["eda587539ada5352"]]},{"id":"eda587539ada5352","type":"sort","z":"b468e89a030cb946","g":"bed0051986a10b75","name":"","order":"ascending","as_num":true,"target":"payload","targetType":"msg","msgKey":"jonaspris","msgKeyType":"jsonata","seqKey":"payload","seqKeyType":"msg","x":640,"y":310,"wires":[["36db9c6b1167cf77"]]},{"id":"36db9c6b1167cf77","type":"change","z":"b468e89a030cb946","g":"bed0051986a10b75","name":"","rules":[{"t":"set","p":"lavpris","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":800,"y":310,"wires":[["cb7159478bedaaf1"]]},{"id":"a14d52fac2cf7941","type":"group","z":"b468e89a030cb946","g":"f3eadd1725385808","name":"Nettleie inkl alle avgifter dag: nld, natt og helg: nlnh. Endres manuelt her ved behov.","style":{"fill":"#92d04f","label":true,"color":"#000000"},"nodes":["d915fc95bcdb617b","599d5c07932ddd54"],"x":514,"y":39,"w":526,"h":82},{"id":"d915fc95bcdb617b","type":"change","z":"b468e89a030cb946","g":"a14d52fac2cf7941","name":"Lagre nettleie","rules":[{"t":"set","p":"nld","pt":"flow","to":".7","tot":"num"},{"t":"set","p":"nlnh","pt":"flow","to":".6","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":790,"y":80,"wires":[[]]},{"id":"599d5c07932ddd54","type":"inject","z":"b468e89a030cb946","g":"a14d52fac2cf7941","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":620,"y":80,"wires":[["d915fc95bcdb617b"]]},{"id":"4ce0f27b155c6831","type":"group","z":"b468e89a030cb946","g":"f3eadd1725385808","name":"Opprett flow.Prisdata72","style":{"fill":"#c8e7a7","label":true,"color":"#000000"},"nodes":["d51b8abd1f8b0102","06b3fdadafe92af4","20507782eea7238f","d68c7c64b4396e88","68dedc7c5f6d81bd","ba37809882e5a246","845af2cac241faae","7a2d84c871bfca40","7ad93125a55d7c60"],"x":34,"y":39,"w":462,"h":312},{"id":"289686ae32e9ac0c","type":"subflow","name":"Konv til eget format m nettleie og sst","info":"","category":"","in":[{"x":60,"y":70,"wires":[{"id":"4ab1c86d5f9c7f0a"}]}],"out":[{"x":640,"y":70,"wires":[{"id":"4ab1c86d5f9c7f0a","port":0}]}],"env":[],"meta":{},"color":"#DDAA99"},{"id":"4ab1c86d5f9c7f0a","type":"function","z":"289686ae32e9ac0c","name":"Konverter til kompakt format med nettleie o.l.","func":"/* Plukker kun ut råpris (Kr/kWh uten avgifter) og starttidspunkt fra inndata og legger på \nnettleie og avgifter her. Resultatet blir:\n.ts Timestamp (millisekund siden 01.01.1970) hentet fra starttidspunkt for strømprisen\n.pris Råpris uten avgifter o.l.\n.pristot Strømpris inkl avgifter og nettleie\n.jonaspris Strømpris inkl avgifter og nettleie minus strømstøtte\n.nettleie\n\[email protected]\n*/\nlet ny = []\nlet msg2 = {}\nlet nld = flow.get(\"$parent.nld\") || 0.7\nlet nlnh = flow.get(\"$parent.nlnh\") || 0.6\nlet pristot = 0;\nlet nl = 0;\n\nfor (var i = 0; i < msg.payload.length; i++ ){\n // Legg til Jonaspris (strømpris + nettleie + forbruksavgift + Enovastøtte med verdier fra flow)\n let dato = new Date(msg.payload[i].time_start);\n let ukedag = (dato.getDay()+6)%7; // Endre ukedag fra søndag=0 til mandag = 0\n let time = dato.getHours();\n let raapris = msg.payload[i].NOK_per_kWh;\n let jonaspris = raapris;\n if (raapris > 0.73) jonaspris = 0.73 + (raapris - 0.73) * 0.1; // Beregn strømstøtte\n \n // Beregn nettleie\n if (ukedag >= 5 || time < 6 || time >= 22){\n pristot = raapris * 1.25 + nlnh;\n jonaspris = jonaspris * 1.25 + nlnh;\n nl = nlnh;\n }\n else {\n pristot = raapris * 1.25 + nld;\n jonaspris = jonaspris * 1.25 + nld;\n nl = nld;\n } \n let ts = Number(new Date(msg.payload[i].time_start));\n ny.push({ \"ts\": ts, \"pris\": msg.payload[i].NOK_per_kWh, \"pristot\": pristot, \"jonaspris\": jonaspris, \"nettleie\": nl });\n}\nmsg2.payload = ny;\nreturn msg2;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":70,"wires":[[]]},{"id":"d51b8abd1f8b0102","type":"http request","z":"b468e89a030cb946","g":"4ce0f27b155c6831","name":"Prisdata fra hvakosterstrømmen.no","method":"GET","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":220,"y":190,"wires":[["d68c7c64b4396e88"]]},{"id":"06b3fdadafe92af4","type":"function","z":"b468e89a030cb946","g":"4ce0f27b155c6831","name":"set url i morgen","func":"var msg2 = {};\n\nvar d2 = new Date();\nd2.setDate(d2.getDate() + 1);\nvar nextYear = d2.getFullYear();\nvar nextMonth = (\"00\" + (d2.getMonth() + 1)).slice(-2);\nvar nextDate = (\"00\" + d2.getDate()).slice(-2);\nvar url2 = \"https://www.hvakosterstrommen.no/api/v1/prices/\" + nextYear + \"/\" + nextMonth + \"-\" + nextDate + \"_NO2.json\";\n//var url2 = \"https://www.hvakosterstrommen.no/api/v1/prices/\" + nextYear + \"/\" + nextMonth + \"-\" + \"07\" + \"_NO2.json\";\n//node.warn(nextYear + \" \" + nextMonth + \" \" + nextDate);\nmsg2.url = url2;\n//console.log(url);\n//console.log(url2);\n\nreturn msg2;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":160,"y":150,"wires":[["d51b8abd1f8b0102","7ad93125a55d7c60"]]},{"id":"20507782eea7238f","type":"function","z":"b468e89a030cb946","g":"4ce0f27b155c6831","name":"Fyll inn pris på riktig plass i Prisdata72","func":"let pd72 = flow.get(\"Prisdata72\") || []\nlet dt = new Date();\n// Offset to prev midnight\nlet dt2 = Number(new Date(dt.getFullYear() + \".\" + (dt.getMonth()+1) + \".\" + dt.getDate() + \" 00:00\"));\n\n// Opprett flow.Prisdata72 om den ikke eksisterer\nif (pd72.length < 71){\n for (let i = 0;i < 72;i++) pd72.push([\"-\"]);\n flow.set(\"Prisdata72\", pd72);}\n\n// Sjekk at data er leselige\nif (msg.payload[0].ts != null){\n let msgStartTs = msg.payload[0].ts - dt2;\n // Fyll inn prisdata i riktig tredjedel av Prisdata72\n if (msgStartTs < 0){ // I går ------------------------------------------------\n node.warn(\"<0 Prisdata for i går mottatt\");\n for (let i = 0;i <= 23;i++){\n pd72[i] = msg.payload[i];}}\n if (msgStartTs == 0){ // I dag ------------------------------------------------\n node.warn(\"0 Prisdata for i dag mottatt\");\n for (let i = 0;i <= 23;i++){\n pd72[i+24] = msg.payload[i];}}\n if (msgStartTs > 0){ // I morgen ------------------------------------------------\n node.warn(\">0 Prisdata for i morgen mottatt\");\n // Hvis data 48-71 er for i dag: Skift array 24 plasser tilbake\n if ((pd72[48].ts - dt2) == 0){\n for (let i = 0;i <= 47; i++){\n pd72[i] = pd72[i+24];}\n }\n // ...og så legg inn morgendagens data\n for (let i = 0;i <= 23;i++){\n pd72[i+48] = msg.payload[i];}}\n}else{\n node.warn(\"Strømdata ikke tilgjengelige...\")}\n\n// Marker at prisdata for i morgen er ankommet.\nif ((pd72[48].ts - dt2) == 86400000){\n flow.set(\"pdMorgenOk\", true);}\nelse{\n flow.set(\"pdMorgenOk\", false);}\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":230,"y":270,"wires":[["0a56154a01aa97c9","68dedc7c5f6d81bd"]]},{"id":"d68c7c64b4396e88","type":"subflow:289686ae32e9ac0c","z":"b468e89a030cb946","g":"4ce0f27b155c6831","name":"","x":230,"y":230,"wires":[["20507782eea7238f"]]},{"id":"68dedc7c5f6d81bd","type":"debug","z":"b468e89a030cb946","g":"4ce0f27b155c6831","name":"debug 187","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":350,"y":310,"wires":[]},{"id":"ba37809882e5a246","type":"inject","z":"b468e89a030cb946","g":"4ce0f27b155c6831","name":"13:02","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"02 13 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":110,"wires":[["06b3fdadafe92af4"]]},{"id":"845af2cac241faae","type":"inject","z":"b468e89a030cb946","g":"4ce0f27b155c6831","name":"hver 5 min","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"*/5 13-21 * * *","once":false,"onceDelay":0.1,"topic":"xx","payload":"","payloadType":"date","x":150,"y":80,"wires":[["7a2d84c871bfca40"]]},{"id":"7a2d84c871bfca40","type":"switch","z":"b468e89a030cb946","g":"4ce0f27b155c6831","name":"Data for i morgen mottatt?","property":"pdMorgenOk","propertyType":"flow","rules":[{"t":"false"}],"checkall":"true","repair":false,"outputs":1,"x":360,"y":80,"wires":[["06b3fdadafe92af4"]]},{"id":"7ad93125a55d7c60","type":"debug","z":"b468e89a030cb946","g":"4ce0f27b155c6831","name":"debug 192","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"url","targetType":"msg","statusVal":"","statusType":"auto","x":350,"y":150,"wires":[]},{"id":"eecf1a11e64a8746","type":"group","z":"b468e89a030cb946","g":"f3eadd1725385808","name":"Prisdata fra NordPool","style":{"fill":"#c8e7a7","label":true,"color":"#000000"},"nodes":["6ce4aa06b498cbbe","62687955c7aa4efe","0b9a6c13ac4210bc","35e0d54c5ac618c8"],"x":34,"y":359,"w":592,"h":132},{"id":"6ce4aa06b498cbbe","type":"nordpool-api-plus","z":"b468e89a030cb946","g":"eecf1a11e64a8746","name":"I morgen","area":"NO2","currency":"NOK","action":"dayAhead","x":140,"y":400,"wires":[["62687955c7aa4efe"]]},{"id":"62687955c7aa4efe","type":"function","z":"b468e89a030cb946","g":"eecf1a11e64a8746","name":"if exist NOK/MWh --> NOK/kWh","func":"let today = new Date()\nlet tomorrow = new Date(today)\ntomorrow.setDate(tomorrow.getDate() + 1)\nlet ddate = new Date(msg.payload[6].timestamp)\n\n// Sjekk at data for i morgen er tilstede\nif (Number(ddate.getDate()) == Number(tomorrow.getDate())){\n for (var i = 0; i < msg.payload.length; i++ ){\n msg.payload[i].NOK_per_kWh = msg.payload[i].price/1000;\n msg.payload[i].time_start = Number(new Date(msg.payload[i].timestamp));\n //node.warn(i);\n }\n return msg;\n}\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":430,"y":400,"wires":[["0b9a6c13ac4210bc"]]},{"id":"0b9a6c13ac4210bc","type":"subflow:289686ae32e9ac0c","z":"b468e89a030cb946","g":"eecf1a11e64a8746","x":450,"y":440,"wires":[["20507782eea7238f"]]},{"id":"35e0d54c5ac618c8","type":"inject","z":"b468e89a030cb946","g":"eecf1a11e64a8746","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":450,"wires":[["6ce4aa06b498cbbe"]]},{"id":"e4c19e487e8287f7","type":"group","z":"b468e89a030cb946","name":"Betjening: Vis kun resttimer i listbokser.","style":{"fill":"#e3f3d3","label":true,"color":"#000000"},"nodes":["b51d06a21038e464","5ab88c22ad7b601e","a9ad0882e1fb8f2b","1f96c7a359c6fede"],"x":14,"y":1189,"w":572,"h":122},{"id":"b51d06a21038e464","type":"ui_switch","z":"b468e89a030cb946","g":"e4c19e487e8287f7","name":"","label":"Vis kun resttimer","tooltip":"","group":"e9e1f6f856a0ef75","order":15,"width":"3","height":"1","passthru":true,"decouple":"false","topic":"topic","topicType":"msg","style":"","onvalue":"true","onvalueType":"bool","onicon":"check_box","oncolor":"lime","offvalue":"false","offvalueType":"bool","officon":"check_box_outline_blank","offcolor":"grey","animate":true,"className":"","x":320,"y":1270,"wires":[["5ab88c22ad7b601e"]]},{"id":"5ab88c22ad7b601e","type":"change","z":"b468e89a030cb946","g":"e4c19e487e8287f7","name":"flow.liste2","rules":[{"t":"set","p":"liste2","pt":"flow","to":"payload","tot":"msg"},{"t":"delete","p":"socketid","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":1270,"wires":[["b6089f059556a469","45a6be97437e5578"]]},{"id":"a9ad0882e1fb8f2b","type":"change","z":"b468e89a030cb946","g":"e4c19e487e8287f7","name":"flow.liste2","rules":[{"t":"set","p":"payload","pt":"msg","to":"liste2","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":110,"y":1270,"wires":[["b51d06a21038e464"]]},{"id":"1f96c7a359c6fede","type":"inject","z":"b468e89a030cb946","g":"e4c19e487e8287f7","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"300","crontab":"","once":true,"onceDelay":"5","topic":"","payload":"","payloadType":"date","x":130,"y":1230,"wires":[["a9ad0882e1fb8f2b"]]}] Tror det skal klare seg å installere node-red-dashboard, node-red-contrib-nordpool-api-plus, og node-red-contrib-cron-plus i paletten utover standard. Endret 24. november av SveinHa 2 Siter
Anbefalte innlegg
Bli med i samtalen
Du kan publisere innhold nå og registrere deg senere. Hvis du har en konto, logg inn nå for å poste med kontoen din.