Can't add blank object to array

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
	}
}

There are many ways to Rome, i would probably use something like this:

const 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" }] }
];

const input = msg.payload
const result = input.target.entity_id.forEach((e,i) =>{
    let c = lookup.filter(x => x.entity_id == e)
    if(c.length>0){
        input.target.entity_id[i] = { entity_id: e, conditions:c.map(m => m.conditions ) }
    }
})

return msg;

result:

{
    "domain": "homeassistant",
    "service": "turn_on",
    "target": {
        "entity_id": [{
            "entity_id": "light.office_lights",
            "conditions": [
                [{
                    "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
    }
}

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