# Mqtt to Modbus Incorrect Value

**URL:** <https://discourse.nodered.org/t/mqtt-to-modbus-incorrect-value/40232>\
**Category:** General\
**Created:** [2 February 2021 11:42 UTC](https://discourse.nodered.org/t/mqtt-to-modbus-incorrect-value/40232 "2021-02-02T11:42:14Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![vaughan.mcd](https://avatars.discourse-cdn.com/v4/letter/v/258eb7/32.png) [@vaughan.mcd](https://discourse.nodered.org/u/vaughan.mcd)\
**Post date:** [2 February 2021 11:42 UTC](https://discourse.nodered.org/t/mqtt-to-modbus-incorrect-value/40232/1 "2021-02-02T11:42:14Z")

</div>

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} ?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [2 February 2021 11:56 UTC](https://discourse.nodered.org/t/mqtt-to-modbus-incorrect-value/40232/2 "2021-02-02T11:56:05Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [2 February 2021 11:58 UTC](https://discourse.nodered.org/t/mqtt-to-modbus-incorrect-value/40232/3 "2021-02-02T11:58:14Z")

</div>

An example

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![vaughan.mcd](https://avatars.discourse-cdn.com/v4/letter/v/258eb7/32.png) [@vaughan.mcd](https://discourse.nodered.org/u/vaughan.mcd)\
**Post date:** [2 February 2021 13:04 UTC](https://discourse.nodered.org/t/mqtt-to-modbus-incorrect-value/40232/4 "2021-02-02T13:04:31Z")

</div>

Its doing something different

```auto
[{"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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/0/a0ab6404e012191ac61498a8e96d7314ec809572.png)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/5/45ee863657d1c1fd8d84a2e463237a71cd7beb68.png)

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

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [2 February 2021 13:12 UTC](https://discourse.nodered.org/t/mqtt-to-modbus-incorrect-value/40232/5 "2021-02-02T13:12:13Z")

</div>

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,

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

```

or without jsonata expression.

```auto
[{"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.

---

<div class="post-metadata">

**Author:** ![vaughan.mcd](https://avatars.discourse-cdn.com/v4/letter/v/258eb7/32.png) [@vaughan.mcd](https://discourse.nodered.org/u/vaughan.mcd)\
**Post date:** [3 February 2021 06:32 UTC](https://discourse.nodered.org/t/mqtt-to-modbus-incorrect-value/40232/6 "2021-02-03T06:32:57Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/8/18c468bcc2b53d6584e71e75c5864a4c38d5ad41.png)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/d/ddc437f81750f4003bd1d97d851df42ae4b01014.png)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/4/d47dbb25013c447075c8e51177f010bc2317127b.png)

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [3 February 2021 06:57 UTC](https://discourse.nodered.org/t/mqtt-to-modbus-incorrect-value/40232/7 "2021-02-03T06:57:42Z")

</div>

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

try this

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/3/333667c8383400f2f7b4b998c06741e590b2fa42.png)

```auto
[{"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.

---

<div class="post-metadata">

**Author:** ![vaughan.mcd](https://avatars.discourse-cdn.com/v4/letter/v/258eb7/32.png) [@vaughan.mcd](https://discourse.nodered.org/u/vaughan.mcd)\
**Post date:** [3 February 2021 07:06 UTC](https://discourse.nodered.org/t/mqtt-to-modbus-incorrect-value/40232/8 "2021-02-03T07:06:24Z")

</div>

Thanks , Here are the results ?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/2/62dcde50055e04f4ab9a74870d967bc55be26cfe.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/0/70062c1019fa2517875f7cb9a39399fb0ea752e2.png)

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [3 February 2021 07:12 UTC](https://discourse.nodered.org/t/mqtt-to-modbus-incorrect-value/40232/9 "2021-02-03T07:12:13Z")

</div>

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

```auto
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,

---

<div class="post-metadata">

**Author:** ![vaughan.mcd](https://avatars.discourse-cdn.com/v4/letter/v/258eb7/32.png) [@vaughan.mcd](https://discourse.nodered.org/u/vaughan.mcd)\
**Post date:** [3 February 2021 10:35 UTC](https://discourse.nodered.org/t/mqtt-to-modbus-incorrect-value/40232/10 "2021-02-03T10:35:54Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/a/da69cd5cd861fb0b452a9e38c48bf0507ca69a87.png)

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [3 February 2021 13:28 UTC](https://discourse.nodered.org/t/mqtt-to-modbus-incorrect-value/40232/11 "2021-02-03T13:28:51Z")

</div>

> [@vaughan.mcd](#):
>
> 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 😉

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [4 April 2021 13:28 UTC](https://discourse.nodered.org/t/mqtt-to-modbus-incorrect-value/40232/12 "2021-04-04T13:28:55Z")

</div>

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