I got this one solved for openHab2, but I guess the same trick can also work with other controllers.
In openhab, activate the mqtt-eventbus bridge that copies all events on the eventbus to an mqtt broker of your choice.
The mqtt.cfg
configuration file (example)
mosquitto.url=tcp://localhost:1883
mosquitto.clientId=openhab
mosquitto.retain=true
mosquitto.async=false
The mqtt-eventbus.cfg
configuration file (example)
broker=mosquitto
statePublishTopic=openHAB/out/${item}/state
stateSubscribeTopic=openHAB/in/${item}/state
commandSubscribeTopic=openHAB/in/${item}/command
In the openhab .things configuration file, add a text element elements for the node
...
text test_text [ nodeId="4", childId="200"]
customSensor gasMeter [ nodeId="4", childId="1"]
...
In the .items configuration file, add the text element as well
String test_text "text" <text> {channel="mysensors:text:gateway:test_text:text"}
Number gasMeter_teller "Counter" <var1> {channel="mysensors:customSensor:gateway:gasMeter:var1"}
Number gasMeter_pulsLengte "Pulse Lengte" <var2> {channel="mysensors:customSensor:gateway:gasMeter:var2"}
On the node, define an extra S_INFO/V_TEXT child sensor. The below code example parses incoming instructions (in the receive() function).
#define CHILD_ID_TEXT 200
MyMessage textMsg(CHILD_ID_TEXT, V_TEXT);
void presentation()
{
...
present(CHILD_ID_TEXT, S_INFO);
...
}
...
void receive(const MyMessage &message)
{
if (message.type == V_VAR1)
{
pulseCounter = message.getLong(); // Update the main counter value at node startup or when updating the value in openhab UI
}
if (message.type == V_TEXT)
{
//this code could be more professional, just doing it quick and dirty for now
String all = message.data;
int equalSignPosition = all.indexOf('=');
if (equalSignPosition < 0) return;
String key = all.substring(0, equalSignPosition);
String value = all.substring(equalSignPosition +1, all.length());
if (key == "debouncePeriod")
{
int dbPvalue = value.toInt();
if (dbPvalue > 5 && dbPvalue < 5000) {
debouncePeriod = dbPvalue;
SendParameter("Debounce period",debouncePeriod);
}
}
if (key == "timeBeforeSleep")
{
long tBFSvalue = value.toInt();
if (tBFSvalue > 1000 && tBFSvalue < 300000) {
timeBeforeSleep = tBFSvalue;
SendParameter("Time before sleep", timeBeforeSleep);
}
}
if (key == "info")
{
Serial.print("Sending information");
SendParameter("debouncePeriod", debouncePeriod);
SendParameter("timeBeforeSleep", timeBeforeSleep);
SendParameter("pulseCounter", pulseCounter);
}
}
}
void SendParameter(char* parameterName, unsigned long variable)
{
char buffer[30];
sprintf(buffer, "%s->%lu", parameterName, variable);
Serial.println(buffer);
send(textMsg.set(buffer));
}
...
Now parameters can be changed by sending mqtt messages to openhab which will forward the instructions to the node. Feedback is provided in the reverse way for every parameter change.
For example, to change the debounce period, send an MQTT message with topic= openHAB/in/test_text/command
and data=debouncePeriod=<value>
. The feedback is provided on topic=openHAB/out/test_text/command
with data=debouncePeriod-><value>
.
To get an overview of the parameters, send info=
. One message is returned for every parameter and one for the current meter counter value.