Is my rule too fast or my JSONPATH transformation too slow?

I have an ESP8266 that gathers 12 analog values (via a number of PCF8591 chips). I used to send those as 12 seperate MQTT messages to OpenHab. Subsequently I send an MQTT message to indicate the the measurement was done and in openhab that item triggered a rule, sending me an email with the measured values, like this:

// measure moisture
rule "last update"

when
 Item lastmeasurement changed to CLOSED
then
meettijd.postUpdate( new DateTimeType() )
sendMail("xxxxxxx@gmail.com", "Garden", "Bed1="+Moisture1.state+",\nBed2="+Moisture2.state+",\nBed3="+Moisture3.state+",\nBed4="+Moisture4.state+",\nBed5="+Moisture5.state+",\nBed6="+Moisture6.state+",\nBed7="+Moisture7.state+",\nBed8="+Moisture8.state+",\nBed9="+Moisture9.state+",\nBed10="+Moisture10.state+",\nBed11="+Moisture11.state+"\nBed12="+Moisture12.state)
end

That worked well.

Both out of efficiency point of view and because I wanted to learn, I replaced the 12 seperate MQTT messages by one message, containing a JSON. Followed again by an MQTT message updating the ‘lastmeasurement’ item, triggering the rule.
In Openhab I now would update the 12 items with a JSONPATH, like so:

{mqtt="<[mosquitto:home/moisture:state:JSONPATH($.data[0])]"}
.........
.........
{mqtt="<[mosquitto:home/moisture:state:JSONPATH($.data[11])]"}

That worked wonderwell.

But then I saw a glitch: usually the values in my mail message would be in line with the updated values in my Openhab application, but sometimes the previous values would be sent and sometimes a mix of new and previous values would be sent in the email.
The screenshot below shows what I mean… but again… my openhab shows all the updated values, it is just that the mail does not always do that. In all honesty, Sometimes it is the first half, sometimes the last, but maybe that is just the inner workings of Openhab


So I guess the Rule is sometimes already sending the email before all my items are updated.
In lieu of a better suggestion I guess the best thing to do is to delay execution of the rule with a few seconds, without ofcourse that holdinng up other processes in my application.

I was wondering how best to do that (I know C++ but not a crack in Java) and if there are other suggestion on maybe I can tackle this, I’d love to hear that too.
Thanks in advance

Maybe it’s enough to put a

Thread::sleep(1000) // time in milliseconds

at the beginning of the rule to give the JSONPATH transformation enough time to execute.

Great, thanks. I am wondering though… will that put my entire openhab to sleep and thus maybe cause update or signaling problems somewhere else?

No, it just delays the specific rule at the mentioned point.

great, thanks

OK with a 1000msec sleep (I put it in between the postUpdate and the sendmail), my recent mail was OK, all updated values shown.
I will keep an eye on it to see if it remains as such,

Update: Seems to go fine
More Update Two days now, reporting every hour. All is well. Thanks again

It probably will work most of the time but occasionally still be wrong. The item updates and the rule execution are executed in concurrent execution threads. Also each rule execution is executed in its own thread. That’s why the sleep in the rule doesn’t block openHAB or other rules. It also means that you can’t assume that a series of MQTT messages are processed in the order they were sent.

Thanks, that is good to know and it explains a lot. Will keep that in mind for other time critical rules and processes.
In this case I am not really worried about it, it is purely cosmetic. The values in Openhab are correct and any irrigation is determined by that, but it is just the little anally COD perfectionist inside of me that wants the email to be correct as well :slight_smile:Oh well, I’ll learn to live with it. Thanks for explaining.
If any… I can just delay the “measurement ready” MQTT from my ESP to come a minute later as I am sure everything will be updated by then :slight_smile:

Update: Seems to go fine
More Update Two days now, reporting every hour. All is well. Thanks again