Dynamic sensors in call service

Hello
I’m trying to get dynamic sensors in my call service but can’t get them. Is there anyone here who can help me with the formatting.

JSONataError: Expected ")", got "|"

{"device_id":"6893ee838a0972bd3e50d9e0faa0ba5a","duration":"120","power":{{ (states('sensor.limited_power') | int(0) | abs)}}}

https://zachowj.github.io/node-red-contrib-home-assistant-websocket/guide/call-service.html#getting-a-property-value-of-a-home-assistant-entity
https://zachowj.github.io/node-red-contrib-home-assistant-websocket/guide/jsonata.html
https://docs.jsonata.org/overview.html

J: JSONata (expression)

The JSONata programming language is not Jinja templating. Any attempt to use {{ template }} syntax will result in an error.

{
    "device_id":"6893ee838a0972bd3e50d9e0faa0ba5a",
    "duration":"120",
    "power": $entities('sensor.limited_power').state~>$round()~>$abs()
}

The $entities() function only works inside the WebSocket nodes (eg Call Service)

~> is the JSONata function chaining operator, similar to | pipe

$round() accepts the context value as input, and by default [$round(0)] rounds to an integer (or you could use $floor()

$abs() accepts the context value as input and returns the absolute value

I can’t test your specific sensor. If your sensor holds a string as state, then you will need ~>$number()~>$round()~>$abs()

If you don’t like function chaining, you can nest the functions, just make sure the ( ) all match correctly :slight_smile:

$abs($round($number($entities('sensor.limited_power').state),0))

Hello
I’m a bit unsure of what you mean, but in the automations in home assistant, the code
"{{ (states('sensor.limited_power') | int(0) | abs)}}" works good.
The original value is - so that’s why I include abs in the code.

Home Assistant uses templates in YAML and in automations.
Templates, things in {{ }} are executed using the Jinja templating engine.

Node-RED is not Home Assistant. When you are in Node-RED, some nodes can use Jinja templating in the {} JSON option, but this does not work well when trying to construct the complex data object for calling a service.

When you are in Node-RED, and you are using J: you are using JSONata.

JSONata is a different language. It is not templating. It does not work like templating.

In Node-RED, in JSONata, the code {{ (states('sensor.limited_power') | int(0) | abs)}} will generate an error.

The correct (assuming I have typed it correctly) JSONata code you want is

$entities('sensor.limited_power').state~>$round()~>$abs()

This is much the same as if I go into a coffee shop in my home town in Bristol (Bristle me babber) and ask “Kan jag ha en stor latte-lampa att gå?” - there would be a blank look as the processing engine does not speak that language.

Some HA services accept jinja2 templates but it varies by service. I recommend letting NR handle the dynamic values in a service call.

By default, NR tries to parse the data field using JSONata which can conflict with the structure of jinja2 templates. If you switch the data field type to “JSON” and check “Use alternate template tags for the data field” what you have should work.