Sending all payload buffer items as different notifications

I try to send battery level notifications per payload buffer.
I did write the next function node.
It is only sending the last payload buffer 4 times
How to send the ohters before the last buffer?

Please some help to fix this code?

var i;
for (i = 0; i < msg.payload.length; i++) {
var nivo = msg.payload[i].attributes.battery_level;
var id = msg.payload[i].entity_id;
//msg.payload = {};
msg.payload.chatId = mychatid;
msg.payload.type = "message";
msg.payload.content = id + ' = '+ nivo + ' %';
node.send(msg, false);
//return msg;
  1. mychatid is never declared
  2. msg.payload.type = "message"; you’re trying to assign a value to msg.payload as if it was an object but it is already an array.
msg.payload.forEach((entity) => {
    const nivo = entity.attributes.battery_level;
    const id = entity.entity_id;
    const data = {
        "payload": {
            // chatId: mychatid,
            type: "message",
            content: `${id} = ${nivo} %`

You can also accomplish the same task not using a function node.


[{"id":"59a9621d.f0c55c","type":"inject","z":"ffbd7f06.4a014","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"entity_id\":\"one\",\"attributes\":{\"battery_level\":50}},{\"entity_id\":\"two\",\"attributes\":{\"battery_level\":75}},{\"entity_id\":\"three\",\"attributes\":{\"battery_level\":100}}]","payloadType":"json","x":290,"y":1024,"wires":[["16318761.8172c9"]]},{"id":"69f19c55.0cdf34","type":"debug","z":"ffbd7f06.4a014","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":1024,"wires":[]},{"id":"16318761.8172c9","type":"split","z":"ffbd7f06.4a014","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":418,"y":1024,"wires":[["1751ba9e.088bd5"]]},{"id":"1751ba9e.088bd5","type":"template","z":"ffbd7f06.4a014","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{\n    \"type\": \"message\",\n    \"content\": \"{{payload.entity_id}} = {{payload.attributes.battery_level}} %\"\n}","output":"json","x":556,"y":1024,"wires":[["69f19c55.0cdf34"]]}]

Thank you very much.
I will try this both options

EDIT: I did write above “mychatid” because not to show everybody mychatid. In the real code i did use the real chatid

I just tried both options.
Both are working great!!!

Thanks again!
I’m able to understand the code after seeing it, but I’m not able to create it myself from the beginning :relaxed:

I was thinking about this.
Is there somewhere a good tutorial to learn all this kind off rules and showing examples?