Openhab - How to respond to Arduino request?



  • In the EnergyPulseMeter example that comes with MySensors, we have this line

    gw.begin(incomingMessage);
    

    The incomingMessage method is then defined as:

    void incomingMessage(const MyMessage &message) {
      if (message.type==V_VAR1) {  
        pulseCount = oldPulseCount = message.getLong();
        Serial.print("Received last pulse count from gw:");
        Serial.println(pulseCount);
        pcReceived = true;
      }
    }
    
    

    We also have

    // Fetch last known pulse count value from gw
    gw.request(CHILD_ID, V_VAR1);
      
    

    So if my understanding is correct, the last call is a request to the GW, and whenever we get a response, the incomingMessage method will handle it.

    My question then - How can I respond to this request from my Openhab rules?


  • Hero Member

    Your understanding is correct. 🙂

    How does the request looks like in the serial monitor? Do you already have an corresponding item that stores the pulse count?



  • This is the output from the serial monitor when starting the sensor:

    sensor started, id=105, parent=0, distance=1
    send: 105-105-0-0 s=255,c=3,t=11,pt=0,l=12,sg=0,st=ok:Energy Meter
    send: 105-105-0-0 s=255,c=3,t=12,pt=0,l=3,sg=0,st=fail:1.0
    send: 105-105-0-0 s=1,c=0,t=13,pt=0,l=0,sg=0,st=ok:
    send: 105-105-0-0 s=1,c=2,t=24,pt=0,l=0,sg=0,st=ok:
    send: 105-105-0-0 s=1,c=2,t=24,pt=0,l=0,sg=0,st=ok:
    

    I'm not storing any pulse count yet, still trying to figure out how everything works together.

    My rules, still based on your code, where I've just added another if for now to log the request

    if(msgType == 1 ){
                    if (subType == V_TEMP){
                        postUpdate(sensorToItemsMap.get( nodeId + ";" + childId + ";"), msg)
                        println ("Temp item: " + sensorToItemsMap.get( nodeId + ";" + childId + ";") + " temp: " + msg )
                        }
                    if (subType == V_HUM){
                        postUpdate(sensorToItemsMap.get( nodeId + ";" + childId + ";"), msg)
                        println ("Hum item: " + sensorToItemsMap.get( nodeId + ";" + childId + ";") + " hum: " + msg )
                        }
                    }    
                // Message Request
                if(msgType == 2){
                	logInfo("TestCheck", "Request, SubType: " + subType)
                 
                }
    


  • Eventually this is what worked for me:

    if(msgType == 2){
        if (subType == V_VAR1){
            sendCommand(Arduino,"105;1;1;0;24;0\n")
        }
    }
    

    Just an example of course. Here I'm saying that the last pulse count is always zero. But this will return data to my sensor at least.


  • Hero Member

    @Daniel-Lindberg : I suppose you are already storing the pulse count in an item?
    If this is the case you simple replace "0" with "pulseCountItem.state".

    For example:

    if(msgType == 2){
        if (subType == V_VAR1){
            sendCommand(Arduino,"105;1;1;0;24;" + pulseCountItem.state + "\n")
        }
    }
    

    Where pulseCountItem is the name of the item you specified in the item definition file.



  • Actually no, not yet. I have been to obsessed with getting a response back to my sensor 🙂
    Next step would be to store the value, and send the last one back as per your example.

    One thing I still don't understand. Do I need a separate item for storing the pulse count?
    Perhaps I actually need three items, since in my sensor code (basically copy/paste from mysensors example) I have. Do I generally need one openhab item per message ?

    MyMessage wattMsg(CHILD_ID,V_WATT);
    MyMessage kwhMsg(CHILD_ID,V_KWH);
    MyMessage pcMsg(CHILD_ID,V_VAR1);
    

  • Hero Member

    I use one Item for every node- and child ID combination. My mapping table in the rules file is getting large ...

    Are you planning to use more than one power meter? Because in that case you also want to map to the corresponding item that stores the pulse count.



  • @TimO No, Just one power meter. My current items hashmap is

    // Mappings
    var HashMap<String, String> sensorToItemsMap = newLinkedHashMap(
        "101;0;"            -> "livingHum01",
        "livingHum01"       -> "101;0;",
        "101;1;"            -> "livingTemp01",
        "livingTemp01"      -> "101;1;",
        "105;1;"            -> "cellarPow01",
        "cellarPow01"       -> "105;1;"
    )
    

    Not sure how to separate the numbers for watts, kwhs and pulse counts since they all share the same CHILD_ID (in the example files). Should I change that to be three separate child IDs ?

    MyMessage wattMsg(CHILD_ID_1,V_WATT);
    MyMessage kwhMsg(CHILD_ID_2,V_KWH);
    MyMessage pcMsg(CHILD_ID_3,V_VAR1);
    

    It feels like using the same CHILD_ID for all three messages would cause postUpdate to write watts, kwhs and pc to the same item, which wouldn't make sense right?


  • Hero Member

    Using three separate child IDs is the easiest way I think, because of the way the rule is working.



  • @TimO said:

    @Daniel-Lindberg : I suppose you are already storing the pulse count in an item?
    If this is the case you simple replace "0" with "pulseCountItem.state".

    For example:

    if(msgType == 2){
        if (subType == V_VAR1){
            sendCommand(Arduino,"105;1;1;0;24;" + pulseCountItem.state + "\n")
        }
    }
    

    Where pulseCountItem is the name of the item you specified in the item definition file.

    @TimO , I tried this command earlier today, but I'm seeing the following statement in the log:

    given new state is NULL, couldn't post update for 'pulseCountItem'
    

    So it seems like there is no state for the item.
    Also tried using

    pulseCountItem.previousState(now).state
    

    but that gives

    Error during the execution of rule 'Arduino sends to Openhab': Could not invoke method: org.openhab.core.persistence.extensions.PersistenceExtensions.previousState(org.openhab.core.items.Item,boolean,java.lang.String) on instance: null
    

    Any ideas on how to proceed?


  • Hero Member

    @Daniel-Lindberg Did you declare an item "pulseCountItem" in the items file? Did you create a rule for the pulse counts that are received by OH (and afterwards stored in pulseCountItem)?



  • @TimO I did, and I can see my stored values in the "my openhab" console.

    I cross-posted my issue on open-habs community, and the concensus there seems to be that my openhab does not support restore/retrieve queries.

    https://community.openhab.org/t/unable-to-restore-state-for-item/3172/2


  • Hero Member

    Well that's reasonable according to persistance and my.openhab.

    What should work is: item.state

    I'm using a rule like:

        if(shutterAutomatic.state == ON)
        	sendCommand(Shutter_All, "UP")
    

    I'm using persistance with MySQL, but I would expect item.state to work without persistance.
    Have you tried to simply print out item.state?



  • I did, but after a simulated "power outage" item.state would simply return 0, so my sensor would reset and start accumulating KWH from 0 again.

    I updated use rrd4j rather than my.openhab and used

    pulseCountItem.previousState(false, "rrd4j").state // get the most recent update, even if it is the same as the current state
    

    It's now working 🙂


Log in to reply
 

Suggested Topics

2
Online

11.4k
Users

11.1k
Topics

112.7k
Posts