Pass entity name to action node at runtime

Hi,
Is there any way to pass at runtime the entity name to an action node?. I am trying with {{msg.payload}} but it gives error.

Regards

Node-RED does not use mustache templates by default, and only in some nodes where specifically mentioned in the documentation. Instead Node-RED uses JSONata expressions, which are set using the J: (expression) option where it is allowed.

When using JSONata, the expression evaluates input fields to their value, hence

payload

will return the value in payload. No need for msg. at the front, and not a single { in sight.

In the editor box UI settings for the Action node, only the Data optional field can use JSONata directly. This permits dynamic setting of optional parameters only.

The Action node does accept all settings from message input, as long as Block input overrides is unchecked, and all such settings must arrive as input in an object in msg.payload. You can use a Change node or similar to set msg.payload to an object with setting values.

The node documentation has the following example for msg.payload:

{
    "action": "homeassistant.turn_on",
    "target": {
        "floor_id": ["first_floor"],
        "area_id": ["kitchen"],
        "device_id": ["8932894082930482903"],
        "entity_id": ["light.kitchen", "switch.garage_light"],
        "label_id": ["outdoor_lights"]
    }
    "data": {
        "brightness_pct": 50
    }
}

The various settings are all optional, but if provided each field value will set (if UI is empty) / override (if UI contains a value) matching settings in the UI.

The action field is now a combined platform.action string
The target field is now an object of optional floor, area, device, entity, label that will be resolved to a list of associated entities. This is additive, so adding both entities and devices for example could end up with more entities than you expect.

Any additional (optional) parameters must be set in a data object.

Hence setting msg.payload (in eg a Change node) to

{"target": {"entity_id: "light.kitchen"}}

should set the target entity field for you. In this case, since everything is a literal, you can simply use the { } JSON option, however use of the J: JSONata option works just as well, and will then allow the use of expressions to build a more complicated input object.

Since every HA Action works slightly differently and may have an entity or target entity as well as optional (data) parameters, it is always worth testing any action in Home Assistant first, and using the YAML code option to see exactly what is required before transferring to Node-RED.

All nodes are also documented, and informative node documentation can be easily seen in the documentation tab section in the Node-RED debug window.

Hello, thank you for your reply. I am trying to give value to an input_number on the fly and I am testing by putting the information in the payload through a node change as you indicated in the previous message. then I send the payload to an action node.

My code is this;

{
    "action": "input_number.set_value",
    "target": {
        "entity_id": [
            "input_number.slider_tiempo_dummy"
        ],
        "data": {
            "value": 20
        }
    }
}

but it fails me with the error

"ValidationError: "action" is not allowed to be empty"

I don’t know if what I am trying to do is possible or not. Any suggestions?

Regards

Inside the action node, uncheck the block input override check box.

It works, but now I like to change the entity_id as a string to a msg.payload which is where I have the name of the entity coming from the previous node.
I have tried to put directly msg.payload or {{msg.payload}} but it does not work. Is there any way to substitute the value of msg.payload in the json?
Regards

When using a mustache template {{ }} The data section needs to be set to JSON and remove msg. So {{payload}}.

image

Edit: It looks like you are using this in the entity text box. There is no JSON setting there. Still use {{payload}} instead of {{msg.payload}}

Hi,

I managed to set input_number.set_value with this

imagen

but now I’m trying to use the decrement but I can’t get it to work. As I don’t have anything in data it doesn’t work. So I think it is not necessary to pass anything in data to the decrement.

imagen

any suggestions?

Regards

Going back to the basics…

The (hopefully correct) picture guide to passing variables to the Action node

The Action Node requires settings for

  • an Action
  • a Target (list of) entity(ies) [composed from entities and entities belonging to areas, devices, floors, and/or labels]
  • optionally extra parameters in a Data object.

There are four ways to provide these necessary settings:

  • a string literal (a fixed value) entered into the UI field
  • using Mustache templates in the UI field
  • using JSONata expression in (only) the Data object UI field
  • using the Input Message Override method

Using a string literal entered into the UI field

Just type in the Action, add a Target entity (and/or device etc) and optionally a Data object. Job done.

Ensure ‘Block input overrides’ is checked to prevent the input message from making changes to your settings!

Using Mustache Templates to pass variable settings to the Action Node

Assume we have an input message with field(s) set to the values we require

We can use Mustache templates in the UI fields

How does this work?

The node passes the UI string to Home Assistant’s template engine, which replaces the string {{payload}} with the value in the input msg.payload, returning a string for the UI field.

Passing extra optional parameters in the Data Object field

All HA Actions require an action and a target.
Some actions require further information, eg input_number.set_value action requires the value to use.

These extra parameters are passed using a Data JSON object. The extra parameters are key/value pairs as required by the Home Assistant action.

NOTE: If HA expects a parameter and it is not provided, HA will complain with an error. If the Data object contains parameters that HA was not expecting, or have inccorect values for that parameter, HA will complain with an error. Use HA Developer Tools > Actions to check the Action and the parameters required. There is a parameter list, documentation, and examples included for each action!

Data Object field using a literal

Just enter a valid JSON object as required.

NOTE: the Data object field has options to use either { } JSON or J: expression [the default option]

  • { } JSON expects just a JSON object (and yes you can use Mustache templates here)
  • J: expression expects a JSONata expression (and no you can’t use Mustache templates here)

For simple literals, there is no difference between these options since JSONata expression evaluates literals as themselves. You just can’t use templates in JSONata.

Data Object field using Mustache Templates

This is possible, but not advised. Since templates use {{ }} as delimiters, and since templates provide string substitution, templating does not always work when creating a JSON object.

Starting with an input message with a field holding the value we require

We can use Mustache templates within the JSON object

NOTES:
The Data UI field option must be { } for JSON for this to work (NOT J:)
It is advised to use the alternative template tags <% %> to prevent issues with {{ }}
This may not work correctly / at all which is why the advice is to use JSONata instead!

Data object fields using JSONata

The Data object field can accept JSONata expressions, when the option is set to J:

Note that in a JSONata expression, passing a field value is done by just referencing the name. The Data Object UI field has an editor ... for either the { } JSON or for the J: JSONata Expression options.

Using the Input Message Override Option

The above methods have used literals, Mustache templates, or JSONata (Data object only) to enter the UI settings, all by working in the Action node itself. An alternative approach is to pass some/all of the settings using the input message.

This works by passing an object in msg.payload, with key/value pairs that match the UI fields.

NOTES:
For this to work, the Block Input Overrides option must be unchecked
The input must be in msg.payload, and must be an object with key/value pair(s)
If a key matches a UI field, the value will replace anything in the UI field, otherwise the UI field entry remains as entered
The node UI fields do not have to be completed if there is a matching input value

Where does the msg.payload come from?

For simple cases, a previous node can be used to set msg.payload = {"Action": "homeassistant.turn_off"}

The ‘full’ input override object can be quite complicated:

{
    "action": "homeassistant.turn_on",
    "target": {
        "floor_id": ["first_floor"],
        "area_id": ["kitchen"],
        "device_id": ["8932894082930482903"],
        "entity_id": ["light.kitchen", "switch.garage_light"],
        "label_id": ["outdoor_lights"]
    },
    "data": {
        "brightness_pct": 50
    }
}

For more complex settings, a Change node can be used to construct the JSON object.

Change nodes can use JSONata, so it is possible to accept input variables into the Change node, and to use JSONata to create the msg.payload input to the Action node.

Step 1: have the settings available in fields in the message (example from an Inject node)

Step 2: use a Change node and JSONata (J:) to construct the JSON object for msg.payload

The JSONata expression… here all the Action node settings are being provided, and are all being generated from message fields.

Step 3: pass the msg.payload as input to the Action node, with the Block input overrides setting unchecked. The Action node now requires no input settings.

The mix-and-match approach

It is possible to use a combination of all these approaches. The Action could be a fixed literal, the Target entity set with Mustache templates from eg msg.topic, and the optional Data object set from an input msg.payload built using a Change node with JSONata.

{
    "action": "input_number.decrement",
    "target": {
        "entity_id": topic
    }
}

Assumes:

You are using the Input Overrides method, passing this object to the Action node in msg.payload (with Block Input Overrides unchecked)

You are using a Change node, and are using the JSONata option J:

You have the entity id already held in msg.topic.

There are other ways of doing this and it is a matter of personal choice…