Hello,
The following code works fine in online JS compilers but not in the node-red function node. What am I missing?
I inject the following msg.payload:
{
"domain": "homeassistant",
"service": "turn_on",
"target": {
"entity_id": [
"light.office_lights",
"light.bedroom_lights"
]
},
"data": {
"brightness_pct": 50
}
}
Into this function node:
var lookup = [
{ "entity_id": "light.office_lights", "conditions": [{ "entity_id": "input_boolean.bedroom_occupied", "allowed_state": "off" }, { "entity_id": "binary_sensor.peloton_workout", "allowed_state": "off" }] }
];
for (var y in msg.payload.target.entity_id) {
var lookupMatch = lookup.find(el => msg.payload.target.entity_id[y] === el.entity_id);
msg.payload.target.entity_id[y] = [msg.payload.target.entity_id[y]];
msg.payload.target.entity_id[y].condition = [];
if(lookupMatch) {
for (var i in lookupMatch.conditions) {
msg.payload.target.entity_id[y].condition.push(lookupMatch.conditions[i]);
}
}
node.warn(msg.payload.target.entity_id[y]);
}
return msg;
I get this back, which is missing the matched condition objects:
{
"domain": "homeassistant",
"service": "turn_on",
"target": {
"entity_id": [
[
"light.office_lights"
],
[
"light.bedroom_lights"
]
]
},
"data": {
"brightness_pct": 50
}
}
And I am actually expecting to see this:
{
"domain": "homeassistant",
"service": "turn_on",
"target": {
"entity_id": [
[
"light.office_lights",
"condition": [
{ "entity_id": "input_boolean.bedroom_occupied", "allowed_state": "off" },
{ "entity_id": "binary_sensor.peloton_workout", "allowed_state": "off" }
]
],
[
"light.bedroom_lights"
]
]
},
"data": {
"brightness_pct": 50
}
}