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;
My guess is that your missing ";"s are the issue, and yes, only inside the loop.
JavaScript statements (usually but not necessarily one per line) should be terminated with ‘;’. In many of your lines, where the execution parse engine finds a keyword (var, let, while) it can work out that one statement has ended and a new one begun. However, (specifically) within your while loop there are no keywords so the missing ‘;’ means that node.warn… concatenates with obj ={…} and arrList.push(obj)i++, making one big statement, and probably one that does not do what you want it to!
I don’t have an alexa, so I tried this out on my window ‘covers’, just to prove that it can work…
Note that to work your comma separated list of alexi must not have a space after the comma. On split, this generates names from the second item with a leading space which then fails. I also moved the ‘cover.’ (‘media.’ in your case) from the list and added it to the object path code. I am also running this on my Raspberry Pi Node-RED, so my websocket connection back to my HA is on a different machine, hence my 'homeAssistant(Pi), so you are recommend not to copy my code verbatim!
I guess this is the challenge of mentally switching from yaml to js to JSONata to Python to jinja and back on the same project
I think I have narrowed down the issue. To the var list. For some reason list doesn’t seem to be processing correctly. If you look at list its not split out but when I look at the testArray I put in global just to see what it’s doing, it’s split out. I’ve added all the recommendations above but unfortunately still no joy. Below is what I have now and the current output.
11/20/2022, 12:44:02 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:44:02 PMnode: hold orig vol
function : (warn)
"Length: 2"
11/20/2022, 12:44:02 PMnode: hold orig vol
function : (warn)
"MSG: media_player.office_echo,media_player.laundry_room"
11/20/2022, 12:44:02 PMnode: hold orig vol
function : (warn)
"List: media_player.office_echo,media_player.laundry_room"
11/20/2022, 12:44:02 PMnode: hold orig vol
function : (warn)
"testList: media_player.office_echo,media_player.laundry_room"
11/20/2022, 12:44:02 PMnode: hold orig vol
function : (warn)
"i:0"
11/20/2022, 12:44:02 PMnode: hold orig vol
function : (warn)
" Current:media_player.office_echo Vol:0.3"
11/20/2022, 12:44:02 PMnode: hold orig vol
function : (warn)
"OBJ:[object Object]"
11/20/2022, 12:44:02 PMnode: hold orig vol
function : (warn)
"i:1"
11/20/2022, 12:44:02 PMnode: hold orig vol
function : (error)
"TypeError: Cannot read properties of undefined (reading 'attributes')"
// Get states and put them into a global array
var states = global.get('homeassistant.homeAssistant.states');
// Build local
var alexa = msg.stack.alexa
var arrList = []
var obj = {}
var list = []
let i = 0
list = alexa.split(',')
global.set('testList',list)
node.warn(states)
node.warn('Length: '+list.length)
node.warn('MSG: '+msg.stack.alexa)
node.warn('List: '+list)
node.warn('testList: '+global.get('testList'))
while(list[i]) {
node.warn('i:' + i);
node.warn(' Current:' + list[i] + ' Vol:' + states[list[i]].attributes.volume_level);
obj = {
"alexa" : list[i],
"vol" : states[list[i]].attributes.volume_level
};
node.warn('OBJ:'+obj);
arrList.push(obj);
i++;
}
global.set('arrTest',arrList)
return msg;
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;