node-red-contrib-home-assistant-websocketnode-red-contrib-home-assistant-websocket
Guides
Nodes
FAQ
Cookbook
Scrubber
Discussions
Discord
Github
Guides
Nodes
FAQ
Cookbook
Scrubber
Discussions
Discord
Github
  • Guides

    • Getting Started
    • First Automation
    • Action Node Tips and Tricks
    • Conditionals
    • Mustache Templates
    • JSONata

      • JSONata
      • JSONata primer
    • Diagnostics Information
  • Custom Integration

    • Getting Started
    • Exposed Nodes
  • Contribute to

    • Development
    • Documentation

Action Node Tips and Tricks

Home Assistant Domain

The homeassistant domain is versatile, allowing you to control multiple entities across different domains with various services.

For example, you can use the homeassistant domain to turn off lights, switches, and any other devices in the laundry room with a single action call. This approach reduces the need for multiple action nodes.

Screenshot of an action node using the Home Assistant domain

Using Mustache Templates

Mustache templates can be applied in the domain, service, and entity ID fields, which is especially useful for setting the service based on msg.payload or other message properties.

For instance, you can use eztimer to set the output of a node to on or off, and then use that output in the action node to determine the service to use.

Screenshot of flow

Screenshot of the action node

[{"id":"522c54b8de855062","type":"api-call-service","z":"04f15f38f9038e78","name":"","server":"","version":6,"debugenabled":false,"action":"homeassistant.turn_{{payload}}","floorId":[],"areaId":["75a88dfa58844f8aad752dde5bc51fc7"],"deviceId":[],"entityId":["light.front_porch","light.garage","light.kitchen"],"labelId":[],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","domain":"homeassistant","service":"turn_{{payload}}","x":670,"y":288,"wires":[[]]},{"id":"cca91c6fe47d7fa0","type":"eztimer","z":"04f15f38f9038e78","name":"","debug":false,"autoname":"sunset - dawn","tag":"eztimer","suspended":false,"sendEventsOnSuspend":false,"latLongSource":"manual","latLongHaZone":"","lat":"","lon":"","timerType":"1","startupMessage":true,"ontype":"1","ontimesun":"sunset","ontimetod":"17:00","onpropertytype":"msg","onproperty":"payload","onvaluetype":"str","onvalue":"on","onoffset":0,"onrandomoffset":0,"onsuppressrepeats":false,"offtype":"1","offtimesun":"dawn","offtimetod":"dusk","offduration":"00:01:00","offpropertytype":"msg","offproperty":"payload","offvaluetype":"str","offvalue":"off","offoffset":0,"offrandomoffset":0,"offsuppressrepeats":false,"resend":false,"resendInterval":"0s","mon":true,"tue":true,"wed":true,"thu":true,"fri":true,"sat":true,"sun":true,"x":400,"y":288,"wires":[["522c54b8de855062"]]}]

Mustache templates are also supported in the data field when set to JSON format.

Related Resources:

  • Mustache Templates

Targets

Target fields allow you to specify areas, devices, and/or entities, either individually or in combination. Note that not all services accept areas and devices.

Mustache templates and environment variables can be used within each target list.

Data Field

Tips

For the data field, it’s recommended to use the JSONata expression (J: Expression), as it offers several benefits over JSON:

  • Handles quotes based on the type of variable
  • Allows the insertion of complex variables such as arrays and objects
  • Supports mathematical operations

Inserting a Message Property into a String

Example: Sending a TTS Message When a Door is Opened

Screenshot of flow

Screenshot of the action node

{ "message": "The " & data.attributes.friendly_name & " has been opened.", "entity_id": "all" }
[{"id":"cd1f9610a73addbc","type":"server-state-changed","z":"c89d915bdff0f798","name":"","server":"","version":6,"outputs":2,"exposeAsEntityConfig":"","entities":{"entity":[],"substring":[],"regex":["sensor\\..+_door$"]},"outputInitially":false,"stateType":"str","ifState":"open","ifStateType":"str","ifStateOperator":"is","outputOnlyOnStateChange":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":446,"y":976,"wires":[["4e016826f6497b79"],[]]},{"id":"4e016826f6497b79","type":"api-call-service","z":"c89d915bdff0f798","name":"send tts","server":"","version":6,"debugenabled":false,"action":"tts.google_translate_say","floorId":[],"areaId":[],"deviceId":[],"entityId":[],"labelId":[],"data":"{\"message\": \"The \" & data.attributes.friendly_name & \" has been opened.\", \"entity_id\": \"all\"}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","domain":"tts","service":"google_translate_say","output_location":"","output_location_type":"none","x":676,"y":976,"wires":[[]]}]

Retrieving a Property Value of a Home Assistant Entity

Home Assistant nodes support custom JSONata functions to fetch properties of any HA entity.

  • $entities() returns all entities in the cache
  • $entities("entity_id") returns a single entity from the cache that matches the given entity_id

Example: Getting the Friendly Name of an Entity

{ "message": "The " & $entities("binary_sensor.front_door").attributes.friendly_name & " has been opened." }

Example: Getting Friendly Names of All Lights and Switches in an On State

{
     "message": "The " & $join($entities().*[state = "on" and entity_id ~> /^light|^switch/].attributes.friendly_name, ", ") & " are on.",
     "title": "Left On"
}

Performing Arithmetic Operations

Since Home Assistant states are represented as strings, you need to cast them to numbers in JSONata to perform arithmetic. Use the function $number() for this purpose. Although most entity attributes are in the correct state, casting them as numbers ensures accuracy.

Example:

$number($entities("sensor.kitchen_lux").state)

Example: Adding 3 to the Current Temperature of a Climate Entity

Screenshot of action node

{ "temperature": $entities("climate.thermostat").attributes.temperature + 3 }

Creating a Comma-Delimited Entity ID List

You can generate an entity ID list by retrieving entities using the get-entities node and then creating a comma-separated list to turn them off. In this example, the entity ID field is left blank as it is defined in the data field.

Screenshot of flow

Screenshot of action node

{ "entity_id": $join(payload.entity_id, ",") }
[{"id":"80c20ba2.f09cd8","type":"ha-get-entities","z":"c89d915bdff0f798","name":"get lights that are on","server":"","version":1,"rules":[{"condition":"state_object","property":"entity_id","logic":"starts_with","value":"light.","valueType":"str"},{"condition":"state_object","property":"state","logic":"is","value":"on","valueType":"str"}],"outputType":"array","outputEmptyResults":false,"outputLocationType":"msg","outputLocation":"payload","outputResultsCount":1,"x":516,"y":1152,"wires":[["cafbf446.080928"]]},{"id":"cafbf446.080928","type":"api-call-service","z":"c89d915bdff0f798","name":"turn them off","server":"","version":6,"debugenabled":false,"action":"light.turn_off","floorId":[],"areaId":[],"deviceId":[],"entityId":[],"labelId":[],"data":"{\"entity_id\": $join(payload.entity_id,\",\")}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","domain":"light","service":"turn_off","output_location":"","output_location_type":"none","x":710,"y":1152,"wires":[[]]},{"id":"39666758.cee048","type":"inject","z":"c89d915bdff0f798","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":332,"y":1152,"wires":[["80c20ba2.f09cd8"]]}]

Additional Resources:

  • Action Node Documentation
  • JSONata Guide
  • JSONata Documentation
  • JSONata Playground
Help us improve this page!
Last Updated:
Contributors: Jason, jason
Prev
First Automation
Next
Conditionals