Openhab + mysensors / configuration variable request

I am running openhab 1.8 currently with mysensors mqtt gateway. So all my nodes talk to mqtt. I am working on trying to request certain variables from Openhab instead of coding directly into the node. In this case, how many times to query a light sensor, and how much time in between polling.

Below is my mysensors code and my openhab configs.

if(currentMillis - previousMillisSettings >= 300000) 
  {
    previousMillisSettings = currentMillis;
    request(2, 24);
    wait(MESSAGEWAIT);
    request(2, 25);
    wait(MESSAGEWAIT);
    
  }

So I am just requesting V_VAR1 and V_VAR2 for child sensor ID2.

Ok, so, so far so good. However, openhab doesn’t do anything with that since I am using the mysensors MQTT gateway. So I loaded up MQTT spy and saw the requests for the info.

So now lets get openhab to monitor for that.

2 new items created in the openhab config.

String MasterBedRoomLightSleepTimeReq {mqtt="<[mqtt-brunkhome:mygateway1-out/200/2/2/0/24:state:default]"}
String MasterBedRoomLightNumReadingsReq {mqtt="<[mqtt-brunkhome:mygateway1-out/200/2/2/0/25:state:default]"}

just 2 items watching for those requests.

Now 2 more items to be the configuration items.

Number MasterBedroomLightSleepTime  
Number MasterBedroomLightNumReadings

Then I created 2 rules (I know it’s getting long).

rule "Request Sleep Time"
when
        Item MasterBedRoomLightSleepTimeReq received update
then
        logInfo("Bedroom","Bedroom Counter Requested")
        publish("mqtt-brunkhome","mygateway1-in/200/2/1/0/24",(MasterBedroomLightSleepTime.state).toString())




rule "Request Num Readings"
when
        Item MasterBedRoomLightNumReadingsReq received update
then
        logInfo("Bedroom","Bedroom Light Sensor Reading Count")
        publish("mqtt-brunkhome","mygateway1-in/200/2/1/0/25",(MasterBedroomLightNumReadings.state).toString()))

These 2 rules see’s anytime there is something on those mqtt nodes, and responds back to the mqtt gateway with the info.

Then in my node, I put the following in my receive()

if (message.type==V_VAR1) {
    DEBUG_PRINT("Sleep Time Sent Back");
    SLEEPTIMEL = atoi(message.data);
    DEBUG_PRINTLN(atoi(message.data));
  }
  else if (message.type == V_VAR2)
    {
    DEBUG_PRINT("Read Count Sent Back");
    numReadings = atoi(message.data);
    DEBUG_PRINTLN(atoi(message.data));
  }

And now my openhab is responding to config requests.

I then went back and updated the number items to also push config too. So the node can come get the config, but if you change it, it also pushes real time too.

Number MasterBedroomLightSleepTime  { mqtt=">[mqtt-brunkhome:mygateway1-in/200/2/1/0/24:command:*:default]" }
Number MasterBedroomLightNumReadings  { mqtt=">[mqtt-brunkhome:mygateway1-in/200/2/1/0/25:command:*:default]" }

SOOO… here is my question… is this even the right way to do this? I mean with this method for EVERY variable I have to create an item to watch for the request, one to hold the value of said variable, and a rule?

Is there a better way to do it? Some generic item that can monitor a node like “mygateway1-out/+/2/#” that would show only config requests. And then do a transform?

Just wondering if anyone has done this with openhab or even something else that just uses the mqttgateway.

I have a separate thread on the mysensors community for this as well.

1 Like