Best strategy for Reading & Sorting Electricity Cost Array



I have just switched electricity suppliers to Octopus Energy who I think are maybe one of the first to have variable half hour rates in the UK depending on wholesale price. Naturally I want to make some switching decisions based on future electricity cost.

There is an example curl statement that gets the results in file attache.result.txt (11.6 KB)

This gives the pricing for the next 24 hours.

I am no programmer but I have Node-RED successfully controlling all sorts of devices mainly by copying other peoples examples and some trial and error to get the result needed.

I thought it easier to explain in words what I am trying to achieve before diving in (and sinking!)

Get the data about 22:00 each day.

I then want to parse the results and find a number of patterns.

One - the lowest cost 2 consecutive 1/2 hour periods. Switch on smart plug A via MQTT. This is to operate washing machine which has a sub 1 hour cycle.

Two - the lowest cost 6 consecutive 1/2 hour periods. Switch on smart plug B via MQTT. This is to operate dryer which has a sub 3 hour cycle.
Optionally to set a threshold price which negates the above 2 scenarios and wait for another day.

Three - When I install a battery picking the X number of lowest 1/2 periods in ascending order of cost so the battery can be recharged at lowest cost. X being the number of 1/2 hours required to re-charge.
This will be far less in Summer when I have surplus solar power.

I guess this could all be done directly in Node-RED but that strikes me as somewhat daunting and way beyond my knowledge.

I wonder if I am better off to get the data into a file, manipulate the file in python and then read the resultant file from Node-RED.

Any thoughts, guidance or suggestions much appreciated.

(Incidentally if anyone is interested in switching to Octopus PM me and I can give a link which gives you a bonus on switching. The process is all done on line and took me all of 10 minutes)





Do you know how to connect to the API and do the call? That is possibly the hardest part. You also need to understand how the API works. In particular, in common with many API's, the output can be paged which means that you might need to do a loop to get more than 1 page of results.

Hard to give specifics since I don't have an account.

Once you have compiled all of the data into a single object, the rest should be pretty straight-forward.



I have similar programm running. Difference is that I get day ahead price data from Nordpool market and the structure of that data is much more complex. And we don't have half-hour prices but it changes hourly.
My program calculates schedule for each device at the beginning of the day. Schedule calculation based on predefined rules. Each rule defines how many hours can device be active and some additional parameters like deal with day time hours only, or consecutive hours prohibed and so on.
So based on rule and price list, the final schedule for each device is calculated and at every hour change if device schedule tells to change the state, the output is sent to relay board.
Of course the manual mode exists for each device. And some overrules.
I also developed some visuals as it is interesting to know how the price moves and why and when some device is on or off.

So it is entirely doable in Node-RED. I can't see any use for python or something else.

The pattern three you mentioned needs UI as you want to manually pick the cheapest hours. So some visual part needs to be done anyways as I understand. I don't have manual picking option as I am lazy enough to not push any button at 3 AM. But it is doable and probably the easiest part of this kind of complex system you are going to build.

1 Like


Really nice use case for IOT. It is not complicated at all doing in Node-RED. I put together a flow that will select the best six consecutive periods from the file. I guess doing for 2 or n periods will be the same.

The flow will generate an array with these elements:


and afterwards a function node will select the one with the lowest costs among them.

Look at this flow as a possible candidate for a quick start. Additional work is required to meet your requirements (again, nothing very complicated) and make the flow robust.

[{"id":"5e45b8d5.4a6f28","type":"tab","label":"Flow 4","disabled":false,"info":""},{"id":"2d27dda5.9122d2","type":"inject","z":"5e45b8d5.4a6f28","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":200,"wires":[["882fdde7.d0c6"]]},{"id":"15936ace.ccd895","type":"comment","z":"5e45b8d5.4a6f28","name":"Best strategy for Reading & Sorting Electricity Cost Array","info":"\n","x":310,"y":100,"wires":[]},{"id":"882fdde7.d0c6","type":"file in","z":"5e45b8d5.4a6f28","name":"Read file","filename":"C:\\Users\\OCM\\.node-red\\static\\nrfiles\\ag.json","format":"utf8","chunk":false,"sendError":false,"x":240,"y":200,"wires":[["3dc9c1e.8610e3e"]]},{"id":"3dc9c1e.8610e3e","type":"json","z":"5e45b8d5.4a6f28","name":"","property":"payload","action":"obj","pretty":false,"x":370,"y":200,"wires":[["17c80a7.0dae2f6"]]},{"id":"7583ef9e.d5ef2","type":"split","z":"5e45b8d5.4a6f28","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":630,"y":200,"wires":[["cad8ad0d.02291"]]},{"id":"17c80a7.0dae2f6","type":"change","z":"5e45b8d5.4a6f28","name":"Results","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.results","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":200,"wires":[["7583ef9e.d5ef2"]]},{"id":"cad8ad0d.02291","type":"link out","z":"5e45b8d5.4a6f28","name":"","links":["8e60f88c.14de78","a93d7927.117a98"],"x":755,"y":200,"wires":[]},{"id":"85cc0983.6330e8","type":"batch","z":"5e45b8d5.4a6f28","name":"Batch 6 periods","mode":"count","count":"6","overlap":"5","interval":10,"allowEmptySequence":false,"topics":[],"x":200,"y":280,"wires":[["3f62a44b.8f82fc"]]},{"id":"a93d7927.117a98","type":"link in","z":"5e45b8d5.4a6f28","name":"","links":["cad8ad0d.02291"],"x":75,"y":280,"wires":[["85cc0983.6330e8"]]},{"id":"3f62a44b.8f82fc","type":"join","z":"5e45b8d5.4a6f28","name":"","mode":"auto","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":370,"y":280,"wires":[["466dbf51.6f863"]]},{"id":"466dbf51.6f863","type":"function","z":"5e45b8d5.4a6f28","name":"Cost for batch","func":"function addValue(arr) {\n    let add = 0;\n    for (let ele of arr) {\n        add = add + ele.value_inc_vat;\n    }\n    return add;\n}\n\nmsg.value_batch = addValue(msg.payload);\n\nreturn msg;","outputs":1,"noerr":0,"x":540,"y":280,"wires":[["41755ece.0fe3a"]],"info":"Calculate the energy cost for the period"},{"id":"c14bf7d5.3bf658","type":"debug","z":"5e45b8d5.4a6f28","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"outcome","targetType":"msg","x":720,"y":340,"wires":[]},{"id":"41755ece.0fe3a","type":"change","z":"5e45b8d5.4a6f28","name":"Transform","rules":[{"t":"set","p":"topic","pt":"msg","to":"payload[5].valid_from","tot":"msg"},{"t":"set","p":"pay.start","pt":"msg","to":"payload[5].valid_from","tot":"msg"},{"t":"set","p":"pay.time_end","pt":"msg","to":"payload[0].valid_to","tot":"msg"},{"t":"move","p":"value_batch","pt":"msg","to":"pay.value","tot":"msg"},{"t":"delete","p":"filename","pt":"msg"},{"t":"delete","p":"_event","pt":"msg"},{"t":"set","p":"payload","pt":"msg","to":"pay.value","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":220,"y":340,"wires":[["15a53398.37b40c"]]},{"id":"15a53398.37b40c","type":"join","z":"5e45b8d5.4a6f28","name":"Create Array","mode":"custom","build":"array","property":"pay","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"2","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":370,"y":340,"wires":[["92b06136.75d29","bdc73762.b85d08"]]},{"id":"92b06136.75d29","type":"function","z":"5e45b8d5.4a6f28","name":"Select lowest","func":"msg.outcome =, current) => (prev.value < current.value) ? prev : current)\nreturn msg;","outputs":1,"noerr":0,"x":550,"y":340,"wires":[["c14bf7d5.3bf658"]]},{"id":"bdc73762.b85d08","type":"debug","z":"5e45b8d5.4a6f28","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":520,"y":420,"wires":[]}]


Another way to reduce the electricity bill:
I run my deep freezers if power is cheap (pv modules) and cool them down to -25°.

To achieve a longer off time, i use small bottles filled with salt water. The system should be failsafe like: node-red only turns the freezer off for a certain time - if no signal arrives, freezer go's on again.



and here is a flow that can be improved to accomplish task #3. You have to hard code the X number in the function node, or bring this value from the dashboard, if you develop one.

[{"id":"2d488151.3842ae","type":"tab","label":"Electricity Cost Array - v3","disabled":false,"info":""},{"id":"a9ee4982.8ee248","type":"inject","z":"2d488151.3842ae","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":200,"wires":[["161e8b61.a77575"]]},{"id":"fae5bc8.e63304","type":"comment","z":"2d488151.3842ae","name":"Best strategy for Reading & Sorting Electricity Cost Array","info":"\n","x":310,"y":100,"wires":[]},{"id":"161e8b61.a77575","type":"file in","z":"2d488151.3842ae","name":"Read file","filename":"C:\\Users\\OCM\\.node-red\\static\\nrfiles\\ag.json","format":"utf8","chunk":false,"sendError":false,"x":240,"y":200,"wires":[["9d226909.884878"]]},{"id":"9d226909.884878","type":"json","z":"2d488151.3842ae","name":"","property":"payload","action":"obj","pretty":false,"x":370,"y":200,"wires":[["59f0573c.4d0ac8"]]},{"id":"590121a5.220bc","type":"split","z":"2d488151.3842ae","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":630,"y":200,"wires":[["da7fbbb5.431ac8"]]},{"id":"59f0573c.4d0ac8","type":"change","z":"2d488151.3842ae","name":"Results","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.results","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":200,"wires":[["590121a5.220bc"]]},{"id":"da7fbbb5.431ac8","type":"link out","z":"2d488151.3842ae","name":"","links":["8e60f88c.14de78","a0256cd3.bce79"],"x":755,"y":200,"wires":[]},{"id":"13f43521.e33c7b","type":"batch","z":"2d488151.3842ae","name":"Batch 1 period","mode":"count","count":"1","overlap":"0","interval":10,"allowEmptySequence":false,"topics":[],"x":200,"y":280,"wires":[["6f47ffb9.beb71"]]},{"id":"a0256cd3.bce79","type":"link in","z":"2d488151.3842ae","name":"","links":["da7fbbb5.431ac8"],"x":75,"y":280,"wires":[["13f43521.e33c7b"]]},{"id":"6f47ffb9.beb71","type":"join","z":"2d488151.3842ae","name":"","mode":"auto","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":370,"y":280,"wires":[["c928820c.cbc51"]]},{"id":"c928820c.cbc51","type":"function","z":"2d488151.3842ae","name":"Cost for batch","func":"function addValue(arr) {\n    let add = 0;\n    for (let ele of arr) {\n        add = add + ele.value_inc_vat;\n    }\n    return add;\n}\n\nmsg.value_batch = addValue(msg.payload);\n\nreturn msg;","outputs":1,"noerr":0,"x":540,"y":280,"wires":[["ea7518a4.de4508","fa30591c.6ca008"]],"info":"Calculate the energy cost for the period"},{"id":"71399baf.3c3094","type":"debug","z":"2d488151.3842ae","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":710,"y":340,"wires":[]},{"id":"ea7518a4.de4508","type":"change","z":"2d488151.3842ae","name":"Transform","rules":[{"t":"set","p":"topic","pt":"msg","to":"payload[0].valid_from","tot":"msg"},{"t":"set","p":"pay.start","pt":"msg","to":"payload[0].valid_from","tot":"msg"},{"t":"set","p":"pay.time_end","pt":"msg","to":"payload[0].valid_to","tot":"msg"},{"t":"move","p":"value_batch","pt":"msg","to":"pay.value","tot":"msg"},{"t":"delete","p":"filename","pt":"msg"},{"t":"delete","p":"_event","pt":"msg"},{"t":"set","p":"payload","pt":"msg","to":"pay.value","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":220,"y":340,"wires":[["8fde6029.957f1"]]},{"id":"8fde6029.957f1","type":"join","z":"2d488151.3842ae","name":"Create Array","mode":"custom","build":"array","property":"pay","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"2","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":370,"y":340,"wires":[["fbdd6b3c.f06518"]]},{"id":"fbdd6b3c.f06518","type":"function","z":"2d488151.3842ae","name":"const cycle = 6","func":"const cycle = 6;\n\nmsg.outcome =,b) => a.value - b.value);\n\nfor (let c = 0; c < cycle ; c++) {\n    node.send(msg.outcome[c]);\n}\n","outputs":1,"noerr":0,"x":560,"y":340,"wires":[["71399baf.3c3094"]]},{"id":"fa30591c.6ca008","type":"debug","z":"2d488151.3842ae","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":730,"y":280,"wires":[]}]


Well I am speechless!

Realising I did not understand how to connect to the API and do the call I suddenly thought can I put the curl command in a script and run with exec. Yes I can!

I then imported the flows that Andrei produced, changed the name to match and hit inject.

Magic! It all works. It took all in about 20 minutes to implement. Power To Node-RED...........

All I have to do is set up the timing, get the results to send appropriate mqtt commands, add some error checking where necessary and job largely done. And of course try to understand how the flows work.

One question is what am I going to do for the next month waiting for the smart meter install and tariff change. I thought I would be really struggling with this for days!

Many many thanks to all particularly to Andrei for the flow.





Sure you can, but do you really need to?

[{"id":"a58b0619.a19f08","type":"http request","z":"ba86d109.1f011","name":"","method":"GET","ret":"obj","url":"","tls":"","x":380,"y":1250,"wires":[["5d69e1a8.0b8f9"]]},{"id":"15046c14.fbf814","type":"inject","z":"ba86d109.1f011","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":1250,"wires":[["a58b0619.a19f08"]]},{"id":"5d69e1a8.0b8f9","type":"debug","z":"ba86d109.1f011","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":550,"y":1250,"wires":[]}]
1 Like


Indeed, this use case shows the power of Node-RED.

Once you add the http request node like said @hotNipi you will see that the flow still runs smoothly.

Probably it is possible some optimization in the flow but it is too early to think about that. Before you should imagine a strategy to trigger the MQTT message to control the devices.

I think it would be smart to delegate the task to a programmable timer/scheduler, instead of trying to code the timer in JavaScript. I wish we could find a scheduler node that is programmable (via msgs), persist after reset of the system, and can be programmed with a timestamp formatted string. Something like start : "2019-02-15T13:00:00Z and then stop: "2019-02-15T14:00:00Z. Looks like node-red-contrib-schedex comes close.



This is part of what I've been thinking about and starting to design into a new node.

The problem is that the scope of the requirements is much larger than that simple example and we will need the ability to manage multiple schedules along with overrides.

I've a bunch of thoughts and ideas that are gradually coming together but happy to consider requirements - but best to start a different thread for that I think.



Hi Julian, let's go for it. Node-RED deserves a better contrib node scheduler. I could give you a hand with coding and testing. See you in the upcoming new thread :wink:



Thanks.I need to add the API key to the http request and I cannot figure out how to do that. In the curl I put:-

curl **-u "nnnnnnnnnneayhhNn5RORJG:"**

Where do you put that in the http request?



Like shown below

and this is where you enter the http request





Sorry, I did not make myself clear. The flow worked but I got "Invalid API key."

When I issue Curl the format is:-
curl -u "nnnnnnnnnfEN2eayhhNn5RORJG:"

"nnnnnnnnnfEN2eayhhNn5RORJG:" is the API key which I guess I need to include in the GET somehow but I don't know where. I have tried a number of different things but none worked.



Assuming the server is using basic authentication. Check the option "Use basic authentication) and enter username and password. I never used but I assume the node will encode the pair as base64 and will build the required header. Test and let us know the result.



I like the idea of using schedex which I use on other applications. At the moment I run manual injected flows to restore schedex schedules after any deployments or restarts. I could do something similar in this application for the time being until a persistent scheduler is available.

Thinking about the decision flow I have realised I need to keep previous days data as well.

The tariff rates for the following day are set sometime after 16:00 every day. I have learnt that I will always avoid using electricity 16:00 to 19:00 every day as there is a 12p surcharge for these peak hours. That is applied by the UK grid network and pays for the grid. Therefore having got next days data at 17:30 the period I need to check is from 19:00 current day until 16:00 the following day. I am thinking of just merging the 2 days data and running the flow on the resultant file.

This is really coming together. Thanks for all the input. I have also realised the battery schedule is more complex than I first thought but I will leave that for another day.



What I do is every time I get new data, I run calculations to create additional total price for all data with all the taxes included. As grid network fee is not constant it makes significant difference. Different prices for night hours and for weekends makes cost saving a little bit challenge to do in head, but computer can and little bit of math helps out here :slightly_smiling_face: