Call Service Node - Set fan speed not working - JSON error

I created a flow to control a fan based on some humidity sensors with a pid controller.

but I can’t seem to figure out how to pass the value to the call service node

If I use this:
grafik

i get this error
grafik

if I use this:
grafik

i get this error:
grafik

here at least it looks like the number arrives at the call service.

Where is my error?

This is the flow and the Code:

[{"id":"e8439e47ea23d3d5","type":"api-call-service","z":"8bcc78a80d0753f6","name":"Fan Speed Control","server":"2fb8aa18.846bb6","version":5,"debugenabled":true,"domain":"fan","service":"set_percentage","areaId":[],"deviceId":["779153d18023a443d5e7ef0511a53ffd"],"entityId":["fan.esphome_esp32_abluft_2"],"data":"{\"{{payload}}\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":830,"y":460,"wires":[["053cd6b761caaf68"]]},{"id":"2fb8aa18.846bb6","type":"server","name":"Home Assistant","addon":true}]

The Call Service node, Data UI field, requires a JSON object.

This object will contain required and optional parameters, each identified by a key/value pair. The parameters are very integration dependent, so the integration documentation will tell you just what is required.

If you want ‘percentage’ then your final data object needs to end up like
{"percentage" : 51}. The integration docs should tell you if the value is a number or a string, so it could be {"percentage" : "51"}

To pass variable values to this data structure, you can use mustache templates, but they don’t work at all well inside structures as the {{ is confused with the JSON object constructor. Ideally you should use JSONata (the J: option) to build your Data field object.

To build a JSON data object using JSONata is very simple.

{"percentage": payload } is probably all you need (but use the J: option).

If your end integration is expecting a number and not a string, and your msg.payload is a string (of a valid number) then at bit more work is required.

{"percentage": $number(payload) } should do the trick, but do experiment first. I think your flow is passing a number, so just

{"percentage": payload}

The best way to sort this out is to use the Home Assistant Developer Tools > Services option. This allows you to set up a service call, to fully test that it works, and then switch from the UI mode to the YAML code mode, and you will see the full data object that the underlying API service call requires. In the WebSocket node, the bit you are interested in is the ‘data’ object field (as a sub-level object). Whatever this is can be converted from the YAML to JSON and used as a template in the node.

You can find extensive documentation on all of this at

https://zachowj.github.io/node-red-contrib-home-assistant-websocket/cookbook/jsonata/call-service.html

Hope this helps!

1 Like

that was the solution, thank you very much