OH3 as facilitator to make "Niko Home Crontrol" MQTT-aware

I’m new to the forum and new to OH. We have the home automation solution of Niko Home Control (version 1) for the light switches and ventilators. This seems to be well supported by OH3 with a bridge. For testing-purposes, I’ve created a couple of things and items for light-switches and all seems to work fine.

What I would like to do is to have OH3 being configured in such a way it will act as the facilitator to have my Niko switches being reachable (bidirectional) via MQTT. So whenever I push a physical light switch, it’ll be triggering the topic-state at my MQTT broker, and whenever I change the state via MQTT-publishing, it’ll reflect on my lightbulb as well.

I’ve installed OH3 and been testing with v3.0.0 and v3.1.0. But I can’t get it to work. Can I solve this by having channels? Or do I need to create custom rules/scripts? Or did I overlook other options?

What is working:

  • Niko Home Control bridge, including thins and items
  • Mosquito is installed as the broker, works fine and bridge in OH3 is configured and working
  • A rule (for testing-purposes) when “item” changes => execute a script to publish the state to a topic on Mosquitto: actions.get(“mqtt”,“mqtt:broker:Mosquitto”).publishMQTT(“home/light/kitchen/state”, ‘{“state”: "’ + itemRegistry.getItem(“light_kitchen”).getState().toString() + ‘"}’)

Any hints/tips/advice/examples are welcome, as it seems the learning curve for OH is very steep. I would like to keep it simple and maintainable. Thanks in advance!

OH is probably a bit heavy weight to serve merely as an MQTT bridge. Is there not already a Niko2MQTT script out there on GitHub or something? OH is a hub. It’s designed to be the central point of control for all your home automation. It’s going to be far more complex than is warranted if all you want is to bridge to MQTT. OH is a full platform and you have to do a lot of work with the docs to get to where you can implement something like this in OH.

That being said, MQTT 2.5+ Event Bus is probably your best bet for simple. However, deploying it requires a fairly good understanding about how openHAB itself works. If nothing else it will provide another example.

But there are no details in “can’t get it to work” so :man_shrugging: . Logs? Errors? Does the broker Thing show as ONLINE?

Thank you for you advice. Yes, the broker is “online”.

In the meantime, I’ve been exploring the next-gen rules. In case a light-switch is changed, the following code is executed to forwarding the events of the light switches towards Mosquitto MQTT broker. I’ve tried to be generic, so the code could be used in case whatever switch has changed its status:

//logger:
var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);


if (typeof event === 'undefined') {
  //simulation-modus, for when the code is manually executed:
  var itemName = "light_master_bedroom";   //replace by the item-name of the light-switch you want to test
  
  var event = {
    "itemName": itemName,
    "itemState": itemRegistry.getItem(itemName).getState().toString()
  };
} else {
  //uncomment next line in case you would like to see the paypload of the event:
  //logger.info("event: " + event.payload);
}

var item = itemRegistry.getItem(event.itemName);
var location = "unknown";

//check the location:
if (typeof item.groupNames[0] !== 'undefined') {
  location = itemRegistry.getItem(item.groupNames[0]).label.replace(/\s+/g, '-');
}


var topic = "home/" + item.category.toLowerCase() + "/" + location.toLowerCase() + "/state";


//check type of item:
if (item.type == "Switch") {
  logger.info("executing rule for switch '" + item.label + "' at location '" + location + "' with status: '" + event.itemState + "'");
  
  actions.get("mqtt","mqtt:broker:Mosquitto").publishMQTT(topic, '{"state": "' + event.itemState + '"}');
} else if (item.type == "Dimmer") {
  var value = event.itemState;
  var state = (value > 0)?"ON":"OFF";
  var brightness = value;
  
  logger.info("executing rule for dimmer '" + item.label + "' at location '" + location + "' with status: '" + value + "%'");
  
  actions.get("mqtt","mqtt:broker:Mosquitto").publishMQTT(topic, '{"state": "' + state + '", "brightness": "' + brightness + '}');
} else {
  logger.error("unknown 'type' ('" + item.type + "') for '" + item.label + "' while executing rule...'");
}

So now I need to manage to read a command back from MQTT and adapt the switch state accordingly. But I haven’t found out yet how to hook up (based on the rules?) to such an event on Mosquitto.

I did provide a link in my reply to a complete and generic way to send and receive updates and commands on Items over MQTT. It’s two way and even if you don’t want to use it you can at least use it as an example. It doesn’t appear you even clicked on it.