I want to summarise data of a key pair, for grouping based on TWO other json key pairs. And, I want to pass through variables for use further down in the flow.
This is the json array & objects I receive in msg.payload:
[
{"id":"Irr-1","name":"Irrigation","date":"2018-12-24","duration_s":20,"calctype":"Duration_1vs0"},
{"id":"Irr-1","name":"Irrigation","date":"2018-12-24","duration_s":25,"calctype":"Duration_1vs0"},
{"id":"Irr-1","name":"Irrigation","date":"2018-12-25","duration_s":25,"calctype":"Duration_1vs0"},
{"id":"Irr-2","name":"Irrigation","date":"2018-12-24","duration_s":30,"calctype":"Duration_1vs0"},
{"id":"Irr-2","name":"Irrigation","date":"2018-12-25","duration_s":30,"calctype":"Duration_1vs0"},
{"id":"Irr-2","name":"Irrigation","date":"2018-12-25","duration_s":35,"calctype":"Duration_1vs0"},
]
In order to group the incoming msg.payload, I am using a Node-RED change node with the following JSONata expression on the above message, to group based on the "id" key pair:
payload{
id : $sum(duration_s)
}
~> $each(function( $v, $k) {
{ "id": $k, "duration_s": $formatNumber($v, '#,###')}
})
Which works, and gives the output to msg.payload:
[
{"id":"Irr-1","duration_s":"70"},
{"id":"Irr-2","duration_s":"95"},
]
But, what I need, is the following output:
[
{"name":"Irrigation","id":"Irr-1",date:"2018-12-24","duration_s":"45"},
{"name":"Irrigation","id":"Irr-1",date:"2018-12-25","duration_s":"25"},
{"name":"Irrigation","id":"Irr-1",date:"2018-12-24","duration_s":"30"},
{"name":"Irrigation","id":"Irr-2",date:"2018-12-25","duration_s":"65"}
]
I do not know how to change the each
function for it to group by date as well, nor how to bring the "name" field through too. Any ideas please?
Here is the Node-RED export for this:
[{"id":"3946568c.64ba1a","type":"function","z":"a57275d4.b9ad68","name":"","func":"msg.payload = [\n {\"id\":\"Irr-1\",\"name\":\"Irrigation\",\"date\":\"2018-12-24\",\"duration_s\":20,\"calctype\":\"Duration_1vs0\"},\n {\"id\":\"Irr-1\",\"name\":\"Irrigation\",\"date\":\"2018-12-24\",\"duration_s\":25,\"calctype\":\"Duration_1vs0\"},\n {\"id\":\"Irr-1\",\"name\":\"Irrigation\",\"date\":\"2018-12-25\",\"duration_s\":25,\"calctype\":\"Duration_1vs0\"},\n {\"id\":\"Irr-2\",\"name\":\"Irrigation\",\"date\":\"2018-12-24\",\"duration_s\":30,\"calctype\":\"Duration_1vs0\"},\n {\"id\":\"Irr-2\",\"name\":\"Irrigation\",\"date\":\"2018-12-25\",\"duration_s\":30,\"calctype\":\"Duration_1vs0\"},\n {\"id\":\"Irr-2\",\"name\":\"Irrigation\",\"date\":\"2018-12-25\",\"duration_s\":35,\"calctype\":\"Duration_1vs0\"},\n];\nreturn msg;","outputs":1,"noerr":0,"x":240,"y":1460,"wires":[["113a7a40.16a186"]]},{"id":"b344738b.b3bc98","type":"inject","z":"a57275d4.b9ad68","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":1460,"wires":[["3946568c.64ba1a"]]},{"id":"113a7a40.16a186","type":"change","z":"a57275d4.b9ad68","name":"group","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload{\t id : $sum(duration_s)\t } \t~> $each(function( $v, $k) {\t { \"id\": $k, \"duration_s\": $formatNumber($v, '#,###')} \t})\t\t\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":1460,"wires":[["825b614c.c856"]]},{"id":"825b614c.c856","type":"debug","z":"a57275d4.b9ad68","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":590,"y":1460,"wires":[]}]