Selecting specific value in an array using var

A bit of a beginners question but I am new to JS and Node-RED.

I am reading values directly from my solar inverter web interface and am struggling with the kWh value. I have the data from the inverter in an array that lists the values in the order of days of the month. This is what I put in a function block ( contains the day of the month):

I want to select the correct value using var today: payload: msg.payload[today]
That does not work. If I test by sticking a number in, [27] for example, it does work.
Is there a solution for this?

Impossible to tell unless you show us what is in (my guess is it's empty or not between 0 & 29) also, expand the debug payload so we can see what's in it.

Like I said above

Try putting node.warn("today = " + today) below var today then look in the debug sidebar - tell us what value you get

Regarding arrays...
you should always check that the index (today) is actually a number and that is >= 0 and < the array length (msg.payload.length) BEFORE trying to access an element of an array.

Ps, look at the side bar "context" to see what is in

Thanks for the quick reply!

"today" looks like a number in the context screen, but not in the node.warn line

The array:

To access today you need to use global.get("today") not

1 Like

Thanks! Works like a charm.

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