Node-Red as Controller

  • Plugin Developer

    Any idea or try on integrating Node-red running on raspberry pi as controller with MySensors network.

  • Hello
    I have been tinkering a little with this and it does actually work.
    The only thing is that you must point the topic of the mqtt node directly to a child. Its not possible to just subscribe to topics higher in the hierarchy.

    For example in MyMQTT app on Android i can just subscribe to topic MyMQTT and then i get messages from all nodes connected to the Gateway. This is not possible with the mqtt node in NodeRED. You must specifically point it.
    below is and example of an mqtt node outputting to a debug node. My Gateway is at

    [{"id":"d0ff7866.2f0088","type":"mqtt-broker","broker":"","port":"1883","clientid":"MQTT"},{"id":"14e0544d.eb1fac","type":"debug","name":"","active":true,"console":"false","complete":"false","x":304,"y":827,"z":"cda8f31c.32571","wires":[]},{"id":"8792e08c.786d2","type":"mqtt in","name":"Temperatur","topic":"MyMQTT/21/1/V_TEMP","broker":"d0ff7866.2f0088","x":67.5,"y":791,"z":"cda8f31c.32571","wires":[["14e0544d.eb1fac"]]}]

    I am working on some code to push temperatures as an object trough a websocket for live viewing on a simple webpage.
    It maybe sounds advanced, but it is actually very easy. In my opinion Node-RED is just as powerfull as OpenHAB and not particulary more advanced either. Will post a full working example when i have everything ready.

  • ok so here is a example.
    please note, i am not a programmer so the code might not be the slickest, but it does its job.
    What this on is doing is basically emulating 3 mqtt inputs, whit options to see 2 different temperatures.
    Send it over via websocket and also use a http request node to serve a webpage straight from nodered, styled and everything, but without use for a server. Pretty nifty 🙂
    So now i must just create 3 temperature sensors and add them instead of the input nodes.

    Some pictures:

    And the webpage generated at localhost:1880/temp:

    Here is the code for the nodes:

    [{"id":"a770a428.588f58","type":"websocket-listener","path":"/admin/ws/temp","wholemsg":"false"},{"id":"40dd0e92.bf22f","type":"http in","name":"allow request on localhost:1880/temp","url":"/temp","method":"get","x":809,"y":578,"z":"cda8f31c.32571","wires":[["e2702220.1d8fe","baa85854.4557a8"]]},{"id":"e2702220.1d8fe","type":"template","name":"make HTTP response","field":"payload","template":"<!DOCTYPE HTML>\n<html>\n    <head>\n    <meta charset=\"utf-8\">\n    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n    <title>Temp</title>\n\n    <!-- Bootstrap -->\n    <link rel=\"stylesheet\" href=\"\">\n\n    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->\n    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->\n    <!--[if lt IE 9]>\n      <script src=\"\"></script>\n      <script src=\"\"></script>\n    <![endif]-->\n    \n    </head>\n    <script type=\"text/javascript\">\n        var server = window.location.hostname;\n        var topics = {};\n        var wsUriC = \"ws://\";\n        function wsConnectC() {\n            console.log(\"connect\",wsUriC);\n            var ws = new WebSocket(wsUriC);\n            ws.onmessage = function(msg) {\n               \n                var payloadObj = JSON.parse(;\n                console.log(payloadObj);\n\t\t\t\tvar output = \"\";\n\t\t\t\t\tfor (var property in payloadObj) {\n  \t\t\t\t\t\toutput += '<li class=\"list-group-item\"><strong>' +property + '</strong>' + ': ' + payloadObj[property]+'°C</li>';\n\t\t\t\t\t\t}\n                document.getElementById('messages').innerHTML = output;                                     \n              \n            }\n            ws.onopen = function() {\n                document.getElementById('status').innerHTML = \"<small>connected</small>\";\n                console.log(\"connected\");\n            }\n            ws.onclose = function() {\n                document.getElementById('status').innerHTML = \"not connected\";\n                setTimeout(wsConnectC,1000);\n            }\n        }\n    </script>\n    <body onload=\"wsConnectC();\" onunload=\"ws.disconnect;\">\n    <div class=\"container\">\n      <div class=\"panel panel-default\" margin: 15px;>\n  \t\t\t\t\t<div class=\"panel-heading\"><h3>Temperaturer</h3></div>\n       \t\t\t\t\t <div class=\"panel-body\" id=\"messages\">\n       \t\t\t\t\t <ul class=\"list-item\" id=\"messages\">\n       \t\t\t\t\t </ul>\n       \t\t\t\t\t \n       \t\t\t\t\t </div>\n        \t\t <div class=\"panel-footer\" id=\"status\"><small>disconnected</small></div>\n      </div>  \t\n     </div> \n    <script src=\"\"></script>  \n    \n    </body>\n</html>","x":1156,"y":513,"z":"cda8f31c.32571","wires":[["ee850758.117af8"]]},{"id":"ee850758.117af8","type":"http response","name":"","x":1267,"y":439,"z":"cda8f31c.32571","wires":[]},{"id":"4f43f8f8.b0bc08","type":"websocket out","name":"","server":"a770a428.588f58","x":1226,"y":389,"z":"cda8f31c.32571","wires":[]},{"id":"77a6395f.8859c8","type":"function","name":"set topic Bedroom","func":"msg.topic=\"Bedroom\";\nreturn msg;","outputs":1,"x":298,"y":237,"z":"cda8f31c.32571","wires":[["e7a54f35.185ab"]]},{"id":"69bbfd9e.964404","type":"function","name":"set topic Kitchen","func":"msg.topic=\"Kitchen\";\nreturn msg;","outputs":1,"x":304,"y":319,"z":"cda8f31c.32571","wires":[["e7a54f35.185ab"]]},{"id":"e37062a.f1c8fa","type":"debug","name":"","active":true,"console":"false","complete":"false","x":1105,"y":238,"z":"cda8f31c.32571","wires":[]},{"id":"208f83d.fdf707c","type":"inject","name":"","topic":"","payload":"22.6","payloadType":"string","repeat":"","crontab":"","once":false,"x":109,"y":298,"z":"cda8f31c.32571","wires":[["69bbfd9e.964404"]]},{"id":"b9972cd4.4668d","type":"inject","name":"","topic":"","payload":"23.8","payloadType":"string","repeat":"","crontab":"","once":false,"x":109,"y":203,"z":"cda8f31c.32571","wires":[["77a6395f.8859c8"]]},{"id":"4ba6c90c.b45938","type":"inject","name":"","topic":"","payload":"24.7","payloadType":"string","repeat":"","crontab":"","once":false,"x":106,"y":409,"z":"cda8f31c.32571","wires":[["9d9cbe26.62634"]]},{"id":"e7a54f35.185ab","type":"function","name":"Add together all payloads and send over as JSON","func":"context.temp = context.temp || new Object();\n\nif (msg.payload == \"\") {\n\tmsg2 = new Object();\n\tmsg2 = context.temp;\n\tmsg.payload=JSON.stringify(msg2);\n\t} else {\n\tcontext.temp[msg.topic] = msg.payload;        \n\tmsg2 = new Object();\n\tmsg2 = context.temp;\n\tmsg.payload=JSON.stringify(msg2);\n\t};\n\n\treturn msg;\n\n","outputs":1,"x":847,"y":296,"z":"cda8f31c.32571","wires":[["e37062a.f1c8fa","4f43f8f8.b0bc08"]]},{"id":"9d9cbe26.62634","type":"function","name":"set topic Basement","func":"msg.topic=\"Basement\";\nreturn msg;","outputs":1,"x":310,"y":388,"z":"cda8f31c.32571","wires":[["e7a54f35.185ab"]]},{"id":"3cb28a44.c34d76","type":"inject","name":"","topic":"","payload":"22.6","payloadType":"string","repeat":"","crontab":"","once":false,"x":110,"y":240,"z":"cda8f31c.32571","wires":[["77a6395f.8859c8"]]},{"id":"89646127.769ba","type":"inject","name":"","topic":"","payload":"24.7","payloadType":"string","repeat":"","crontab":"","once":false,"x":107,"y":331,"z":"cda8f31c.32571","wires":[["69bbfd9e.964404"]]},{"id":"dbd016fe.242fe8","type":"inject","name":"","topic":"","payload":"23.8","payloadType":"string","repeat":"","crontab":"","once":false,"x":106,"y":376,"z":"cda8f31c.32571","wires":[["9d9cbe26.62634"]]},{"id":"baa85854.4557a8","type":"function","name":"send empty payload to send last known temperatures","func":"msg.payload=\"\";\nreturn msg;","outputs":1,"x":749,"y":528,"z":"cda8f31c.32571","wires":[["5a9cc578.a5633c"]]},{"id":"5a9cc578.a5633c","type":"delay","name":"Delay so websocket is started before sending","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":776,"y":487,"z":"cda8f31c.32571","wires":[["e7a54f35.185ab"]]}]

  • Plugin Developer

    @Fredrik-Carlsson Very nice . node-red give a non-programmer the flexibility to write the controller logic without going into the complexity of programming.

  • ...Will post a full working example when i have everything ready.

    Any updates on this please?!

  • Hero Member

    I managed to get the following configuration to run:

    MySensors --> MySensorsEthernetGateway --> FHEM --> FHEM/MQTTDevice --> Mosquitto--> Node-Red
    FHEM and Mosquitto are running an a raspberry.
    Running Node-Red on IBM Bluemix in the cloud also succeeded to connect to my local mosquitto broker.

  • @vikasjee I am still working on my own system. Started to learn MongoDb and AngularJS so i am building my own frontend based on node-red as backend.

    There is another guy at the node-red maillist that has made a simple "mobile-app" based on node-red with a pure javascript/jquery setup. Check it out here:

  • @Heinz said:

    I managed to get the following configuration to run:

    MySensors --> MySensorsEthernetGateway --> FHEM --> FHEM/MQTTDevice --> Mosquitto--> Node-Red
    FHEM and Mosquitto are running an a raspberry.
    Running Node-Red on IBM Bluemix in the cloud also succeeded to connect to my local mosquitto broker.

    @Heinz Do you have some code/work/instreuctions to share to get an insight?

  • Hero Member

    Everything is straight forward:

    • install FHEM on a rapsberry
    • add the MYSENSORS gateway to fhem.cfg
    define MYSENSOR_Gateway MYSENSORS
    attr MYSENSOR_Gateway autocreate 1
    attr MYSENSOR_Gateway requestAck 1
    attr MYSENSOR_Gateway stateFormat connection
    • install mosquitto on the raspberry
    • add the MQTT device to fhem.cfg (point to local MQTT broker)
    define mqtt MQTT
    • publish sensor data (e.g. MQTT_CO2) from FHEM to MQTT
    attr MQTT_CO2 IODev mqtt
    attr MQTT_CO2 publishState fhem/CO2
    attr MQTT_CO2 stateFormat transmission-state

    Whenever the CO2 value changes it is forwarded to the local mosquitto broker via topic fehm/CO2
    Now you any client in the world that has access to the raspberry my subscribe this topic. You simply have to
    know the IP address of the MQTT broker.

    • install Node-Red on the raspberry
    • open the Node-Red window of the raspberry <IP>:1883
    • drag the MQTT input node to the sheet
    • configure the MQTT input node so that it uses the local broker and the topic from above
    • drag a debug output node to the sheet
    • connect both
    • Deploy

    2015-04-05 23_25_01-Node-RED _ fhem.png

    --> The CO2 values are printed in the debug window.

    The same can be done from within bluemix with the difference that node-red runs in the cloud accessing your local broker in the raspberry.

  • @Heinz Thanks a lot for the details. Actually i am looking out for a confirmation and method/way of using the nodejs controller code in NodeRED somehow (looking out for direct porting or with minimal effort) so that all the functions of a MySensor controller may be made available under NodeRED and hence a NodeRED Controller for MySensor...

    This way we may port many other functionalities available as nodejs scripts into NodeRED controller...

    Any way forward or a nudge into that direction will also help...

  • I also use node-red with a serial gateway attached. I created a flow that converts the serial payload to a mqtt type payload and it is picked up by openHAB. This also works the other way around. openHAB sents everything out via mqtt and node-red picks it up and parses it back into a serial formated payload which is picked up by the serial gateway. I also use node-red's Owntracks node to monitor my proximity and send updates to openHAB as an mqtt message and I also have a flow that monitors my phone and my wifes phone, if we are both home the outside lights get turned off at 22:00. node-red also sends twitter messages to me when my doors and windows are opened and no one is home.

    I only use mqtt with with a combiniation if nfr radios via the serial gateway, like I mentioned above, and I also use ESP8266 modules running lua. So as not to send the wrong payload to the serial gateway, all topics that come from openHAB that are destined for the serial gateway start with RF24SN/out/... and on node-red I subscribe the RF24SN/out/#. This will capture all those message and they will get parsed to the serial format (Nodeid;clientid;message;...etc).

    Everything that happens in node-red is passed to openHAB. I have a couple of relays and leds I control from openHAB. The leds use the MySensor LEDDimmer. Basically node-red acts as the middle man between the serial gateway and openHAB/mqtt.


  • @Fredrik-Carlsson said:

    The only thing is that you must point the topic of the mqtt node directly to a child. Its not possible to just subscribe to topics higher in the hierarchy.

    For example in MyMQTT app on Android i can just subscribe to topic MyMQTT and then i get messages from all nodes connected to the Gateway. This is not possible with the mqtt node in NodeRED. You must specifically point it.

    You can capture all higher level topics by using the # symbal. As I mention above, my openHAB sents RF24SN/out/.... and on node-red I have an mqtt node that listens for RF24SN/out/#.


  • @Mike-Cayouette thanks, I will try that tomorrow!

  • Hardware Contributor

    Here is my setup:

    • MyMQTT Client Gateway, OpenHAB, HomeMatic and node-red all connected to mosquitto
    • OpenHab configured to expose the internal bus to mosquitto
    • All glued together with node-red: the OpenHab Items have no mqtt binding, all messages are exchanged via the OpenHab internal bus.
    • Messages from any sensors are transformed in node-red to the destination (topic and payload)

    The advantage of this setup is:

    • OpenHab is only the GUI and doesn't need to know anything about the sensors
    • I can exchange any sensor/actuator without any change in OpenHab. I can even change from a netatmo device to a mysensor device or vice versa.

    I was thinking about replacing the MyMQTT Client Gateway with a Serial Gateway and node-red, but the gateway is stable since weeks.... never touch a running system. This would solve my biggest concern with the MyMQTT Client Gateway: it doesn't support encryption....

  • Hero Member

    The following node-red sample illustrates how the data sent by the serial gateway can be analysed.
    2015-04-09 21_42_45-Node-RED.png
    The upper lane receives the data from COM-Port, splits the incomming message into single tokens and feeds the result into
    a switch block.
    The lower lane implements a http request which displays data for message type "set".
    The result of this simple sniffer looks like:
    2015-04-09 21_45_56-Mozilla Firefox.png

    On the top right I added a debug node to visualize the incomming data:
    2015-04-09 21_47_17-Node-RED.png

    This should be a minimalistic sample to get a feeling for how much work it would take to implement a fully functional controller.
    If the "presentation" messages would be fed into some no-sql database like mongoose, answering nodeid-requests shouldn't be a challenging task. In the end I think this could be done in 1-2 days.

  • Hero Member

    if anybody wants to try it on his own here is the source which can be imported into node-red.

    [{"id":"e2627000.52f61","type":"serial-port","serialport":"COM13","serialbaud":"115200","databits":"8","parity":"none","stopbits":"1","newline":"10","bin":"false","out":"time","addchar":true},{"id":"ae116278.ceadf","type":"serial in","name":"SerialGateway","serial":"e2627000.52f61","x":75.5,"y":170.3333511352539,"z":"6e87bdcb.1ab8d4","wires":[["421469f9.a974f8"]]},{"id":"421469f9.a974f8","type":"function","name":"split message","func":"var message = msg.payload;\nsplitMessage = message.split(\" \");\nif(splitMessage[0] == \"0;0;3;0;9;read:\")\n{\n    msg.route = splitMessage[1].split(\"-\");\n    \n    var splitData = splitMessage[2].split(\"\\n\");\n    var tokens = splitData[1].split(\";\")\n    \n    msg.rawData = tokens;\n    if(tokens.length == 6)\n    {\n        msg.nodeId = tokens[0];\n        msg.childSensorId = tokens[1];\n        msg.messageType = tokens[2];\n        msg.ack = tokens[3];\n        msg.subType = tokens[4];\n        msg.payload = tokens[5];\n    }\n}\nelse\n{\n    \n}\nreturn msg;","outputs":1,"valid":true,"x":260.5,"y":170.3333511352539,"z":"6e87bdcb.1ab8d4","wires":[["ea996599.33b75"]]},{"id":"ea996599.33b75","type":"switch","name":"switch message type","property":"messageType","rules":[{"t":"eq","v":"0"},{"t":"eq","v":"1"},{"t":"eq","v":"2"},{"t":"eq","v":"3"},{"t":"eq","v":"4"}],"checkall":"true","outputs":5,"x":461.49998474121094,"y":170.3333511352539,"z":"6e87bdcb.1ab8d4","wires":[["4bca8505.d463e4"],["cfb87963.3e2a8"],["7cd01fb.f71436"],["e722ad62.22ebd"],["9f4ff922.f54838"]]},{"id":"4bca8505.d463e4","type":"function","name":"presentation","func":"msg.topic = \"presentation\";\nreturn msg;","outputs":1,"valid":true,"x":681.5,"y":37.333335876464844,"z":"6e87bdcb.1ab8d4","wires":[[]]},{"id":"7cd01fb.f71436","type":"function","name":"req","func":"msg.topic = \"req\";\nreturn msg;","outputs":1,"valid":true,"x":698.3333129882812,"y":152.33334350585938,"z":"6e87bdcb.1ab8d4","wires":[[]]},{"id":"e722ad62.22ebd","type":"function","name":"internal","func":"msg.topic = \"internal\";\nreturn msg;","outputs":1,"valid":true,"x":693.3333129882812,"y":203.33334350585938,"z":"6e87bdcb.1ab8d4","wires":[[]]},{"id":"9f4ff922.f54838","type":"function","name":"stream","func":"msg.topic = \"stream\";\nreturn msg;","outputs":1,"valid":true,"x":692.3333129882812,"y":261.3333435058594,"z":"6e87bdcb.1ab8d4","wires":[[]]},{"id":"137f8a82.b7ce45","type":"debug","name":"debug","active":true,"console":"false","complete":"true","x":897.3333129882812,"y":101.33333587646484,"z":"6e87bdcb.1ab8d4","wires":[]},{"id":"ff056cd7.417e98","type":"http response","name":"http out","x":744,"y":444.3333740234375,"z":"6e87bdcb.1ab8d4","wires":[]},{"id":"74b70760.4f808","type":"http in","name":"","url":"/mysensorsdata","method":"get","x":151.00001525878906,"y":444.3333740234375,"z":"6e87bdcb.1ab8d4","wires":[["68713a54.5c30ec"]]},{"id":"e26e1fbf.606c8","type":"template","name":"html template","field":"","format":"html","template":"\nGateway <br />\nreceived at:    {{data.timestamp}}<br />\nnode-id:        {{data.nodeId}}<br />\nchild-SensorId: {{data.childSensorId}}<br />\nmessageType:    {{data.messageType}}<br />\nsubType:        {{data.subType}}<br />\npayload:        {{data.payload}}\n","x":560.0000152587891,"y":445.33336663246155,"z":"6e87bdcb.1ab8d4","wires":[["ff056cd7.417e98"]]},{"id":"68713a54.5c30ec","type":"function","name":"message buffer","func":" = || new Object();\n\nswitch (msg.topic) {\n    case \"set\":\n = new Date().toLocaleString();\n = msg.nodeId;\n = msg.childSensorId;\n = msg.messageType;\n = msg.subType;\n = msg.payload;\n        msg = null;\n        break;\n    default:\n    \ =;\n}\n\nreturn msg;","outputs":1,"valid":true,"x":393.00001525878906,"y":445.3333435058594,"z":"6e87bdcb.1ab8d4","wires":[["e26e1fbf.606c8"]]},{"id":"8f3083f8.2dc96","type":"http in","name":"","url":"/mysensors","method":"get","x":135.00001525878906,"y":507.3333740234375,"z":"6e87bdcb.1ab8d4","wires":[["92f9be8f.e42c68"]]},{"id":"92f9be8f.e42c68","type":"template","name":"Auto Update Script","field":"","template":"<script>\n    setInterval(function(){\t\n\t    var theUrl = \"http://localhost:1880/mysensorsdata\";\n\t    var xmlHttp = new XMLHttpRequest();\n\t \"GET\", theUrl, false );\n\t    xmlHttp.send( null );\n\t    document. getElementById('mysensorsdata')\n\t    \t.innerHTML = xmlHttp.responseText;\n    },1000);\n</script>\n<div id = 'mysensorsdata'>\n ... Loading ...\n</div>\n","x":410,"y":507.3333740234375,"z":"6e87bdcb.1ab8d4","wires":[["ff056cd7.417e98"]]},{"id":"cfb87963.3e2a8","type":"function","name":"set","func":"msg.topic = \"set\";\nreturn msg;","outputs":1,"valid":true,"x":696.5,"y":101.33333587646484,"z":"6e87bdcb.1ab8d4","wires":[["137f8a82.b7ce45","68713a54.5c30ec"]]}]

  • Plugin Developer


    where are you storing the sensor data for presentation , stream. let me know which database you are using here? you can have another flows for sending the the request to sensor node via serial port. I tried it same some time back . may be i can share it here. 😃

  • @Heinz ,

    Thanks for the flow. kindly post how you have referenced mysensors library for the flow? Split function is spitting out an error as is.
    OR Is it required at all?

    Function Error: TypeError: Cannot call method 'split' of undefined (line 8, col 31)

  • Hero Member

    @Dheeraj right now I do not have a database attached as the flow is not finished. I plan to use redis or mongodb.

    @vikasjee Indid not reference any mysensors lib in the flow. Split is standard javascript. I wonder why this should not work on your system.

  • @Dheeraj said:


    where are you storing the sensor data for presentation , stream. let me know which database you are using here? you can have another flows for sending the the request to sensor node via serial port. I tried it same some time back . may be i can share it here. 😃

    @Dheeraj, Will you like to share your way out here?

Log in to reply





Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.