EDIT: The below question can be ignored as it turns out, seems I'm the dumby afterall and this just got a lot easier . I'll leave this open if that's cool as I'm bound to have future issues/questions.
Ok so just to preface, I'm brand new to Node-Red (and js if I'm honest) so please bear with me as I'll definitely being doing a lot of this wrong or inefficiently The fun is in the learning though.
Basically what I'm trying to do here is interface with my ducted airconditioning API so that I can automate some things in HomeAssistant. The company hasn't officially released the API and it doesn't sound like they're going to be much help (and their app is rubbish and unreliable), hence the tinkering.
I do have some of this working already, in that I can can turn the aircon on and off in HomeAssistant now (built in Node-Red).
What I need to do is:
Use a static access token (I already have this) to get a Bearer token from their API so that I can issue commands. This token expires after 72 hours, which is where most of my issue is coming from. I'm already able to grab a Bearer token using Node-Red, however it's pretty dumb at the moment as their system simply dishes out a new token to you whenever you ask, however none of the new tokens work until the initial token has expired so I need a way to determine that.
So what I'm trying to do is essentially grab a token, get the current time and add 72 hours to it and then write this to a file. Then next time I try and grab a token, check this file and if the current time is less than the time in the file, use the existing token, otherwise grab a new one.
My issue is that when I save the date to the file, it's saved as a string, When I then read this file later, it's still a string so I can't work out how to either convert that, or somehow just find a way to compare it to the current time.
Now there's likely a much better way of going about this so I'm all ears, honestly
I've attached a modified flow below. I've stripped out all the API stuff and am just injecting a file at the start with what the API would be returning so you can see how it's working (or not).
[{"id":"fe10a1ed.44f0b","type":"tab","label":"Flow 8","disabled":false,"info":""},{"id":"eed6c365.c39fd","type":"file","z":"fe10a1ed.44f0b","name":"Token to file","filename":"/data/Bearer.txt","appendNewline":false,"createDir":true,"overwriteFile":"true","encoding":"none","x":626.5,"y":160,"wires":[[]]},{"id":"b38d3d46.e321e","type":"debug","z":"fe10a1ed.44f0b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":758,"y":398,"wires":[]},{"id":"f1b4b359.ed134","type":"function","z":"fe10a1ed.44f0b","name":"Split Bearer Token","func":"var access = msg.payload.access_token;\nmsg.payload = access;\nreturn msg;\n","outputs":1,"noerr":0,"x":309,"y":164,"wires":[["b38d3d46.e321e"]]},{"id":"208d42c6.494eae","type":"function","z":"fe10a1ed.44f0b","name":"Split Expiry + add to current time","func":"var t = new Date();\nvar expiry = msg.payload.expires_in;\nt = new Date(t.getTime() + (1000 * expiry))\nmsg.payload = t\nreturn msg;","outputs":1,"noerr":0,"x":352,"y":269,"wires":[["b38d3d46.e321e"]]},{"id":"72ef7d48.3ad914","type":"file","z":"fe10a1ed.44f0b","name":"Expiry to file","filename":"/data/Expiry.txt","appendNewline":false,"createDir":true,"overwriteFile":"true","encoding":"none","x":650.5,"y":271,"wires":[[]]},{"id":"e586db6f.e414a8","type":"inject","z":"fe10a1ed.44f0b","name":"","topic":"","payload":"{\"access_token\":\"gU8S6TqKLnUHGSojIs4fzclitepZkdCBBm-hD159i6tY_TfZKOL2_lTxoWrBo1Fk49_Y7S7ftb17ASkwRPhBm2Uz5VKmiaQmN-hFhI-LLObydy6FCK5WqmsZnLMcjY_i4AKoPZyTE-M1CaRZbCVrSLEg4jfKydNKx76dMGjB9gLLX1HfjF9AHDabntg2zOMz-FRbvBlR_O2MOFYOgTHikkpFG04-quTh6wm7_0d98XISYwWOM4xyOYa1euIsmz367lOWR04dowaq47xNq0RsQzWbtfwUVulyKW-fYJAUedM2zW0IhXDQAQZlG9fu3_8EihpgQOfm-faIA_9kwScBnk4QSQWcs0AinY0GRKHmkialJ0sQcWHRW2fKUbwejxQMrEff-TLyK2df-M6jg-iSFYANrOHsTAO6gsnw48YJvxcGGZKBaSzIwOOLLuUV3Ow4t-PXafRm03Q9o0FavWpZX_BWGshLgdzYJlQys0RTp888_xKFNLnaFLR8ZYD-r8iNIup_852mvlvUvGOkbNi1kg\",\"token_type\":\"bearer\",\"expires_in\":259199}","payloadType":"json","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":97,"y":227,"wires":[["208d42c6.494eae","f1b4b359.ed134"]]}]
Thanks a lot for your help, Node-Red is pretty damn awesome...
Summary
This text will be hidden