Help with function node

I’m trying to simplify my lighting automations, and wanting to pull entity_id from a previous node, so I can more efficiently scale out my lighting flows.

In the node above, I’m seeing the entity_id being passed all the way through, but not sure how to capture it to use in the call service node.

The current function node is:

var state = msg.payload
if(state == 'on'){
    msg.payload = {
        service: 'turn_on'
    }
} else if(state == 'off'){
    msg.payload = {
        service: 'turn_off'
    }
}
return msg;

Any suggestions?

1 Like

Not sure I follow exacting what you’re trying to accomplish but if you just want to use the data from the current-state node with override topic checked.

1 Like

@Kermit, thank you, that is very helpful. I guess a better indication of what I’m trying to do is below:

Essentially, I have some Z-wave devices that don’t always behave as they should, so I like to have a check/balance on them to make sure they turn on/off as they should. I also want to eliminate the complexity of my flows, so was hoping to create a sub flow that provides the check without having to manually enter the entity id. So in the above example, I would like the current state node to pull the entity from the call service node that precedes it. Your example helped with part of that (eliminating the function node entirely), but a little unclear how to get the entity_id into the csn.

Thank you!

Hate to bump why own thread, but I’ve been trying to no avail to get this to work. Any suggestions?

Thank you!

well where is it getting {{ object }} from?

That was one of my attempts to get it working. I put {{object}} in the Current State node where I have {{payload.data.entitiy_id}} above.

So, instead of everything outputting to the same DEBUG, break it down piece by piece and have a DEBUG for each thing (give the debug a name so that you can follow it through the log)

What do you get from the service call?

What do you get from the ‘current state’?

@flamingm0e

I broke out the debugs, and the second one (attached to the current state) doesn’t produce anything, but the actual current state node throws the second error. I’ve tried pretty much every iteration of entity_id and nothing has worked. The included example was copied directly from the node path in the debug window.

I’ve never used the {{}} within the HA nodes in node-red, so I will have to play with mine and see if I can duplicate what you are trying to do.

You currently can’t use templates within the current-state node.

[{"id":"5797dc09.8564a4","type":"inject","z":"2441972b.173ec8","name":"","topic":"","payload":"on","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":112,"wires":[["6aa52273.5a220c"]]},{"id":"1eb0621c.7b4fce","type":"inject","z":"2441972b.173ec8","name":"","topic":"","payload":"off","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":146,"y":160,"wires":[["6aa52273.5a220c"]]},{"id":"6aa52273.5a220c","type":"api-call-service","z":"2441972b.173ec8","name":"","service_domain":"homeassistant","service":"turn_{{payload}}","data":"{\"entity_id\":\"light.some_light\"}","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":396,"y":112,"wires":[["457f06bb.c09738","4bf02ca.7240bd4"]]},{"id":"4bf02ca.7240bd4","type":"ha-wait-until","z":"2441972b.173ec8","name":"","outputs":2,"entityId":"light.some_light","property":"state","comparator":"is","value":"payload","valueType":"msg","timeout":"20","timeoutUnits":"seconds","entityLocation":"","entityLocationType":"none","checkCurrentState":true,"blockInputOverrides":true,"x":620,"y":160,"wires":[[],["6aa52273.5a220c"]]},{"id":"457f06bb.c09738","type":"debug","z":"2441972b.173ec8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":620,"y":112,"wires":[]}]
1 Like

@Kermit Thank you! Is there no way to pass the entity id to the wait until node?

Next release.

https://github.com/zachowj/node-red-contrib-home-assistant-websocket/commit/c4d308196f4f34d369b451b376a507b30508cc75

Cool. When will that happen? Also, is there a nice way to pass {“brightness_pct”:100} on the Service Call but only when it’s On, as that seems to trip up the turn_off if it’s present.

Maybe I’ll see about getting a release out this weekend.

const entities = global.get('homeassistant').homeAssistant.states;

if(entities['light.somelight'].state === "on") {
    msg.payload = { "data": {"brightness_pct": 100} };
}

return msg;
1 Like

where would the above snippet of code go?

function node

And again, is it possible to pass the entity_id as a variable? I’m trying to simplify the process of adding multiple lights, so hoping to not have to duplicate the entity_id in multiple nodes.
Something like {{payload}} in place like below?:

const entities = global.get('homeassistant').homeAssistant.states;

if(entities['{{payload}}'].state === "on") {
    msg.payload = { "data": {"brightness_pct": 100} };
}

return msg;

Thank you!

const entities = global.get('homeassistant').homeAssistant.states;
const entity_id = msg.payload;

if(entities[entity_id].state === "on") {
    msg.payload = { "data": {"brightness_pct": 100} };
}

return msg;

I hate being so bad at all this, but still trying to learn. Any possibility of seeing a complete example? Thank you so much!

function : (error)

"TypeError: Cannot read property 'state' of undefined"