# Change request url for Vattenfall API automatically every day

**URL:** <https://discourse.nodered.org/t/change-request-url-for-vattenfall-api-automatically-every-day/48255>\
**Category:** General\
**Created:** [10 July 2021 20:14 UTC](https://discourse.nodered.org/t/change-request-url-for-vattenfall-api-automatically-every-day/48255 "2021-07-10T20:14:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![masteroftherings](https://avatars.discourse-cdn.com/v4/letter/m/2acd7d/32.png) [@masteroftherings](https://discourse.nodered.org/u/masteroftherings)\
**Post date:** [10 July 2021 20:14 UTC](https://discourse.nodered.org/t/change-request-url-for-vattenfall-api-automatically-every-day/48255/1 "2021-07-10T20:14:00Z")

</div>

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](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!!

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [10 July 2021 20:42 UTC](https://discourse.nodered.org/t/change-request-url-for-vattenfall-api-automatically-every-day/48255/2 "2021-07-10T20:42:36Z")

</div>

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

2. Clear the http request node `URL` property so that is uses `msg.url` from the function

---

<div class="post-metadata">

**Author:** ![masteroftherings](https://avatars.discourse-cdn.com/v4/letter/m/2acd7d/32.png) [@masteroftherings](https://discourse.nodered.org/u/masteroftherings)\
**Post date:** [10 July 2021 21:56 UTC](https://discourse.nodered.org/t/change-request-url-for-vattenfall-api-automatically-every-day/48255/3 "2021-07-10T21:56:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![masteroftherings](https://avatars.discourse-cdn.com/v4/letter/m/2acd7d/32.png) [@masteroftherings](https://discourse.nodered.org/u/masteroftherings)\
**Post date:** [11 July 2021 20:07 UTC](https://discourse.nodered.org/t/change-request-url-for-vattenfall-api-automatically-every-day/48255/4 "2021-07-11T20:07:56Z")

</div>

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!!

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [12 July 2021 00:03 UTC](https://discourse.nodered.org/t/change-request-url-for-vattenfall-api-automatically-every-day/48255/5 "2021-07-12T00:03:29Z")

</div>

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](https://momentjs.com/docs/#/displaying/format/) 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](https://nodered.org/docs/user-guide/writing-functions#using-the-functionexternalmodules-option)

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/7/f7c18d0be01080d314f303411833359d3527aff5.png)

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

```auto
[{"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"]]}]

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [26 July 2021 00:03 UTC](https://discourse.nodered.org/t/change-request-url-for-vattenfall-api-automatically-every-day/48255/6 "2021-07-26T00:03:56Z")

</div>

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