openhab sendCommand problem with serialbinding (Solved)



  • Hello

    I did not find the solution for the loopback problem:

    when I use sendCommand with serialbinding the message is sent to the gateway.
    but at the same time this message is presented as an incoming message to openhab.

    so this message is processed again and creates a deathloop.

    I tried to move the sendCommand from the processing incoming message rule to a separate one
    but the problem is the same. With this new code I use cron so the message does not come immediately
    but the loop is still here.

    I could set up a workaround but I would like to know if there is a clean solution ?
    Is it the way the rule are working ? Is it due to serialbinding ? I tried to have a look at the serial binding code
    but did not find what could create the loopback.

    Help help thank you for your support !

    here is the code :

    //receiving msg from mysensors gateway
    rule "Arduino sends to Openhab"
        when
            Item Arduino received update
        then
            var String lineBuffer =  Arduino.state.toString.split("\n")
            for (String line : lineBuffer) {
                var String[] message = line.split(";")
                var Integer nodeId = new Integer(message.get(0))
                var Integer childId = new Integer(message.get(1))
                var Integer msgType = new Integer(message.get(2))
                var Integer ack = new Integer(message.get(3))
                var Integer subType = new Integer(message.get(4))
                var String msg = message.get(5)
          
                // Internal Command
                if ( msgType == C_INT ){
                     if(subType == I_TIME ){ 
                            println("Time request from node : " + nodeId)   
                            timeToNodeId = nodeId
                            sendTime = 1
                      }              
                }
            }
    end
    //
    //
    //
    rule "send time"
    when
                   Time cron "0 * * * * ?"
    then
                   if (sendTime == 1) {
                       var Integer time2 = now().getMillis()/1000 +7200
                       sendCommand(Arduino, timeToNodeId+";255;3;0;1;" + time2+"\n") 
                       sendTime = 0 
                   }       
    end
    

  • Hero Member

    OpenHAB is using a message bus and every message send to that bus is received by every one/rule.
    Simply moving the code will not help.

    I think the best way is to differentiate between the request and answer(sendCommand).

    Currently the rule is triggered whenever a message with "subType == I_TIME" is received by openHAB.

    if(subType == I_TIME ){ 
                            println("Time request from node : " + nodeId)   
                            timeToNodeId = nodeId
                            sendTime = 1
                      } 
    

    So, in the code above you need to differ between request and answer. I don't know how the request looks like, so I'm only able to give a hint.

    In the answer the payload of the message is the content of "time", something like 1234567. I suppose the payload in the request differs for example in length, it's shorter I suppose.

    So you could go with:

    if(subType == I_TIME ){ 
                            if(msg.length < 1) {
                               sendCommand(Arduino, timeToNodeId+";255;3;0;1;" + time2+"\n")
                            }
                      } 
    

    I hope this helps.



  • Thank you for the info

    so all the sendCommand to Arduino I will initiate will be seen again when using a trigger like "Arduino received update".

    The workaround I was thinking is the one you suggested. That is to say to check the payload of the incoming message.

    So this problem is solved !

    Thank you !



  • @Shasha, have you tried this? https://github.com/openhab/openhab/wiki/Rules#concurrency-guard

    I'm currently trying the same (because I have the same issue) and wondering if you tried this before.


Log in to reply
 

Suggested Topics

  • 22
  • 4
  • 1
  • 10
  • 19
  • 3

1
Online

11.4k
Users

11.1k
Topics

112.7k
Posts