[Solved] Error Undefined

I am banging my head against a wall at this point. I for the life of me cannot figure out why I am getting the following error. I’ve tried a for loop, a while loop, same result. I break it out and it’s an error inside the loop but individually each command is working so it has something to do with the loop I just don’t know what. Any help would be greatly appreciated.

Input msg in msg.stack.alexa is a comma separated list of alexas.

11/19/2022, 2:04:38 PMnode: hold orig vol
function : (warn)
"Length:2"
11/19/2022, 2:04:38 PMnode: hold orig vol
function : (warn)
"i:0 Current:media_player.office_echo Vol:0.3"
11/19/2022, 2:04:38 PMnode: hold orig vol
function : (error)
"TypeError: Cannot read properties of undefined (reading 'attributes')"

Here is my code in the function node.

// Get states and put them into a global array
var states = global.get('homeassistant.homeAssistant.states');

// Build local
var alexa = msg.stack.alexa
var obj = {"alexa":"","vol":""}
var list = alexa.split(',')
var arrList = []
let i = 0

global.set('testList',list)
node.warn('Length:'+list.length)

while(list[i]) {
    node.warn('i:' + i + ' Current:' + list[i] + ' Vol:' + states[list[i]].attributes.volume_level)

    obj = {
        "alexa" : list[i],
        "vol"   : states[list[i]].attributes.volume_level
    }

    arrList.push(obj)
    i++
}

global.set('arrTest',arrList)

return msg;

Split the last node.warn() into two statements, the first that outputs i and the second outputting the rest.

And add node.warn for both list and states

Appreciate the help tracking this down.

11/20/2022, 12:22:58 PMnode: hold orig vol
function : (warn)
{ person.ross: object, person.vianna: object, person.nico: object, person.tracy: object, binary_sensor.remote_ui: object … }
11/20/2022, 12:22:58 PMnode: hold orig vol
function : (warn)
"Length: 2"
11/20/2022, 12:22:58 PMnode: hold orig vol
function : (warn)
"MSG: media_player.office_echo,media_player.laundry_room"
11/20/2022, 12:22:58 PMnode: hold orig vol
function : (warn)
"List: media_player.office_echo,media_player.laundry_room"
11/20/2022, 12:22:58 PMnode: hold orig vol
function : (warn)
"i:0"
11/20/2022, 12:22:58 PMnode: hold orig vol
function : (warn)
" Current:media_player.office_echo Vol:0.3"
11/20/2022, 12:22:58 PMnode: hold orig vol
function : (warn)
object
alexa: "media_player.office_echo"
vol: 0.3
11/20/2022, 12:22:58 PMnode: hold orig vol
function : (warn)
"i:1"
11/20/2022, 12:22:58 PMnode: hold orig vol
function : (error)
"TypeError: Cannot read properties of undefined (reading 'attributes')"

The error is it attempting to determine states[list[i]].attributes.volume_level in the warn statement. states[list[i]] does not exist for i=1.

Just caught that through your suggestion! Thank you. I have narrowed it down to the list being an issue for some reason. If you look at the list it's not broken into an array like it should be but when I look at the context data in the testArray it is so idk whats going on there but that's I believe where the trouble is.

I am an absolute bone head. The problem was never in the function. The problem was in the input coming in and I was trying to look up the attributes of a device that didn't exist. Thank you all for the help! Below is working code with all the debug removed!

// Get states and put them into a global array
var states = global.get('homeassistant.homeAssistant.states');

// Build local
let alexa = msg.stack.alexa;

let arrList = [];
let obj = { "alexa": "", "vol": "" };
let list = alexa.split(',');
let i = 0;

while(list[i]) {
    obj = {
        "alexa" : list[i],
        "vol"   : states[list[i]].attributes.volume_level
    };

    arrList.push(obj);
    i++;
}

flow.set('arrVol',arrList);

return msg;
1 Like

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