#Is it possible to run actions directly inside a function?

1 messages · Page 1 of 1 (latest)

waxen trout
#

I'm still in the process of learning how to use node-red but I've got a decent grasp on a workflow that works well-ish for me and all the scripting I want to do, albeit it's a bit clunky. It primarily consists of 3 parts: an event is the input, a function does all the scripting, filtering, reformatting, and then an action or some other node is the output.

The problem I'm running into is that the communication from the function to the output node is pretty clunky because for every potential output that any function may have, it needs to have its own entire output on the function. This just adds more annoying boilerplate to deal with, and I'd really prefer if I could just call those actions directly inside the function. Is such a thing possible? Is there some other workflow that might be a bit less cumbersome? Overall I'd really prefer to just use proper text-based scripting as much as possible.

jovial obsidian
#

AFAIK it's not possible in NodeRED. There's AppDaemon, the add-on that brings pure Python to HA - it is possible there i guess.

woeful haven
#

Are you married to 100% NodeRED or are you OK with a hybrid solution? Because you can create automations to run scripts to either perform all your actions or to return a value so you can pipe that into something else (like an action or script). The scripts can be either the built in YAML scripts, which are quite powerful, the built in pyscript if you need to get more advanced, python script/appdaemon from HACS if you want to really go crazy

arctic mountain
# waxen trout I'm still in the process of learning how to use node-red but I've got a decent g...

it needs to have its own entire output on the function. This just adds more annoying boilerplate to deal with

What additional boilerplate are you referring to? You can connect an action node directly to a function node and either return an object with the action you want to perform, or use the asynchronous node.send to dispatch multiple actions from within the same function node.

For example, you could set msg.payload like this:

{ "action": "light.turn_on", "data": { "brightness_pct": 60 } }

This approach reduces complexity, and you can handle multiple actions or conditions without extra boilerplate.

If you really want to send API calls from a function node, you can use any request library like axios or fetch. This will allow you to make HTTP requests directly within the function node for more complex integrations.

waxen trout
#

Hm, that's a step in the right direction but still not as much as I'd hope. Is there perhaps any way to set the target entities by code? That could help

#

And is there any documentation for raw http? What would that look like?

arctic mountain