save data change to database (SQLite)
Example JSON object data within an object consists of various separate items.
Utilize Function Nodes to access data for each item.
Send the obtained data through a filter to check for changes. If the data has changed from the original, it will be sent to the next section.
Organize the data into a new format, creating a set of insert commands to store the data in the database.
Database section.
Copy JSON
Note: some third-party nodes may appear with blank styling, and not as they appear in the Node-RED Editor.
Copy [{"id":"7f42f0eec47392cc","type":"sqlite","z":"285ecd284afc36bc","mydb":"95be1cdf72319762","sqlquery":"msg.topic","sql":"","name":"","x":1160,"y":240,"wires":[["1450fb96a13985ab"]]},{"id":"b9e641260d07ae6c","type":"inject","z":"285ecd284afc36bc","name":"select all data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"SELECT * FROM datalog","payload":"","payloadType":"date","x":950,"y":340,"wires":[["7f42f0eec47392cc"]]},{"id":"1450fb96a13985ab","type":"debug","z":"285ecd284afc36bc","name":"debug out","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1340,"y":240,"wires":[]},{"id":"1e74cba1232c842a","type":"rbe","z":"285ecd284afc36bc","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":false,"property":"payload","topi":"topic","x":470,"y":80,"wires":[["18c86194ca931450","673e308d2ff9b92d"]]},{"id":"022fabef50d9af16","type":"inject","z":"285ecd284afc36bc","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"item1\":10,\"item2\":20,\"item3\":30,\"item4\":40}","payloadType":"json","x":130,"y":80,"wires":[["9af65dff20cd00b5","8c18bbd3dafc019f","b62c936f28b52683","034ae14bea9fd568","0d895f52ec055f4d"]]},{"id":"18c86194ca931450","type":"debug","z":"285ecd284afc36bc","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":80,"wires":[]},{"id":"be519059c9295bab","type":"rbe","z":"285ecd284afc36bc","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":false,"property":"payload","topi":"topic","x":470,"y":120,"wires":[["941390fb7004e3fc","673e308d2ff9b92d"]]},{"id":"941390fb7004e3fc","type":"debug","z":"285ecd284afc36bc","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":120,"wires":[]},{"id":"9af65dff20cd00b5","type":"function","z":"285ecd284afc36bc","name":"get item1","func":"let data = msg.payload.item1 | 0\nmsg.topic = \"item1\"\nmsg.payload = data\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":80,"wires":[["1e74cba1232c842a"]]},{"id":"8c18bbd3dafc019f","type":"function","z":"285ecd284afc36bc","name":"get item2","func":"let data = msg.payload.item2 | 0\nmsg.topic = \"item2\"\nmsg.payload = data\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":120,"wires":[["be519059c9295bab"]]},{"id":"b0b0ac7479d7ee76","type":"rbe","z":"285ecd284afc36bc","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":false,"property":"payload","topi":"topic","x":470,"y":160,"wires":[["195eaaa6e15ac0c2","673e308d2ff9b92d"]]},{"id":"84d8edc4b53e6b4f","type":"rbe","z":"285ecd284afc36bc","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":false,"property":"payload","topi":"topic","x":470,"y":200,"wires":[["2d3b02bcdd5a4a09","673e308d2ff9b92d"]]},{"id":"b62c936f28b52683","type":"function","z":"285ecd284afc36bc","name":"get item3","func":"let data = msg.payload.item3 | 0\nmsg.topic = \"item3\"\nmsg.payload = data\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":160,"wires":[["b0b0ac7479d7ee76"]]},{"id":"034ae14bea9fd568","type":"function","z":"285ecd284afc36bc","name":"get item4","func":"let data = msg.payload.item4 | 0\nmsg.topic = \"item4\"\nmsg.payload = data\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":200,"wires":[["84d8edc4b53e6b4f"]]},{"id":"195eaaa6e15ac0c2","type":"debug","z":"285ecd284afc36bc","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":160,"wires":[]},{"id":"2d3b02bcdd5a4a09","type":"debug","z":"285ecd284afc36bc","name":"debug 4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":200,"wires":[]},{"id":"634d0d8a05fdef63","type":"sql-prepare","z":"285ecd284afc36bc","name":"","x":950,"y":240,"wires":[["7f42f0eec47392cc"]]},{"id":"673e308d2ff9b92d","type":"function","z":"285ecd284afc36bc","name":"mapping data to database","func":"let getmoment = new moment();\nlet datetime = getmoment.tz('Asia/Bangkok').format(\"YYYY-MM-DD HH:mm:ss\");\nlet name = msg.topic\nlet value = msg.payload | 0\n\nmsg.column_name = [\"datetime\", \"name\", \"value\"]\nmsg.column_sort = [datetime, name, value]\nmsg.database = \"datalog\"\nmsg.command = \"insert\"\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"moment","module":"moment"}],"x":710,"y":240,"wires":[["634d0d8a05fdef63"]]},{"id":"52e66c98ec7446f1","type":"inject","z":"285ecd284afc36bc","name":"delete all data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"DELETE FROM datalog;","payload":"","payloadType":"date","x":950,"y":300,"wires":[["7f42f0eec47392cc"]]},{"id":"3ad485d677f8b144","type":"comment","z":"285ecd284afc36bc","name":"CREATE TABLE","info":"CREATE TABLE datalog (\n id INTEGER PRIMARY KEY,\n datetime DATETIME,\n name TEXT,\n value REAL\n);","x":1160,"y":200,"wires":[]},{"id":"0d895f52ec055f4d","type":"debug","z":"285ecd284afc36bc","name":"debug 0","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":320,"y":240,"wires":[]},{"id":"95be1cdf72319762","type":"sqlitedb","db":"/data/datalog.db","mode":"RWC"}]