# Value for modbus write

**URL:** <https://discourse.nodered.org/t/value-for-modbus-write/66374>\
**Category:** General\
**Created:** [12 August 2022 22:58 UTC](https://discourse.nodered.org/t/value-for-modbus-write/66374 "2022-08-12T22:58:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tobbbbe18](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tobbbbe18/32/66128_2.png) [@Tobbbbe18](https://discourse.nodered.org/u/Tobbbbe18)\
**Post date:** [12 August 2022 22:58 UTC](https://discourse.nodered.org/t/value-for-modbus-write/66374/1 "2022-08-12T22:58:55Z")

</div>

I try to read electricity prices, and pass on to the heating system. Got stuck a bit now.  
Have managed to get the modbus communication to work and read home the price from the electricity company.  
not quite sure how to get the function block to forward the data to the modbus block. grateful for help.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/d/fd441ba78e8c353dba34469940c72bf77d68c9ad.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:** [13 August 2022 04:07 UTC](https://discourse.nodered.org/t/value-for-modbus-write/66374/2 "2022-08-13T04:07:49Z")

</div>

You can save a value in Context and then read it whenever you need it  
... either from a Change node or a Function node

For Example :

```auto
flow.set("price", 435) // to set a Flow Context variable

let price = flow.get("price") // to read a Flow Context variable

```

You can read more information on Context here  
[Working with context](https://nodered.org/docs/user-guide/context)

---

<div class="post-metadata">

**Author:** ![Tobbbbe18](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tobbbbe18/32/66128_2.png) [@Tobbbbe18](https://discourse.nodered.org/u/Tobbbbe18)\
**Post date:** [13 August 2022 09:47 UTC](https://discourse.nodered.org/t/value-for-modbus-write/66374/3 "2022-08-13T09:47:37Z")

</div>

sorry im pretty new to this, I don't understand how it should be able to filter out the "value"?  
So i can put it in the string to the next function node  
that row will be updated once an hour.  
Thanks for helping 🙂

---

<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:** [13 August 2022 09:57 UTC](https://discourse.nodered.org/t/value-for-modbus-write/66374/4 "2022-08-13T09:57:44Z")

</div>

Can you share your Flow so we can better understand how you have configured each node ?

You can Export your flow by making a selection of those nodes and pressing `Ctrl-E` and then "Copy to Clipboard". you can post it on the forum using the Preformatted text icon ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/0/406ef58b7171502458876f9708d927729274866b.png)

ps. since you are new to Node-red you can watch these video tutorials made by the developers of Node-red  
[Node-RED Essentials](https://www.youtube.com/watch?v=ksGeUD26Mw0&list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6)

---

<div class="post-metadata">

**Author:** ![Tobbbbe18](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tobbbbe18/32/66128_2.png) [@Tobbbbe18](https://discourse.nodered.org/u/Tobbbbe18)\
**Post date:** [13 August 2022 20:10 UTC](https://discourse.nodered.org/t/value-for-modbus-write/66374/5 "2022-08-13T20:10:24Z")

</div>

Absolutely, not really sure if i succeeded but i'll try.  
want to get the variable I read from the website to be passed on to the heating system.  
thanks again

```auto
[{"id":"f91f6b68267a79b6","type":"inject","z":"abe0b36c1e5fb859","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"2","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"500","payloadType":"str","x":130,"y":200,"wires":[["ec8e692479547b7f"]]},{"id":"ec8e692479547b7f","type":"function","z":"abe0b36c1e5fb859","name":"function 1","func":"msg.payload = { value: msg.payload, 'fc': 6,\n 'unitid': 1,\n 'address': 6200, \n 'quantity': 1 };\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":340,"y":200,"wires":[["6454af260977d11b"]]},{"id":"6454af260977d11b","type":"modbus-flex-write","z":"abe0b36c1e5fb859","name":"","showStatusActivities":true,"showErrors":false,"server":"f3bec5fc660dfdd0","emptyMsgOnFail":false,"keepMsgProperties":false,"x":550,"y":200,"wires":[[],["c0580805dd570431"]]},{"id":"c0580805dd570431","type":"modbus-response","z":"abe0b36c1e5fb859","name":"","registerShowMax":20,"x":930,"y":200,"wires":[]},{"id":"9fd6f67b8bdba0e9","type":"inject","z":"abe0b36c1e5fb859","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"60","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":110,"y":360,"wires":[["6c65b346b698e7fb"]]},{"id":"6c65b346b698e7fb","type":"http request","z":"abe0b36c1e5fb859","name":"","method":"GET","ret":"txt","paytoqs":"ignore","url":"https://www.elbruk.se/timpriser-se4-malmo","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":340,"y":360,"wires":[["70e2df1d63712e27"]]},{"id":"70e2df1d63712e27","type":"html","z":"abe0b36c1e5fb859","name":"","property":"payload","outproperty":"payload","tag":"span.info-box-number","ret":"text","as":"multi","x":600,"y":360,"wires":[["0af2828acb4f5163"]]},{"id":"2aadb62436e381de","type":"debug","z":"abe0b36c1e5fb859","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":920,"y":460,"wires":[]},{"id":"0af2828acb4f5163","type":"split","z":"abe0b36c1e5fb859","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":590,"y":460,"wires":[["d1a5ad10ff8b81ca"]]},{"id":"d1a5ad10ff8b81ca","type":"function","z":"abe0b36c1e5fb859","name":"function 2","func":"flow.set(\"price\", 400) // to set a Flow Context variable\n\nlet price = flow.get(\"price\") // to read a Flow Context variable\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":760,"y":460,"wires":[["2aadb62436e381de"]]},{"id":"f3bec5fc660dfdd0","type":"modbus-client","name":"BAS2","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"queueLogEnabled":false,"failureLogEnabled":true,"tcpHost":"192.168.68.54","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","serialAsciiResponseStartDelimiter":"0x3A","unit_id":1,"commandDelay":1,"clientTimeout":1000,"reconnectOnTimeout":true,"reconnectTimeout":2000,"parallelUnitIdsAllowed":true}]

```

---

<div class="post-metadata">

**Author:** ![Tobbbbe18](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tobbbbe18/32/66128_2.png) [@Tobbbbe18](https://discourse.nodered.org/u/Tobbbbe18)\
**Post date:** [13 August 2022 20:23 UTC](https://discourse.nodered.org/t/value-for-modbus-write/66374/6 "2022-08-13T20:23:30Z")

</div>

so i want "funktion2" to send the current spotprice on to "funktion1", for now 471,23 but it will change in an hour.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/0/1077b13fd9c4708430fd3a21b08147a446c2efeb.png)

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [14 August 2022 00:39 UTC](https://discourse.nodered.org/t/value-for-modbus-write/66374/7 "2022-08-14T00:39:07Z")

</div>

SO based on the debug window the spotprice appears as msg.payload.1 - there is no need to pass this one as it is already part of the message - so unless you destroy that msg or overwrite it in your function node it is available to be referenced in function 1 as msq.payload.1

The flow you posted is not the one in the picture though ?

I think what you need to show us is the format of the modbus write command that your end device expects to get ? i.e. show us the full packet that you want to put into the modbus write command.

Craig

---

<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:** [14 August 2022 04:58 UTC](https://discourse.nodered.org/t/value-for-modbus-write/66374/8 "2022-08-14T04:58:08Z")

</div>

the flow in your last screenshot looks better .. triggering everything on the same line  
(so you dont need Context)

i would recommend changing the **Html node** to output as an array

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

and that will make selecting your value easier `msg.payload[3]`

In the Function node the `price` needs some processing .. since its a string it needs to be converted to a number .. a float ??

as @craigcurtin said .. that depends on your modbus device how it accepts the value  
if it accepts it as a float it may be 32bit so you have to write to 2 registers .. so maybe you'll need FC 16 ?  
If you provide this information we can help further.

**Test Flow:**

```auto
[{"id":"ec8e692479547b7f","type":"function","z":"54efb553244c241f","name":"function 1","func":"// processing of price depends on how Modbus accepts it\n\nlet price = msg.payload[3]\nprice = price.replace(\",\", \".\")\nprice = parseFloat(price)\n\nmsg.payload = price\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":3560,"wires":[["22bb0c4999057d89"]]},{"id":"6454af260977d11b","type":"modbus-flex-write","z":"54efb553244c241f","name":"","showStatusActivities":true,"showErrors":false,"server":"f3bec5fc660dfdd0","emptyMsgOnFail":false,"keepMsgProperties":false,"x":890,"y":3560,"wires":[[],[]]},{"id":"9fd6f67b8bdba0e9","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"60","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":230,"y":3460,"wires":[["6c65b346b698e7fb"]]},{"id":"6c65b346b698e7fb","type":"http request","z":"54efb553244c241f","name":"","method":"GET","ret":"txt","paytoqs":"ignore","url":"https://www.elbruk.se/timpriser-se4-malmo","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":390,"y":3460,"wires":[["70e2df1d63712e27"]]},{"id":"70e2df1d63712e27","type":"html","z":"54efb553244c241f","name":"","property":"payload","outproperty":"payload","tag":"span.info-box-number","ret":"text","as":"single","x":600,"y":3460,"wires":[["ba5c6714ae648cbd","ec8e692479547b7f"]]},{"id":"ba5c6714ae648cbd","type":"debug","z":"54efb553244c241f","name":"debug 1","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":840,"y":3460,"wires":[]},{"id":"22bb0c4999057d89","type":"debug","z":"54efb553244c241f","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":380,"y":3640,"wires":[]},{"id":"df55b6b6d7b4b745","type":"function","z":"54efb553244c241f","name":"function 2","func":"msg.payload = {\n value: msg.payload,\n 'fc': 16,\n 'unitid': 1,\n 'address': 6200,\n 'quantity': 2\n};\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":3560,"wires":[["41c76b5b40c4a362","6454af260977d11b"]]},{"id":"41c76b5b40c4a362","type":"debug","z":"54efb553244c241f","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":760,"y":3640,"wires":[]},{"id":"f3bec5fc660dfdd0","type":"modbus-client","name":"BAS2","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"queueLogEnabled":false,"failureLogEnabled":true,"tcpHost":"192.168.68.54","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","serialAsciiResponseStartDelimiter":"0x3A","unit_id":1,"commandDelay":1,"clientTimeout":1000,"reconnectOnTimeout":true,"reconnectTimeout":2000,"parallelUnitIdsAllowed":true}]

```

---

<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:** [13 October 2022 04:58 UTC](https://discourse.nodered.org/t/value-for-modbus-write/66374/9 "2022-10-13T04:58:09Z")

</div>

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