Node-Red Modbus read to call service node

I am able to read the vale from via modbus from my PLC in a debug node but I am trying to figure out how to update a call service node with this value. I am getting the Call service error and I am not sure what I should be putting in the data field for the call service node?

image

[{"id":"60cce770e7fc7035","type":"inject","z":"91cfb05c729d8220","name":"","props":[{"p":"topic","v":"","vt":"date"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":190,"y":2340,"wires":[["494db88edae7cd17"]]},{"id":"494db88edae7cd17","type":"function","z":"91cfb05c729d8220","name":"Display Gate Operational State","func":"msg.payload = { \n    value: msg.payload, \n    'fc': 3,\n    'unitid': 1,\n    'address': 49152,\n    'quantity':1,\n    } \n    return msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":2340,"wires":[["b9619e76bf35c64f"]]},{"id":"43797ef24240043b","type":"api-call-service","z":"91cfb05c729d8220","name":"","server":"788afdb8.f1c034","version":5,"debugenabled":false,"domain":"input_number","service":"set_value","areaId":[],"deviceId":[],"entityId":["input_number.gate_status"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1090,"y":2400,"wires":[[]]},{"id":"7a9c4a1615ad580e","type":"debug","z":"91cfb05c729d8220","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1050,"y":2320,"wires":[]},{"id":"b9619e76bf35c64f","type":"modbus-flex-getter","z":"91cfb05c729d8220","name":"","showStatusActivities":false,"showErrors":false,"logIOActivities":false,"server":"6acb19b43669ef88","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"keepMsgProperties":false,"x":750,"y":2340,"wires":[["7a9c4a1615ad580e"],["43797ef24240043b"]]},{"id":"bec896dd87d68917","type":"comment","z":"91cfb05c729d8220","name":"Read Gate PLC Counter Value","info":"","x":630,"y":2280,"wires":[]},{"id":"788afdb8.f1c034","type":"server","name":"Home Assistant","version":4,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m"},{"id":"6acb19b43669ef88","type":"modbus-client","name":"Click PLC","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"queueLogEnabled":false,"tcpHost":"192.168.1.25","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":1,"commandDelay":1,"clientTimeout":1000,"reconnectOnTimeout":true,"reconnectTimeout":2000,"parallelUnitIdsAllowed":true}]

Use the developer tools in HA to see what the service call should look like.

You have the debug on the top out what is the output of the bottom?


here is what i see in services, but I am still not sure what i need to put in the call service node data field.

image
the payload in the debug field shows as an array and you can see the value i want to set this helper input number to.

image
in the modbus flex getter node I have not defined anything in either output but the debug node is at least showing the value read from the PLC

Try to set the value to msg.payload[0] instead

Set the value to msg.payload{0} in the Data field of the call service node? I tried that and did not work

not { }, but [ ]

Sorry,
used the wrong symbols in the reply here, I did use [] in the Data field

I am still getting this error: “Call-service error. required key not provided @ data[‘value’]”

That message is different than before.
It sounds like you do not have the value keyword in there,
Does your data look something like:

j: {“value”:msg.payload[0] }

It was not but I did just try putting in what you sent here and I am still getting this error

Put a debug node on the lower output of the modbus node and post the data.

1 Like

It is just an input_number call, so j: {“value”:msg.payload[0] } should be correct.
You got the output in the debug log, that showed 0: 2540. Could you click the “copy path” icon to the right of it to see what it write out?

{“data”:[2571],“buffer”:[10,11]}

when I click copy path is just says: payload

under the help for the Modbus node it shows the difference between the 2 outputs

Output 1: data Array (PDU), modbus response Buffer, input message

Output 2: modbus response Buffer, data Array (PDU), input message

I am not sure which one I should use for the call service node but output 1 makes more sense with the debug node showing the value in a single element array?

I do not understand why it make the error “Call-service error. required key not provided @ data[‘value’]” with the data line set to

j: {"value":msg.payload[0]}

Try maybe with

j: {'value':msg.payload[0]}

The only difference here is the characters around the value. It is a longshot, but maybe something replaced the characters, so they are not the correct ones. This is just the other character that can be used in JSONata, so maybe the change will not occur with this.

1 Like

The call-service node accepts config overrides in msg.payload, https://zachowj.github.io/node-red-contrib-home-assistant-websocket/node/call-service.html#input.

msg.payload.data is one of those overrides. So it doesn’t matter what you put in the data field of the node it will always be overwritten by the array [2571]. Use a change node before the call-service node to change the array to a single integer value.

image

in the call-service node use {"value": payload}

But it does not look like it has a msg.data inside the input message.
At least not what I can see here.