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