# 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 a light and a switch 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

TIP

Most services in Home Assistant accept multiple entity ids in a single call. This is accomplished in the call-service node by using a comma-delimited list in the entity id field.

# 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 eztimer (opens 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":"834d8143.777f7","type":"eztimer","z":"f731b121.0463a","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":254,"y":144,"wires":[["4d3c3546.e4fedc"]]},{"id":"4d3c3546.e4fedc","type":"api-call-service","z":"f731b121.0463a","name":"turn on/off outside lights","server":"","version":1,"debugenabled":false,"service_domain":"homeassistant","service":"turn_{{payload}}","entityId":"light.porch_light, switch.pathway_lights","data":"","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":476,"y":144,"wires":[[]]}]

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

Also see:

TIP

It's recommend using 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

# Data Field

# 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":"1d489592.f42d9a","type":"server-state-changed","z":"f731b121.0463a","name":"","server":"","version":1,"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,"x":242,"y":320,"wires":[["240c55f3.35cd4a"],[]]},{"id":"240c55f3.35cd4a","type":"api-call-service","z":"f731b121.0463a","name":"send tts","server":"","version":1,"debugenabled":false,"service_domain":"tts","service":"google_translate_say","entityId":"all","data":"{\"message\": \"The \" & data.attributes.friendly_name & \" has been opened.\"}","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":474,"y":320,"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 then in JSONata they will need to be cast to a number first use $number() (opens new window). Most attributes of entities are in their correct state but never hurts 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 we defining it 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: