Does sending message from function node turn on the light?

I have the following code in a function node. That’s the entire code in that flow and function. Assuming that the entity ID is correct, shouldn’t it turn on that light? Right now it is not.

Thanks.

node.send({
    payload: {
        "entity_id": "light.kitchen_under_cabinet_light",
        "service": "turn_on",
        "service_data": { "brightness_pct": 80, "color_temp": 350 }
    }
});
1 Like

No, you use a call service node
The output from your function node fits the required data in a service node.

And node.send is just the same as return msg here.
Send.node does not stop execution in the node unlike return msg does, but there is nothing more in the node anyway.

1 Like

What would be simplest way to call whatever API call service node uses so that I can continue with other code in that function node after turning on the light?

Thanks.

I have no idea how to make a service call from a function node.

No worries, hopefully someone who knows will respond and help us out :smile:

Cheers!

Huh?

What is it that you want to do?
You can connect the output of the Call Service node to any other node you want.

I am flow challenged and prefer writing code over connecting boxes with lines. :slight_smile:

Yes, that’s how my current flow works. But flows are too cumbersome for the kind of automation I want to build. I just find code more convenient.

Thanks.

I find flows far more simple for some things. But you still haven’t said what you want to do.

I do a lot of calculation and preparing in function nodes too, but the service calls are still placed in the nodes for that.

You can generally do all the calculations in one function node with the help of the global variable for HA values.

I am not even sure that there is a function available in a function node to make a service call.
A service call is a HA specific feature and Node Red is not made specifically for HA, but rather as a general automation tool.

You could use an API call to HA.
But if you don’t like the graphics of Node red then use HA automations instead.

Either way the question is far too vague and generally that we can only speculate or guess.
If you make your question more precise then we might be able to help you.

That would be great. Please point me to some documentation or example of an API call to HA from a function node. That is the help I am looking for.

Here’s an simple example of what I would like to do:

  • if a person arrives home
    — turn on their room lights using custom brightness and color temperature curve that they can configure
    —— while the light stays on update the light every 5 mins based on same curve
    —— but if the lights get turned off manually keep them off till turned on manually
    — if the room is hot turn on the AC
    —— turn off the AC when room temperature is at a configured level
    — if the room is cold turn on the heater
    —— turn off the heater when room temperature is at a configured level
    — but if there is no motion in room in some configured minutes turn off everything and suspend automation for the room
    — if there is motion in the room restart all automation for the room
    — if the person leaves turn off devices that are still on
    —— if HomePods in room are playing pause them

There is lot more to do, but on a mobile so taking too long. My groovy code for this is on GitHub. I don’t necessarily want to replicate all of it but some parts of it:

Thanks again.

I strongly advice against such a setup.
Having all that in one function node (if it’s even possible) will be a mess and you would need to loop indefinitely inside this loop.
There is really nothing good.

You are not using any of the benefits of HA when you set it up like that.
Make sure your automations don’t run all the time, instead make them run when they should run.
The way I understand your code it’s almost like “are we there yet? are we there yet?”

Nah. SmartThings groovy code was also trigger based and single threaded like HA so a lot of that is achieved through maintaining various state and checking combination of conditions on trigger. Nothing in the code that I have, executes for more than a few ms at a time and is always triggered by device or time conditions.

Please point me to some documentation or example of an API call to HA from a function node. That is the help I am looking for.

Thanks.

I’m not your servant.
Feel free to google home assistant API yourself and finding a way to do this from function node.
I doubt you can do it from a function node but you can send the message to a http node.

Well I was only requesting for pointers because from this post earlier in this thread I got the impression that it could be done.

Back to searching for it.

Thanks.

You can do a API call, bur if that is possible from a function node, don’t know.
Doubt it. That is not the intended way to use the function node.

I think the OP is really trying to do this the hard way.

Why do you want to control an entity from a function node when there’s a perfectly good call.service node available?

That sounds about right. :slight_smile:

My current thinking is, because I would then be able to control a variety of devices from the function node without having to use msg.payload from the function node with a switch node to control which all devices need to be set to various states.

Thanks.

You don’t need to do that.
You can just send a message with the command to the call service and it will be dynamic.

Please share an example. ( not an order, just a request. :wink: )

Thanks.