First time with node red, issue with chart node

Hi I am new here and work with node red the first time.
I am a programmer in vb.net. So some knowledge about programming is there.
I want to display some values with the chart node.
I imported a flow from the www to learn, but I get "no data available" with that.
It contains an inject node, which triggers a function node, that increases a simple value every 3 seconds. thes values i want to display with a bar chart. But the dashboard say "no data available".

code in the function node:

var i = 0;

i = i + 5;

msg.payload = {
"series": ["Wert"],
"data": [
[20 + i]
],
"labels": ["Zeit"]
};

return msg;

What I did wrong?

Wrap it in [ ]

var i = 0;

i = i + 5;

msg.payload = [{
"series": ["Wert"],
"data": [
[20 + i]
],
"labels": ["Zeit"]
}];

return msg;

Add an inject node to trigger the data

1 Like

thank you!

please what is wrong here: I want to output 3 values, but chart shows me the first only.

msg.payload = [
    { 
        topic: "Strompreis", 
        payload: 84.75, 
        timestamp: "Jetzt" 
    },
    { 
        topic: "w", 
        payload: 78.50, 
        timestamp: "Nächste Stunde" 
    },
    { 
        topic: "1", 
        payload: 79.00, 
        timestamp: "Ăśbermorgen" 
    }
];

node.warn(msg.payload); // Gibt das Array von Objekten aus

return msg;

Try...

const messages = [
    { 
        topic: "Strompreis", 
        payload: 84.75, 
        timestamp: "Jetzt" 
    },
    { 
        topic: "w", 
        payload: 78.50, 
        timestamp: "Nächste Stunde" 
    },
    { 
        topic: "1", 
        payload: 79.00, 
        timestamp: "Ăśbermorgen" 
    }
]
return messages

I think that should be

const messages = [
  [  
    { 
        topic: "Strompreis", 
        payload: 84.75, 
        timestamp: "Jetzt" 
    },
    { 
        topic: "w", 
        payload: 78.50, 
        timestamp: "Nächste Stunde" 
    },
    { 
        topic: "1", 
        payload: 79.00, 
        timestamp: "Ăśbermorgen" 
    }
  ]
]
return messages

Hi! Both versions work, many thanks.
Could anybody explain why I need to save these value in a CONST first and not directly into msg.payload?

Because they are completely different operations.

Setting msg.payload then returning the msg will simply return 1 msg with the payload set as an array.

Returning an array of messages will cause the function to output multiple messages.

This is easily verified and can be visually understood by putting a debug node on the output of the function. Your original version will send 1 message. Colins code will send 3 messages.

https://nodered.org/docs/user-guide/writing-functions#multiple-messages

1 Like

thank you very much.

I have an other question:

I am reading in a JSON from a power supplier from www with http request, it contains 24 prices and the corresponding time stamps (hour). I am reading in this JSON every 24h.

In my function node I output these 24 values in a chart and also in 24 checkboxes. I filter the lowest 4 prices and check the checkboxes accordingly. When the actual hour corresponds with the hour of one of the 4 prices it activates loading my house battery.
Additionally I could check/uncheck one or some of these 24 checkboxes manually as well.

So far so good.

The problem is, that the function node runs the code just once in 24h. But it shall run at least every hour. So I need a second inject node which is directly connected to the function node. But if I do so I get an error in a debug node, which says "invalid payload format". But why is it invalid suddenly, I just run the code more often!?

Any idea why?

Where is the debug node connected to? A node it is connected to is presumably generating the error.