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 (global.today 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 global.today (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 global.today.

Thanks for the quick reply!

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

The array:
["9.638","8.927","11.47","19.44","19.22","18.93","19.67","19.11","18.53","19.92","19.93","18.86","16.56","17.19","21.32","20.01","22.43","22.25","22.49","22.61","22.62","22.33","21.89","22.17","21.91","18.94","22.49","0.000","0.000","0.000"]

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

1 Like

Thanks! Works like a charm.

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