Mqtt to Modbus Incorrect Value

I am having trouble with mqtt to modbus.

Off AirCon/SetOnOff=0
On AirCon/SetOnOff=1
Off modbus {"address":0,"state":true}
On modbus {"address":0,"state":true}

as you can see no matter if mqtt receives a message 0 or 1 when it passes to Modbus wright node
is shows as {"address":0,"state":true}

How do I send a {"address":0,"state":false} ?

not enough info.

where / how do you attempt to change {"address":0,"state":true} to {"address":0,"state":false} based on the MQTT value? In a function? A change node? (copy and paste the function & a screenshot of your flow would be a start). include screenshots showing the data coming out of MQTT and out of your function/change node)

An example

[{"id":"c88ee50b.60616","type":"inject","z":"57675e72.26a0d8","name":"1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":300,"y":3920,"wires":[["6566afd8.d78ed8"]]},{"id":"6566afd8.d78ed8","type":"change","z":"57675e72.26a0d8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"address\":0,\"state\":$boolean(payload)}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":3880,"wires":[["7c3a6f44.9d4da"]]},{"id":"68bde274.646a64","type":"inject","z":"57675e72.26a0d8","name":"0","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":280,"y":3860,"wires":[["6566afd8.d78ed8"]]},{"id":"7c3a6f44.9d4da","type":"debug","z":"57675e72.26a0d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":720,"y":3880,"wires":[]}]

Its doing something different

[{"id":"b9ee7504.de4d78","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"d6bc5de9.9b116","type":"change","z":"b9ee7504.de4d78","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"address\":0,\"state\":$boolean(payload)}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":660,"y":260,"wires":[["e9110e0b.630f"]]},{"id":"e9110e0b.630f","type":"debug","z":"b9ee7504.de4d78","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":950,"y":260,"wires":[]},{"id":"74db1897.77ce18","type":"inject","z":"b9ee7504.de4d78","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"AirCon/SetOnOff","payload":"0","payloadType":"msg","x":320,"y":260,"wires":[["d6bc5de9.9b116"]]},{"id":"d4cda24.bd8556","type":"inject","z":"b9ee7504.de4d78","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"AirCon/SetOnOff","payload":"1","payloadType":"msg","x":330,"y":320,"wires":[["d6bc5de9.9b116"]]}]

When I tried it on my live flow it shows as below.

image

MQTT is sending
AirCon/SetOnOff=0
AirCon/SetOnOff=1

Modbus write node requires
fc=5 address=0 value=0
fc=5 address=0 value=1

Make up your mind the first post said it require boolean true or false , then you say now 1 or 0
Edit jsonata expression and remove boolean,

{"fc":5,"address":0,"state":payload}

or without jsonata expression.

[{"id":"aeb45263.467678","type":"inject","z":"57675e72.26a0d8","name":"0","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":190,"y":3920,"wires":[["a080117c.795dd8"]]},{"id":"a080117c.795dd8","type":"change","z":"57675e72.26a0d8","name":"","rules":[{"t":"set","p":"hold","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"{\"fc\":5,\"address\":0}","tot":"json"},{"t":"move","p":"hold","pt":"msg","to":"payload.state","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":3940,"wires":[["c7d62f3a.9ab3f"]]},{"id":"9dec0ca9.9bef68","type":"inject","z":"57675e72.26a0d8","name":"1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":210,"y":3980,"wires":[["a080117c.795dd8"]]},{"id":"c7d62f3a.9ab3f","type":"debug","z":"57675e72.26a0d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":3940,"wires":[]}]

If this fails please show us a debug of mqtt in nodes output.

Sorry but this makes no sense to me, It will turn on but not off.
Even with the new rules it will not send state :false to Modbus. but its confusing because the Modbus device says it needs a value "0" to turn off. I believe the Modbus node handles address and function. Code.
Even though Modbus receives a value of 0 it should show as false in the Modbus response I think.

See the images of the two debug nodes and what the pick up.

image

image

Hi .. in your flow your injects are not set to Number 0 and Number 1

try this

[{"id":"e9110e0b.630f","type":"debug","z":"94ab22aa.035ba","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":930,"y":620,"wires":[]},{"id":"74db1897.77ce18","type":"inject","z":"94ab22aa.035ba","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"AirCon/SetOnOff","payload":"true","payloadType":"bool","x":240,"y":540,"wires":[["cc5d800d.3d5528"]]},{"id":"d4cda24.bd8556","type":"inject","z":"94ab22aa.035ba","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"AirCon/SetOnOff","payload":"false","payloadType":"bool","x":250,"y":600,"wires":[["cc5d800d.3d5528"]]},{"id":"bd8ef1c9.41c448","type":"modbus-flex-write","z":"94ab22aa.035ba","name":"","showStatusActivities":false,"showErrors":false,"server":"bf37180a.2d18e","emptyMsgOnFail":false,"keepMsgProperties":false,"x":690,"y":640,"wires":[["e9110e0b.630f"],[]]},{"id":"cc5d800d.3d5528","type":"function","z":"94ab22aa.035ba","name":"","func":"msg.payload = { \n   \n    value: msg.payload, \n    'fc': 5, \n    'unitid': 1, \n    'address': 0 , \n    'quantity': 1 \n    \n} \n\nreturn msg\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":490,"y":640,"wires":[["bd8ef1c9.41c448"]]},{"id":"7e93a593.363944","type":"inject","z":"94ab22aa.035ba","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"AirCon/SetOnOff","payload":"1","payloadType":"num","x":230,"y":760,"wires":[["cc5d800d.3d5528"]]},{"id":"591ed24e.84118c","type":"inject","z":"94ab22aa.035ba","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"AirCon/SetOnOff","payload":"0","payloadType":"num","x":240,"y":820,"wires":[["cc5d800d.3d5528"]]},{"id":"e6cc9f4c.d8ea28","type":"comment","z":"94ab22aa.035ba","name":"or","info":"","x":230,"y":680,"wires":[]},{"id":"bf37180a.2d18e","type":"modbus-client","name":"","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"queueLogEnabled":false,"tcpHost":"192.168.1.66","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":1,"commandDelay":20,"clientTimeout":1000,"reconnectOnTimeout":true,"reconnectTimeout":2000,"parallelUnitIdsAllowed":true}]

ps. modify the Modbus config to represent your settings.

Thanks , Here are the results ?


image

ok .. its seems that you are receiving strings from your MQTT
if you dont have control over the mqtt msg to make it to either Boolean True, False or Number 1, 0

then in Function node .. convert it to number yourself

msg.payload = { 
    value: Number(msg.payload), 
    'fc': 5, 
    'unitid': 1, 
    'address': 0 , 
    'quantity': 1     
} 

return msg

ps. by the way what is your Modbus unit ID ? dont forget to modify that as well if its not 'unitid': 1,

Thank you that function node worked. Not sure why out of the whole thing the on and off gave me the most trouble.

I believe its because you were using the Modbus Write node that only accepts a msg.payload with either Boolean or Number 0 or 1. Your attempts above were sending an object which wasnt a valid input for that node.

The Modbus Flex Write on the other hand allows you to send all the config (value, fc, unitid, address, quantity) as an object.

With either a Change node or a Function node you structure your payload in the correct format and it should work :wink:

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.