Change request url for Vattenfall API automatically every day

Hi guys,

I'm new in Node Red!

I live in Sweden and I have the swedish electricity company Vattenfall as my electricity supplier. They have an API that publishes tomorrow electricity price hour by hour. The problem is that this url is unique everyday because it consists the actual date for the next date. For example the url for tomorrow 11:th of july is https://www.vattenfall.se/api/price/spot/pricearea/2021-07-11/2021-07-11/SN3

Is there any way to modify the date section of the url in Node Red so that it updates automatically the date and change when its a new month/year etc...?

thanks in advance!!

  1. Put a function node before the HTTP Request node containing...

    const today = new Date()
    const tomorrow = new Date(today)
    tomorrow.setDate(tomorrow.getDate() + 1)
    const yyyy = tomorrow.getFullYear();
    const mm = (tomorrow.getMonth() + 1).toString().padStart(2, "0");
    const dd = tomorrow.getDate().toString().padStart(2,"0");
    msg.url = `https://www.vattenfall.se/api/price/spot/pricearea/${yyyy}-${mm}-${dd}/${yyyy}-${mm}-${dd}/SN3`
    return msg;
    
  2. Clear the http request node URL property so that is uses msg.url from the function

Thank you very much for the help Steve-Mcl, you just saved my a lot of hours google around :wink: the flow work just fine!!

I have tried the flow for 24 hours and I have ran in to some problems (I would like to use the data to present the prices each hour in a time-serie graph in Grafana). First of all is that tomorrows electrical prices is released first 16:00 the day before (24 values for each hour). Each value have a unique timestamp for the actual time.

The format of the timestamp in the json string is 2021-07-11T00:00:00 but my SQlite database would like to have the timestamp in the format 2021-07-11 00:00:00 instead. How can I reformat the timestamp so I can store the data in my database? Is there a problem that the timestamp is for the future one day ahead or would this work correct in the database?

The next question is, is it possible to present future data in a time serie graph in Grafana?

Thanks in advance!!

Hi .. i was playing around with this also and i noticed too that the api doesnt return data (404 status error) for the next day when its not past some specific time. So its after 16:00 ?
I guess you can use a Trigger node and set it up to trigger the request of data once a day and after 16:00.

Regarding the manipulation of datetimes i always find it easy to use the moment.js library.

If you are running Node red 1.3 or greater you can enable in your settings.js file the option to load additional modules (as is the moment.js module).

More info on this new functionExternalModules Node-red feature here

Once you have that setup you can use it in your Function node.

Demo flow of its use and formatting of TimeStamp to the format your sqlite db needs.

[{"id":"0e58a03f349f4bf0","type":"inject","z":"4895ea10b4ee9ead","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"00 16 * * *","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":250,"y":680,"wires":[["16a66d05b8144149"]]},{"id":"16a66d05b8144149","type":"function","z":"4895ea10b4ee9ead","name":"","func":"let urlDate = moment().add(1, 'day').format(\"YYYY-MM-DD\");\nnode.warn(urlDate)\n\n// if (moment().isAfter(moment('16:00', 'HH:mm'))) {\n//     urlDate = moment().add(1, 'day').format(\"YYYY-MM-DD\");\n// }\n// else {\n//     urlDate = moment().format(\"YYYY-MM-DD\");\n// }\n\n\nmsg.url = `https://www.vattenfall.se/api/price/spot/pricearea/${urlDate}/${urlDate}/SN3`\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"moment","module":"moment"}],"x":440,"y":680,"wires":[["1b9b2d8f17fcfcc8"]]},{"id":"1b9b2d8f17fcfcc8","type":"http request","z":"4895ea10b4ee9ead","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","x":630,"y":680,"wires":[["25c114981d693ae5"]]},{"id":"523288eb84a1db59","type":"debug","z":"4895ea10b4ee9ead","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":990,"y":680,"wires":[]},{"id":"25c114981d693ae5","type":"function","z":"4895ea10b4ee9ead","name":"","func":"if (msg.statusCode === 200) {\n    msg.payload = msg.payload.map(el => {\n        return {\n            TimeStamp: moment(el.TimeStamp).format('YYYY-MM-DD HH:mm:ss'), // format timestamp using moment\n            Value: el.Value\n        }\n    });\n    return msg\n}\nelse {\n    msg.payload = \"No Data\"\n    return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"moment","module":"moment"}],"x":800,"y":680,"wires":[["523288eb84a1db59"]]}]

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.