bisand / node-red-contrib-tibber-api

Node Red module for integrating with Tibber api
MIT License
20 stars 2 forks source link

Convert ISO date to epoch in payload.from + payload.to (and payload.timestamp) #64

Closed Wigdens closed 1 year ago

Wigdens commented 1 year ago

Hello! I'm a beginner and trying to convert the ISO date (from Tibber) to epoch. The purpose is to feed a line diagram with consumption and cost. As I understand, I have to feed the Chart (X:) with Epoch time.' https://github.com/node-red/node-red-dashboard/blob/master/Charts.md

I tried to use $toMillis() with Jsonata If there is only one value in the Json feed, it works, but not when there is multipple posts with the same name. If i use $toMillis($.$string(payload.from)) I got this error message: The argument of the toMillis function must be an ISO 8601 formatted timestamp. Given "[\"2022-11-27T00:00:00.000+01:00\",\"2022-11-28T00:00:00.000+01:00\",\"2022-11-...

I also tried to get rid of the \ with: $toMillis($replace($string(payload.from),/\"/, "")) The same error message but without the \ . The argument of the toMillis function must be an ISO 8601 formatted timestamp. Given "[2022-11-27T00:00:00.000+01:00,2022-11-28T00:00:00.000+01:00,2022-11-29T00:00:00.000+01:00,2022-11-30T00:00:00.000+01:00,2022-12-01T00:00:00.000+01:00....

I tried both using change node with jsonata and a function node. I can't figure it out despite persistent googling. Anyone made a similar solution?

The Json data from Tibber {"_msgid":"87b329529f9dc0fa","payload":[{"from":"2022-11-27T00:00:00.000+01:00","to":"2022-11-28T00:00:00.000+01:00","cost":53.2597735,"unitPrice":1.621796,"unitPriceVAT":0.324359,"consumption":32.84,"consumptionUnit":"kWh","currency":"SEK"},{"from":"2022-11-28T00:00:00.000+01:00","to":"2022-11-29T00:00:00.000+01:00","cost":57.8222245,"unitPrice":2.295444,"unitPriceVAT":0.459089,"consumption":25.19,"consumptionUnit":"kWh","currency":"SEK"},{"from":"2022-11-29T00:00:00.000+01:00","to":"2022-11-30T00:00:00.000+01:00","cost":105.64867525,"unitPrice":4.109244,"unitPriceVAT":0.821849,"consumption":25.71,"consumptionUnit":"kWh","currency":"SEK"},{"from":"2022-11-30T00:00:00.000+01:00","to":"2022-12-01T00:00:00.000+01:00","cost":132.644581625,"unitPrice":5.201748,"unitPriceVAT":1.04035,"consumption":25.5,"consumptionUnit":"kWh","currency":"SEK"},{"from":"2022-12-01T00:00:00.000+01:00","to":"2022-12-02T00:00:00.000+01:00","cost":161.4772125,"unitPrice":4.843348,"unitPriceVAT":0.96867,"consumption":33.34,"consumptionUnit":"kWh","currency":"SEK"},{"from":"2022-12-02T00:00:00.000+01:00","to":"2022-12-03T00:00:00.000+01:00","cost":163.027514625,"unitPrice":4.196332,"unitPriceVAT":0.839266,"consumption":38.85,"consumptionUnit":"kWh","currency":"SEK"},{"from":"2022-12-03T00:00:00.000+01:00","to":"2022-12-04T00:00:00.000+01:00","cost":174.9835495,"unitPrice":3.560194,"unitPriceVAT":0.712039,"consumption":49.15,"consumptionUnit":"kWh","currency":"SEK"},{"from":"2022-12-04T00:00:00.000+01:00","to":"2022-12-05T00:00:00.000+01:00","cost":151.800119375,"unitPrice":3.496893,"unitPriceVAT":0.699379,"consumption":43.41,"consumptionUnit":"kWh","currency":"SEK"},{"from":"2022-12-05T00:00:00.000+01:00","to":"2022-12-06T00:00:00.000+01:00","cost":110.539872125,"unitPrice":3.284013,"unitPriceVAT":0.656803,"consumption":33.66,"consumptionUnit":"kWh","currency":"SEK"},{"from":"2022-12-06T00:00:00.000+01:00","to":"2022-12-07T00:00:00.000+01:00","cost":137.99885775,"unitPrice":2.712775,"unitPriceVAT":0.542555,"consumption":50.87,"consumptionUnit":"kWh","currency":"SEK"},{"from":"2022-12-07T00:00:00.000+01:00","to":"2022-12-08T00:00:00.000+01:00","cost":118.96963325,"unitPrice":4.161232,"unitPriceVAT":0.832246,"consumption":28.59,"consumptionUnit":"kWh","currency":"SEK"},{"from":"2022-12-08T00:00:00.000+01:00","to":"2022-12-09T00:00:00.000+01:00","cost":139.668237625,"unitPrice":3.623981,"unitPriceVAT":0.724796,"consumption":38.54,"consumptionUnit":"kWh","currency":"SEK"},{"from":"2022-12-09T00:00:00.000+01:00","to":"2022-12-10T00:00:00.000+01:00","cost":129.516830125,"unitPrice":4.14985,"unitPriceVAT":0.82997,"consumption":31.21,"consumptionUnit":"kWh","currency":"SEK"},{"from":"2022-12-10T00:00:00.000+01:00","to":"2022-12-11T00:00:00.000+01:00","cost":139.408396,"unitPrice":3.270962,"unitPriceVAT":0.654192,"consumption":42.62,"consumptionUnit":"kWh","currency":"SEK"},{"from":"2022-12-11T00:00:00.000+01:00","to":"2022-12-12T00:00:00.000+01:00","cost":136.706897125,"unitPrice":3.313303,"unitPriceVAT":0.662661,"consumption":41.26,"consumptionUnit":"kWh","currency":"SEK"},{"from":"2022-12-12T00:00:00.000+01:00","to":"2022-12-13T00:00:00.000+01:00","cost":152.189356125,"unitPrice":3.793354,"unitPriceVAT":0.758671,"consumption":40.12,"consumptionUnit":"kWh","currency":"SEK"},{"from":"2022-12-13T00:00:00.000+01:00","to":"2022-12-14T00:00:00.000+01:00","cost":190.960746625,"unitPrice":4.878915,"unitPriceVAT":0.975783,"consumption":39.14,"consumptionUnit":"kWh","currency":"SEK"},{"from":"2022-12-14T00:00:00.000+01:00","to":"2022-12-15T00:00:00.000+01:00","cost":251.084173375,"unitPrice":5.770723,"unitPriceVAT":1.154145,"consumption":43.51,"consumptionUnit":"kWh","currency":"SEK"},{"from":"2022-12-15T00:00:00.000+01:00","to":"2022-12-16T00:00:00.000+01:00","cost":173.132418125,"unitPrice":3.145003,"unitPriceVAT":0.629001,"consumption":55.05,"consumptionUnit":"kWh","currency":"SEK"},{"from":"2022-12-16T00:00:00.000+01:00","to":"2022-12-17T00:00:00.000+01:00","cost":106.927210375,"unitPrice":1.830632,"unitPriceVAT":0.366126,"consumption":58.41,"consumptionUnit":"kWh","currency":"SEK"},{"from":"2022-12-17T00:00:00.000+01:00","to":"2022-12-18T00:00:00.000+01:00","cost":99.312449625,"unitPrice":1.734715,"unitPriceVAT":0.346943,"consumption":57.25,"consumptionUnit":"kWh","currency":"SEK"},{"from":"2022-12-18T00:00:00.000+01:00","to":"2022-12-19T00:00:00.000+01:00","cost":52.953907625,"unitPrice":1.173624,"unitPriceVAT":0.234725,"consumption":45.12,"consumptionUnit":"kWh","currency":"SEK"},{"from":"2022-12-19T00:00:00.000+01:00","to":"2022-12-20T00:00:00.000+01:00","cost":75.029905125,"unitPrice":1.447336,"unitPriceVAT":0.289467,"consumption":51.84,"consumptionUnit":"kWh","currency":"SEK"},{"from":"2022-12-20T00:00:00.000+01:00","to":"2022-12-21T00:00:00.000+01:00","cost":90.759338,"unitPrice":2.052914,"unitPriceVAT":0.410583,"consumption":44.21,"consumptionUnit":"kWh","currency":"SEK"}],"topic":""}

Example of

bisand commented 1 year ago

If you use Javascript you should be able to convert it using the Date's function .getTime(). See example below: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime

const event = new Date('2022-11-27T00:00:00.000+01:00');
console.log(event.toString());
// expected output: "Sun Nov 27 2022 00:00:00 GMT+0100 (Central European Standard Time)"
console.log(event.toISOString());
// expected output: "2022-11-26T23:00:00.000Z"
console.log(event.getTime());
// expected output: 1669503600000
bisand commented 1 year ago

Closing due to inactivity