zinen / node-red-contrib-nordpool-api-plus

6 stars 5 forks source link

Price for current hour #27

Open PowerZone21 opened 2 days ago

PowerZone21 commented 2 days ago

Hi,

How can I get the price for current hour?

zinen commented 2 days ago

Depends. Looking for a simple but slow solusion? Then this could work. Also to get you started with node-red and function nodes using javascript programming.

[{"id":"88bd18c039f058e3","type":"nordpool-api-plus","z":"b4de871a.229aa8","name":"","area":"Oslo","currency":"EUR","action":"todayOrOverride","x":440,"y":7380,"wires":[["dcd016220d06510d","36b4652256147264"]]},{"id":"d8869c6f32b139f4","type":"inject","z":"b4de871a.229aa8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":230,"y":7380,"wires":[["88bd18c039f058e3"]]},{"id":"36b4652256147264","type":"function","z":"b4de871a.229aa8","name":"function 2","func":"let now = new Date();\n\n// Create a new date, rounding down to the nearest whole hour\nlet roundedHour = new Date(now.setMinutes(0, 0, 0)).toISOString()\n\nfor (const item of msg.payload) {\n    if (item.timestamp == roundedHour) {\n        msg.payload = item.price \n        return msg;\n    }\n}\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":7380,"wires":[["3eb6980eacab1b9c"]]},{"id":"3eb6980eacab1b9c","type":"debug","z":"b4de871a.229aa8","name":"debug 251","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":850,"y":7380,"wires":[]}]

It will however need to ask nordpool each time you need to know the price. A better way would be to store the last API data in the memory of the node but that is a more advanced approch.

zinen commented 2 days ago

And here are a more advanced approch. It will only ask nordpool if it does not know the current price else it will search in its memory from last request. This very much speeds the proces up. But is a harder code to understand:

[{"id":"ed0528d14a216786","type":"inject","z":"b4de871a.229aa8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":240,"y":7580,"wires":[["e4a096fa2a2d3606"]]},{"id":"e4a096fa2a2d3606","type":"function","z":"b4de871a.229aa8","name":"function 1","func":"if (msg.topic == 'getNewData') {\n    // If input if from nordpool API\n    // store the payload as prices in the node memory\n    if (msg.payload) context.set(\"prices\", msg.payload)\n}\n// Extract the prices from the node memory\nconst prices = context.get(\"prices\") || [];\n\nlet now = new Date();\n// Create a new date, rounding down to the nearest whole hour\nlet roundedHour = new Date(now.setMinutes(0, 0, 0)).toISOString()\n\nfor (const item of prices) {\n    if (item.timestamp == roundedHour) {\n        msg.payload = item.price\n        return msg;\n    }\n}\nif (msg.topic == 'getNewData') {\n    // If input was from nordpool API and no matching date was found\n    // then write warning to console return null\n    node.warn('Price tag not found for ' + roundedHour)\n    return\n}\n// If input was not from nordpool API and prices was not matched.\n// then try getting new prices from nordpool\nnode.send([null, { topic: 'getNewData' }])","outputs":2,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":7580,"wires":[["5f840613b9bec787"],["708dc0252a0cba10"]]},{"id":"5f840613b9bec787","type":"debug","z":"b4de871a.229aa8","name":"debug 253","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":690,"y":7580,"wires":[]},{"id":"708dc0252a0cba10","type":"nordpool-api-plus","z":"b4de871a.229aa8","name":"","area":"Oslo","currency":"EUR","action":"todayOrOverride","x":460,"y":7640,"wires":[["e4a096fa2a2d3606"]]}]

Copy code and import in your editor

PowerZone21 commented 2 days ago

Thank you :)