openhab / openhab-js

openHAB JavaScript Library for JavaScript Scripting Automation
https://www.openhab.org/addons/automation/jsscripting/
Eclipse Public License 2.0
38 stars 31 forks source link

history.persist writes incorrect value #318

Closed mtraxiot closed 5 months ago

mtraxiot commented 5 months ago

Expected Behavior

Persist function should write current value to SQL database

Current Behavior

persist is writting zero to mysql database

Possible Solution

workaround fix to add short delay after persist call java.lang.Thread.sleep(100);

Steps to Reproduce (for Bugs)

in Automation rule I update this value every hour eg rules.when().item("Energy_Mains_Total_minute").receivedUpdate().then(event => { ... // write value every hour if (new Date().getMinutes() == 59 { Energy_Mains_Hourly.history.persist(); // reset for next hour // java.lang.Thread.sleep(100); // <=== add delay to allow persist to work Energy_Mains_Hourly.postUpdate(0); // <=== Update item }

Context

I'm writting values of my energy consumption every hour ( just before the hour) and resetting

Your Environment

running Openhab 4.1 openhab-js version 4.7.2 running on Ubuntu 22.04.3 LTS

florian-h05 commented 5 months ago

As explained in https://community.openhab.org/t/openhab-4-1-release-discussion/152252/145?u=florian-h05, this is "intended" behaviour due to the fact that persist is (and has to be) asynchronous.

The library code will not add a Thread.sleep anywhere, in case you need that, please add that to your code as you did.

I will improve the docs to note that persist is asynchronous and warn for the side effects.