MQTT messages to update state in Openhab through Node-RED


I wanted to get your opinion on the usage of Node RED and MQTT. Recently I have been moving from integrating mosquitto with openhab to integrating with node-red.

What I mean with that is that instead of configuring the mqtt channel on the item in openhab, I only create the item in openhab. Then instead of updating the state of the item through openhab mqtt integration, i created a MQTT receiving node in Node-RED and use that to transform where needed (change node) and update the item state in openHAB.

The main benefit for me is that I dont need to mess around with java scripts to do my simple transformations and can handle these with the change node in Node-RED.

For example if my motion sensor sends an MQTT message with “occupancy:false”, I can easily use a change node in Node-RED with “if contains false, set payload to OFF” and send that payload to the item state for my motion sensor (Switch)

What is your thought on this approach does it make sense or do I set myself up for failure? I do recognize that for more complicated transformations I might still need javascript, but that can also be done in Node-RED through the function-node.


If you are uncomfortable with javascript transformations nothing speaks against using nodered as a kind of visual transformation service. Just a question, if the motion detector sends a plain payload of occupancy:false why don’t you use a combination of regex and a custom on off value in paper Ui? Something like s/occupancy://g and on = true off = false.

The motion sensor was just an example. I also have temp/hum, contact and vibration sensors and switches in the system.

I will continue with node-red for my transformations and use openHAB for managing the item states.

1 Like

I don’t see why you would have to mess around with JavaScript transform for messages like these. A Regex or Map transform seems plenty adequate for messages like these. This would be true for temp/hum type messages as well.

Running a completely separate service just to avoid using a transformation that you probably don’t need to be using anyway seems like a way to avoid a little bit of work now (learning how to properly use transformations) at the expense of a lot more work later on (upgrades, breakages in NodeRed or openHAB, etc). I wouldn’t recommend it.

If you were using NodeRed for other features of NodeRed beyond doing this transformation then that is something else entirely. In that case you’d be using NodeRed anyway so most of those extra long term costs will have to be paid anyway. Therefore the little bit of labor you save up front doesn’t result in extra work long term so it’s not that bad of a choice.

But like I said, it appears you are trying to solve a problem (using the JavaScript transform) that doesn’t actually exist since the JavaScript transform does not appear to be the best choice for messages like these, particularly with MQTT which allows chaining of transformations.

As a for instance, if you have a message “temperature: 20 °C” a simple “REGEX(temperature: (\d+)°C” would do the job.

1 Like

Thanks for your elaborate response. I will have a closer look at what REGEX can do for me. It might be more manageable then using NodeRed as that requires several nodes to achieve a item state update.

As a side note I am also using NodeRed as a rule engine so it is not purely for these kind of transformations.

How about when it comes to switches (single tap , double, long hold, etc)?

If it’s about using the raw button channel events and you use node red, we had a long thread about different approaches how to use them in node red directly starting at this post continuing quite a bit down Node-RED as Alternative Rule Engine

1 Like