Openhab - How to respond to Arduino request?
-
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.
-
@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); -
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.
-
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?
-
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 usingpulseCountItem.previousState(now).statebut 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: nullAny ideas on how to proceed?
-
@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
-
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 stateIt's now working :-)