Format time after JSON query

Here's the problem. I don't think it is too hard to overcome but I'm getting myself into a mess trying to figure it out.

The flow looks at an api and returns some of the data. One element is time and it is formatted in a (visually) ugly way on my work in progress dashboard

image

I'd like to have it read a calendar type date, so something along the lines of 'Today at 20:29' which I think will happen. I've simplified the flow to. Hopefully it make sense.

I was aiming at following this thread but there is something missing.
The debug window gives me

Input property, Date, does NOT exist in the input msg. Output has been set to NOW.

I had assumed it was looking for the correct field out of the function I use to format the JSOn data (which I pinched from elsewhere)

data = msg.payload[msg.payload.length-1];
report = "" + data.id + " " + data.tle0 + " " + data.frequency + " " + data.mode;

remaining = Math.round(((new Date(data.start).getTime()) - (new Date().getTime())) / 60000);

return {
    count : msg.payload.length,
    data : data,
    report : report,
    remaining : remaining
}

The flow in full is:

[{"id":"aab2522b.30f63","type":"function","z":"8a486d35.989e88","name":"get schd passes","func":"data = msg.payload[msg.payload.length-1];\nreport = \"\" + data.id + \" \" + data.tle0 + \" \" + data.frequency + \" \" + data.mode;\n\nremaining = Math.round(((new Date(data.start).getTime()) - (new Date().getTime())) / 60000);\n\nreturn {\n count : msg.payload.length,\n data : data,\n report : report,\n remaining : remaining\n}","outputs":1,"noerr":0,"x":560,"y":340,"wires":[["df281de6.15fe28"]]}]

Your help would be appreciated.

That is not a flow, it is only the function node.

Here's the whole flow

[{"id":"d8ffbf6a.b605d","type":"tab","label":"G7KSE VHF","disabled":false,"info":""},{"id":"9dad6ebd.f0248","type":"tab","label":"G7KSE UHF","disabled":false,"info":""},{"id":"a56f29d3.12da48","type":"tab","label":"General Information","disabled":false,"info":""},{"id":"87c7f3e6.8d2368","type":"ui_base","theme":{"name":"theme-dark","lightTheme":{"default":"#0094CE","baseColor":"#0094CE","baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif","edited":true,"reset":false},"darkTheme":{"default":"#097479","baseColor":"#394448","baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif","edited":true,"reset":false},"customTheme":{"name":"Untitled Theme 1","default":"#4B7930","baseColor":"#4B7930","baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif"},"themeState":{"base-color":{"default":"#097479","value":"#394448","edited":true},"page-titlebar-backgroundColor":{"value":"#394448","edited":false},"page-backgroundColor":{"value":"#111111","edited":false},"page-sidebar-backgroundColor":{"value":"#000000","edited":false},"group-textColor":{"value":"#5b6c73","edited":false},"group-borderColor":{"value":"#555555","edited":false},"group-backgroundColor":{"value":"#333333","edited":false},"widget-textColor":{"value":"#eeeeee","edited":false},"widget-backgroundColor":{"value":"#394448","edited":false},"widget-borderColor":{"value":"#333333","edited":false},"base-font":{"value":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif"}},"angularTheme":{"primary":"indigo","accents":"blue","warn":"red","background":"grey"}},"site":{"name":"SatNOGS GS Dashboard","hideToolbar":"false","allowSwipe":"true","lockMenu":"true","allowTempTheme":"false","dateFormat":"DD/MM/YYYY","sizes":{"sx":48,"sy":48,"gx":6,"gy":6,"cx":6,"cy":6,"px":0,"py":0}}},{"id":"166441f4.1e2f3e","type":"ui_group","z":"","name":"VHF","tab":"1a85f56f.e2c8c3","order":3,"disp":true,"width":"6","collapse":false},{"id":"d9ab9a6f.d3846","type":"ui_group","z":"","name":"UHF","tab":"1a85f56f.e2c8c3","order":2,"disp":true,"width":"6","collapse":false},{"id":"bc20b4c2.49b01","type":"ui_group","z":"","name":"General Information","tab":"1a85f56f.e2c8c3","order":1,"disp":true,"width":"12","collapse":false},{"id":"1a85f56f.e2c8c3","type":"ui_tab","z":"","name":"SatNOGS Ground Station Dashboard","icon":"dashboard","order":1,"disabled":false,"hidden":false},{"id":"a5e8d7c9.b00da","type":"ui_spacer","name":"spacer","group":"bc20b4c2.49b01","order":1,"width":1,"height":1},{"id":"1efe931e.94f435","type":"ui_spacer","name":"spacer","group":"166441f4.1e2f3e","order":2,"width":1,"height":1},{"id":"bb035244.07f438","type":"ui_group","z":"","name":"Default","tab":"","disp":true,"width":"6","collapse":false},{"id":"81ac0601.bb5ce","type":"ui_group","z":"","name":"group1","tab":"","disp":true,"width":"6","collapse":false},{"id":"3cd2fc25.541874","type":"ui_group","z":"","name":"group2","tab":"","disp":true,"width":"6","collapse":false},{"id":"8e96250a.3a20f","type":"mqtt-broker","z":"","name":"Node-RED","broker":"localhost","port":"1883","clientid":"Node-RED","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"5cd0451d.2c0cac","type":"ui_group","z":"","name":"Home","tab":"","disp":true,"width":"6","collapse":false},{"id":"a51d8dc9.a1dc6","type":"ui_group","z":"","name":"Debug","tab":"","disp":true,"width":"6","collapse":false},{"id":"838bd601.8d4c8","type":"ui_group","z":"","name":"LED example - Allow Colors for Values in msg","tab":"","disp":true,"width":"6","collapse":false},{"id":"c2962211.06f448","type":"ui_group","z":"","name":"LED example - Allow Colors for Values in msg","tab":"54eac8fb.ac5258","disp":true,"width":"6","collapse":false},{"id":"54eac8fb.ac5258","type":"ui_tab","z":"","name":"Home","icon":"dashboard","order":1,"disabled":false,"hidden":false},{"id":"bce9b1e1.bcdae","type":"inject","z":"d8ffbf6a.b605d","name":"GS33 - G7KSE VHF @ 15min","topic":"","payload":"","payloadType":"date","repeat":"900","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":240,"wires":[["f6ab7d61.0f5b9"]]},{"id":"f6ab7d61.0f5b9","type":"http request","z":"d8ffbf6a.b605d","name":"station status","method":"GET","ret":"obj","paytoqs":false,"url":"https://network.satnogs.org/api/stations/?id=33","tls":"","proxy":"","x":400,"y":240,"wires":[["4e75104.af7d3f","a03a63d9.1abeb","d2ff2fc.be2afd"]]},{"id":"4e75104.af7d3f","type":"function","z":"d8ffbf6a.b605d","name":"up time","func":"end = new Date(msg.payload[0].created);\nstart = new Date(msg.payload[0].last_seen);\n\nmsg.uptime = start.getTime() - end.getTime();\nhours = parseFloat(msg.uptime / 3600000);\nmsg.days = parseInt(hours / 24);\nmsg.hours = parseInt(hours % 24);\nmsg.minutes = parseInt((msg.uptime / 60000) % 60);\n\nreturn msg;","outputs":1,"noerr":0,"x":700,"y":240,"wires":[["3e232f09.da78a"]]},{"id":"d258833b.84fa88","type":"http request","z":"d8ffbf6a.b605d","name":"sched list","method":"GET","ret":"obj","paytoqs":false,"url":"https://network.satnogs.org/api/jobs/?id=&ground_station=33","tls":"","proxy":"","x":280,"y":400,"wires":[["5f32b197.eabab8"]]},{"id":"53cd3f4b.96484","type":"inject","z":"d8ffbf6a.b605d","name":"5 min","topic":"","payload":"","payloadType":"date","repeat":"300","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":400,"wires":[["d258833b.84fa88"]]},{"id":"5f32b197.eabab8","type":"function","z":"d8ffbf6a.b605d","name":"get schd passes","func":"data = msg.payload[msg.payload.length-1];\nreport = \"\" + data.id + \" \" + data.tle0 + \" \" + data.frequency + \" \" + data.mode;\n\nremaining = Math.round(((new Date(data.start).getTime()) - (new Date().getTime())) / 60000);\n\nreturn {\n    count : msg.payload.length,\n    data : data,\n    report : report,\n    remaining : remaining\n}","outputs":1,"noerr":0,"x":460,"y":400,"wires":[["aeb04542.3a1a38","35f6dc9d.971164","e142bab5.fe94e","dc430762.c9993","58aca1ee.dca2e","881f01e2.57b12"]]},{"id":"a03a63d9.1abeb","type":"ui_text","z":"d8ffbf6a.b605d","group":"bc20b4c2.49b01","order":2,"width":0,"height":0,"name":"","label":"VHF Station Status:","format":"{{msg.payload[0].status}}","layout":"row-spread","x":630,"y":80,"wires":[]},{"id":"d2ff2fc.be2afd","type":"ui_text","z":"d8ffbf6a.b605d","group":"bc20b4c2.49b01","order":3,"width":0,"height":0,"name":"","label":"VHF Station Observations:","format":"{{msg.payload[0].observations}}","layout":"row-spread","x":660,"y":140,"wires":[]},{"id":"3e232f09.da78a","type":"ui_text","z":"d8ffbf6a.b605d","group":"bc20b4c2.49b01","order":4,"width":0,"height":0,"name":"","label":"VHF Station Uptime (days):","format":"{{msg.days}}","layout":"row-spread","x":1000,"y":240,"wires":[]},{"id":"aeb04542.3a1a38","type":"ui_text","z":"d8ffbf6a.b605d","group":"166441f4.1e2f3e","order":5,"width":0,"height":0,"name":"","label":"Number of scheduled passes:","format":"{{msg.count}}","layout":"row-spread","x":1030,"y":360,"wires":[]},{"id":"881f01e2.57b12","type":"ui_text","z":"d8ffbf6a.b605d","group":"166441f4.1e2f3e","order":6,"width":0,"height":0,"name":"","label":"Start of next pass:","format":"{{msg.data.start}}","layout":"row-spread","x":990,"y":400,"wires":[]},{"id":"35f6dc9d.971164","type":"ui_text","z":"d8ffbf6a.b605d","group":"166441f4.1e2f3e","order":7,"width":0,"height":0,"name":"","label":"Name of next pass:","format":"{{msg.data.tle0}}","layout":"row-spread","x":990,"y":440,"wires":[]},{"id":"e142bab5.fe94e","type":"ui_text","z":"d8ffbf6a.b605d","group":"166441f4.1e2f3e","order":8,"width":0,"height":0,"name":"","label":"Mode of next pass:","format":"{{msg.data.mode}}","layout":"row-spread","x":990,"y":480,"wires":[]},{"id":"dc430762.c9993","type":"ui_text","z":"d8ffbf6a.b605d","group":"166441f4.1e2f3e","order":9,"width":0,"height":0,"name":"","label":"Frequency of next pass:","format":"{{msg.data.frequency}}","layout":"row-spread","x":1010,"y":520,"wires":[]},{"id":"58aca1ee.dca2e","type":"ui_text","z":"d8ffbf6a.b605d","group":"166441f4.1e2f3e","order":10,"width":0,"height":0,"name":"","label":"Minutes till next pass:","format":"{{msg.remaining}}","layout":"row-spread","x":1000,"y":560,"wires":[]},{"id":"3704ba27.27f966","type":"inject","z":"9dad6ebd.f0248","name":"G7KSE UHF @ 15min","topic":"","payload":"","payloadType":"date","repeat":"900","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":200,"wires":[["d7c2c9b7.7e42d"]]},{"id":"d7c2c9b7.7e42d","type":"http request","z":"9dad6ebd.f0248","name":"station status","method":"GET","ret":"obj","paytoqs":false,"url":"https://network.satnogs.org/api/stations/?id=328","tls":"","proxy":"","x":380,"y":200,"wires":[["7f9c8c4b.98eb6c","e4e222d2.3115d","f13d4d8.5869fb"]]},{"id":"cc7b077c.959358","type":"http request","z":"9dad6ebd.f0248","name":"Obs list","method":"GET","ret":"obj","paytoqs":false,"url":"https://network.satnogs.org/api/observations/?ground_station=","tls":"","proxy":"","x":280,"y":440,"wires":[[]]},{"id":"7f9c8c4b.98eb6c","type":"function","z":"9dad6ebd.f0248","name":"up time","func":"end = new Date(msg.payload[0].created);\nstart = new Date(msg.payload[0].last_seen);\n\nmsg.uptime = start.getTime() - end.getTime();\nhours = parseFloat(msg.uptime / 3600000);\nmsg.days = parseInt(hours / 24);\nmsg.hours = parseInt(hours % 24);\nmsg.minutes = parseInt((msg.uptime / 60000) % 60);\n\nreturn msg;","outputs":1,"noerr":0,"x":620,"y":200,"wires":[["b12f6784.29e9e8"]]},{"id":"f84d6498.4727f","type":"http request","z":"9dad6ebd.f0248","name":"sched list","method":"GET","ret":"obj","paytoqs":false,"url":"https://network.satnogs.org/api/jobs/?id=&ground_station=","tls":"","proxy":"","x":280,"y":360,"wires":[["874306ff.90aaa"]]},{"id":"54f5066b.d15ac8","type":"inject","z":"9dad6ebd.f0248","name":"5 min","topic":"","payload":"","payloadType":"date","repeat":"300","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":360,"wires":[["f84d6498.4727f"]]},{"id":"874306ff.90aaa","type":"function","z":"9dad6ebd.f0248","name":"get schd passes","func":"data = msg.payload[msg.payload.length-1];\nreport = \"\" + data.id + \" \" + data.tle0 + \" \" + data.frequency + \" \" + data.mode;\n\nremaining = Math.round(((new Date(data.start).getTime()) - (new Date().getTime())) / 60000);\n\nreturn {\n    count : msg.payload.length,\n    data : data,\n    report : report,\n    remaining : remaining\n}","outputs":1,"noerr":0,"x":460,"y":380,"wires":[["dc428517.f56af8","6aaadaf0.0eb41c","9bfa0cfd.0cf78","b8686bd2.6a7338","9a6631e8.c24178","e990f869.a01018"]]},{"id":"e990f869.a01018","type":"ui_text","z":"9dad6ebd.f0248","group":"d9ab9a6f.d3846","order":10,"width":0,"height":0,"name":"","label":"Minutes till next pass:","format":"{{msg.remaining}}","layout":"row-spread","x":1000,"y":520,"wires":[]},{"id":"e4e222d2.3115d","type":"ui_text","z":"9dad6ebd.f0248","group":"bc20b4c2.49b01","order":2,"width":0,"height":0,"name":"","label":"UHF Station Status:","format":"{{msg.payload[0].status}}","layout":"row-spread","x":550,"y":80,"wires":[]},{"id":"f13d4d8.5869fb","type":"ui_text","z":"9dad6ebd.f0248","group":"bc20b4c2.49b01","order":3,"width":0,"height":0,"name":"","label":"UHF Station Observations:","format":"{{msg.payload[0].observations}}","layout":"row-spread","x":700,"y":140,"wires":[]},{"id":"b12f6784.29e9e8","type":"ui_text","z":"9dad6ebd.f0248","group":"bc20b4c2.49b01","order":4,"width":0,"height":0,"name":"","label":"UHF Station Uptime (days):","format":"{{msg.days}}","layout":"row-spread","x":1000,"y":200,"wires":[]},{"id":"dc428517.f56af8","type":"ui_text","z":"9dad6ebd.f0248","group":"d9ab9a6f.d3846","order":5,"width":0,"height":0,"name":"","label":"Number of scheduled passes:","format":"{{msg.count}}","layout":"row-spread","x":1030,"y":320,"wires":[]},{"id":"6aaadaf0.0eb41c","type":"ui_text","z":"9dad6ebd.f0248","group":"d9ab9a6f.d3846","order":6,"width":0,"height":0,"name":"","label":"Start of next pass:","format":"{{msg.data.start}}","layout":"row-spread","x":990,"y":360,"wires":[]},{"id":"9bfa0cfd.0cf78","type":"ui_text","z":"9dad6ebd.f0248","group":"d9ab9a6f.d3846","order":7,"width":0,"height":0,"name":"","label":"Name of next pass:","format":"{{msg.data.tle0}}","layout":"row-spread","x":990,"y":400,"wires":[]},{"id":"b8686bd2.6a7338","type":"ui_text","z":"9dad6ebd.f0248","group":"d9ab9a6f.d3846","order":8,"width":0,"height":0,"name":"","label":"Mode of next pass:","format":"{{msg.data.mode}}","layout":"row-spread","x":990,"y":440,"wires":[]},{"id":"9a6631e8.c24178","type":"ui_text","z":"9dad6ebd.f0248","group":"d9ab9a6f.d3846","order":9,"width":0,"height":0,"name":"","label":"Frequency of next pass:","format":"{{msg.data.frequency}}","layout":"row-spread","x":1010,"y":480,"wires":[]},{"id":"cdbd1dfd.24b238","type":"inject","z":"a56f29d3.12da48","name":"1 sec","topic":"","payload":"","payloadType":"date","repeat":"1","crontab":"","once":false,"onceDelay":0.1,"x":142,"y":73,"wires":[["74be1adf.2652bc"]]},{"id":"3dadf399.679a2c","type":"ui_text","z":"a56f29d3.12da48","group":"bc20b4c2.49b01","order":1,"width":0,"height":0,"name":"","label":"UTC:","format":"{{msg.payload}}","layout":"row-spread","x":722,"y":73,"wires":[]},{"id":"74be1adf.2652bc","type":"moment","z":"a56f29d3.12da48","name":"UTC","topic":"","input":"","format":"LLL","locale":"en_US","output":"payload","fakeUTC":false,"x":322,"y":73,"wires":[["3dadf399.679a2c","642b765d.196c"]]},{"id":"642b765d.196c","type":"debug","z":"a56f29d3.12da48","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":491,"y":151,"wires":[],"l":false}]

getting rid of everything but the inject, http request, function and adding a debug. The first time I did an inject I saw an error but after that it is working. The debug shows:
Screen Shot 2019-12-12 at 4.55.20 AM

I might not have asked the question well but I get a good dashboard that is consistent but I wanted to change the date format on the VHF & UHF panels to match that in the General Information one. I can't seem to find the right function to format the data correctly.

This function node sorts out the data for me

data = msg.payload[msg.payload.length-1];
report = "" + data.id + " " + data.tle0 + " " + data.frequency + " " + data.mode;

remaining = Math.round(((new Date(data.start).getTime()) - (new Date().getTime())) / 60000);

return {
    count : msg.payload.length,
    data : data,
    report : report,
    remaining : remaining
}

I had assumed that if I wanted to change the date /time I would just format part of the {{msg.data.start}} to something else. I'm probably looking in the wrong place / not explaining very well.

So you want the date/time that looks like this : 2019-12-12T19:02:34Z to look like this: December 12, 2019 7:05 PM

You could use node-red-contrib-moment but you would first have to strip the trailing Z to make it a valid timestamp the node could handle.

Here is a sample flow demonstrating the method:

[{"id":"98a9005f.98de38","type":"tab","label":"dash","disabled":false,"info":""},{"id":"79d3d6f9.4f2fb","type":"inject","z":"98a9005f.98de38","name":"","topic":"does this show","payload":"2019-12-12T19:02:34Z","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":120,"wires":[["90608bd1.b14ce8"]]},{"id":"892952da.8ad578","type":"debug","z":"98a9005f.98de38","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":290,"y":300,"wires":[]},{"id":"377db78e.426d5","type":"moment","z":"98a9005f.98de38","name":"","topic":"","input":"","inputType":"msg","inTz":"America/New_York","adjAmount":0,"adjType":"days","adjDir":"add","format":"LLLL","locale":"en_US","output":"","outputType":"msg","outTz":"America/New_York","x":260,"y":240,"wires":[["892952da.8ad578"]]},{"id":"90608bd1.b14ce8","type":"change","z":"98a9005f.98de38","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"Z","fromt":"str","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":220,"y":180,"wires":[["377db78e.426d5"]]}]
1 Like

Awesome. Thanks @zenofmud I was just trying to do it the wrong way. That has been annoying me for a long time