Value for modbus write

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.

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 :

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

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 :slight_smile:

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

ps. since you are new to Node-red you can watch these video tutorials made by the developers of Node-red
Node-RED Essentials

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

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

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

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

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

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:

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

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