Read/write json revisited

Hi,

I'm using text input (set as time picker) to store hours and minutes to a json file. This part works (well...).
Then when the web page is loaded/reloaded, I need to read the json file and set the text input to the saved time.

So, I'm having a few issues:

  1. I cannot get to parse the json data to manipulate it.
  2. I don't know how to read said file when the web page is loaded.
  3. I'm now storing hour and minutes, but what if the file contains more data like:

{"led": on, "relay": off, "hour":12, "minute":15}

I really appreciate some assistance on this.
TIA

[{"id":"e3f00cd4.c13ba8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"d084f6b3.0b2fe8","type":"debug","z":"e3f00cd4.c13ba8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":870,"y":220,"wires":[]},{"id":"60686e6d.c19ee8","type":"ui_text_input","z":"e3f00cd4.c13ba8","name":"webTime","label":"Hora persiana","tooltip":"","group":"e624b78b.0592d8","order":2,"width":"3","height":"1","passthru":false,"mode":"time","delay":"400","topic":"","x":100,"y":140,"wires":[["f88152f4.dd1"]]},{"id":"f88152f4.dd1","type":"function","z":"e3f00cd4.c13ba8","name":"extractTime","func":"var time = msg.payload;\nvar minutes = time / 60000;\nvar hour = 0;\nif (minutes > 59) {\n    hour = Math.floor(minutes / 60)\n    minutes = minutes % 60\n}\nmsg.payload = {\"hour\":hour, \"minute\":minutes};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":250,"y":140,"wires":[["6402657c.6bc40c"]]},{"id":"c672fbec.bf17d8","type":"file","z":"e3f00cd4.c13ba8","name":"","filename":"/home/pi/test.log","appendNewline":false,"createDir":false,"overwriteFile":"true","encoding":"none","x":540,"y":140,"wires":[[]]},{"id":"6402657c.6bc40c","type":"json","z":"e3f00cd4.c13ba8","name":"","property":"payload","action":"","pretty":false,"x":390,"y":140,"wires":[["c672fbec.bf17d8"]]},{"id":"b1db9cb4.baf77","type":"inject","z":"e3f00cd4.c13ba8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":90,"y":220,"wires":[["9d14a4c.cb751d8"]]},{"id":"9d14a4c.cb751d8","type":"file in","z":"e3f00cd4.c13ba8","name":"","filename":"/home/pi/test.log","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":290,"y":220,"wires":[["fe5c13a5.c61c6"]]},{"id":"fe5c13a5.c61c6","type":"function","z":"e3f00cd4.c13ba8","name":"timeConvert","func":"var m=msg.payload;\nvar hr=JSON.parse(m.hour);\nvar min=JSON.parse(x.minute);\nvar time=((hr*60)+(min))*60000;\nmsg.payload=time;\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":510,"y":220,"wires":[["d084f6b3.0b2fe8","60686e6d.c19ee8"]]},{"id":"e624b78b.0592d8","type":"ui_group","z":"","name":"Mnto","tab":"2332b0a1.2f261","order":3,"disp":true,"width":"6","collapse":true},{"id":"2332b0a1.2f261","type":"ui_tab","z":"","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

The JSON node will turn a string of JSON back into a JS object

Use the UI_Control node to let you know when page is loaded

If it stores more data - dont use it - ignore it!

Based on your comments it seems to me you might not be quite getting JSON / JS Objects.

This will explain how to use the debug panel to find the right path to any data item.

https://nodered.org/docs/user-guide/messages

This' the first obstacle.

var m=msg.payload 
//var hr=JSON.parse(m.hour);
//var min=JSON.parse(x.minute);
//var time=((hr*60)+(min))*60000;
msg.payload=m;
return msg;   //returns {"hour":12,"minute":00}

but

var m=msg.payload
var hr=JSON.parse(m.hour);
//var min=JSON.parse(x.minute);
//var time=((hr60)+(min))60000;
msg.payload=hr;
return msg; //
* fails

Try this example

[{"id":"26dffcb.5654f84","type":"ui_text_input","z":"a1dea2b.76223e","name":"webTime","label":"Hora persiana","tooltip":"","group":"f7409047.72d11","order":2,"width":"3","height":"1","passthru":true,"mode":"time","delay":"400","topic":"","x":100,"y":140,"wires":[["ded6e057.f6c1c8","f580a7a9.8e6188"]]},{"id":"ded6e057.f6c1c8","type":"function","z":"a1dea2b.76223e","name":"extractTime","func":"var time = msg.payload;\nvar hour = Math.floor(time / 3600000);\nvar minutes = Math.round((time % 3600000) / 60000);\nmsg.payload = {\"hour\":hour, \"minute\":minutes};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":300,"y":120,"wires":[["f9251ea2.35ea8","f580a7a9.8e6188"]]},{"id":"44fb75fe.12ae14","type":"function","z":"a1dea2b.76223e","name":"timeConvert","func":"var m=msg.payload;\nvar time=((m.hour*3600000)+(m.minute*60000));\nmsg.payload=time;\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":540,"y":220,"wires":[["26dffcb.5654f84","f580a7a9.8e6188"]]},{"id":"f580a7a9.8e6188","type":"debug","z":"a1dea2b.76223e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":300,"wires":[]},{"id":"f9251ea2.35ea8","type":"json","z":"a1dea2b.76223e","name":"","property":"payload","action":"","pretty":false,"x":450,"y":120,"wires":[["f580a7a9.8e6188"]]},{"id":"1459ca5e.bb38fe","type":"json","z":"a1dea2b.76223e","name":"","property":"payload","action":"","pretty":false,"x":350,"y":220,"wires":[["44fb75fe.12ae14","f580a7a9.8e6188"]]},{"id":"c1397013.69a42","type":"inject","z":"a1dea2b.76223e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"hour\":12,\"minute\":57}","payloadType":"str","x":140,"y":220,"wires":[["1459ca5e.bb38fe","f580a7a9.8e6188"]]},{"id":"f7409047.72d11","type":"ui_group","z":"","name":"Mnto","tab":"c84fe82d.fb3f58","order":3,"disp":true,"width":"6","collapse":true},{"id":"c84fe82d.fb3f58","type":"ui_tab","z":"","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

It worked, thank you much!!

Just one more question:

The write-to-file node has the option to overwrite or to append. But what if I want to modify the data? Should I read it first, modify it and then overwite the old data with the new one? Or is there a way to modify just the corrected data, hour and minute, in this case?

Thanks again.

Yes as it has to be valid json, if you append you would create a non valid json.
so read json, change values, then overwrite.

Not with text files, you could use context storage or a data base, then you could target just the changing variables.

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