Call Service Tips and Tricks

Homeassistant Domain

The homeassisant domain can be used with different domains of entities with certain services.

Here's an example of using the homeassistant domain to turn off some lights, switches, and everything that can be turned off in the laundry room in a single service call. This can save you from having to have multiple call-service nodes.

screenshot of a call-service node using homeassistant domain

Using Mustache Templates

Mustache templates can be used in the domain, service, and entity id fields. This is useful if you want to set the service based on msg.payload or any other message property.

Here's an example using eztimeropen in new window to set the output of the node to on or off. Then use that in the call-service node for which service to use.

screenshot of flow

screenshot of the call-service node

[{"id":"522c54b8de855062","type":"api-call-service","z":"a38123c1.2fd3f","name":"","server":"","version":4,"debugenabled":false,"domain":"homeassistant","service":"turn_{{payload}}","target":{"areaId":["75a88dfa58844f8aad752dde5bc51fc7"],"deviceId":[],"entityId":["light.front_porch","light.garage","light.kitchen"]},"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":526,"y":672,"wires":[[]]},{"id":"cca91c6fe47d7fa0","type":"eztimer","z":"a38123c1.2fd3f","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":272,"y":672,"wires":[["522c54b8de855062"]]}]

Mustache templates are also accepted in the data field when their type is set to JSON.

Also see:

Targets

The target fields allow you to target areas, devices, and/or entities. Any combination can be used when they are available, some services don't accept areas and devices.

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

Data Field

TIP

It's recommended to use the JSONata expression, J: Expression, for the data field as it has several advantages over JSON.

  • Handles quotes based on the type of variable
  • Allows the insertion of more complex variables such as arrays and object
  • Mathematical operations are permitted

Inserting a message property into a string

Sending a TTS message when a door is opened

screenshot of flow

screenshot of the call-service node

{ "message": "The " & data.attributes.friendly_name & " has been opened." }
[{"id":"cd1f9610a73addbc","type":"server-state-changed","z":"a38123c1.2fd3f","name":"","server":"","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor\\..+_door$","entityidfiltertype":"regex","outputinitially":false,"state_type":"str","haltifstate":"open","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":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":338,"y":560,"wires":[["4e016826f6497b79"],[]]},{"id":"4e016826f6497b79","type":"api-call-service","z":"a38123c1.2fd3f","name":"send tts","server":"","version":4,"debugenabled":false,"domain":"tts","service":"google_translate_say","target":{"areaId":[],"deviceId":[],"entityId":["all"]},"data":"{\"message\": \"The \" & data.attributes.friendly_name & \" has been opened.\"}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","output_location":"","output_location_type":"none","x":570,"y":560,"wires":[[]]}]

Getting a property value of a Home Assistant entity

There's a custom function in JSONata inside Home Assistant nodes that allows the fetching of any property of an HA entity.

  • $entities() returns all entities in the cache
  • $entities("entity_id") returns a single entity from the cache matching the passed in entity_id

Doing arithmetic

Home Assistant states are represented as strings so to be able to do arithmetic on them in JSONata they will need to be cast to a number first use $number()open in new window. Most attributes of entities are in their correct state but never hurt to be safe and cast them as a number.

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

Adding 3 to the current temperature of a climate entity

screenshot of call-service node

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

Create a comma-delimited entity id list

Example of getting a list of lights from the get-entities node and then creating an entity id list to turn them off. The entity id field is left blank in this example as it is defined in the data field.

screenshot of flow

screenshot of call-service node

{ "entity_id": $join(payload.entity_id, ",") }
[{"id":"80c20ba2.f09cd8","type":"ha-get-entities","z":"f731b121.0463a","server":"","name":"get lights that are on","rules":[{"property":"entity_id","logic":"starts_with","value":"light.","valueType":"str"},{"property":"state","logic":"is","value":"on","valueType":"str"}],"output_type":"array","output_empty_results":false,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":468,"y":528,"wires":[["cafbf446.080928"]]},{"id":"cafbf446.080928","type":"api-call-service","z":"f731b121.0463a","name":"turn them off","server":"","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"","data":"{\"entity_id\": $join(payload.entity_id,\",\")}","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":662,"y":528,"wires":[[]]},{"id":"39666758.cee048","type":"inject","z":"f731b121.0463a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":284,"y":528,"wires":[["80c20ba2.f09cd8"]]}]

Also see: