-
-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for persisting state with timestamp #335
Comments
Did you try something like:
|
@mherwege Is right, this should work. I only have to find a way to document the overrides in the JSDoc and need to update the README |
@mherwege, @florian-h05 - thanks for the guidance. On first attempt I got "ReferenceError: "PersistedState" is not defined: ReferenceError: "PersistedState" is not defined", so will need to play with it some more. @florian-h05, do you want to keep this issue for reference when improving the documentation, or should I close it (at least when figuring out how to do this). |
Sorry I didn’t read the code well enough, Marks code won‘t work. @jlaur Your code didn’t fail like expected because you tried to persist a Quantity, which is casted to a QuantityType (which implements State) by GraalJS. However would it fail in most other cases because you don’t create a Java State inside JS - for better scripting support it would be great to have a persist method that accepts a stateAsString similar to https://www.openhab.org/javadoc/latest/org/openhab/core/automation/module/script/defaultscope/scriptbusevent#postUpdate(org.openhab.core.items.Item,java.lang.String). However do I wonder why it persisted with wrong timestamp in your case. |
@florian-h05 - I tried a few things, but nothing so far worked. Inspired by what you wrote, same but more explicit: var { QuantityType } = require("@runtime");
// (...)
var state = new QuantityType(totalPrice.toString());
items.Energi_Data_Service_Total_Price.persistence.persist(spotPrice.timestamp, state); As expected, this also persists with "now" as timestamp. Now I'm wondering if the provided JS Joda openhab-js/src/items/item-persistence.js Lines 152 to 154 in a53216a
|
It seems to make no difference, same result with: const ZonedDateTime = Java.type('java.time.ZonedDateTime');
var state = new QuantityType(totalPrice.toString());
var timestamp = ZonedDateTime.parse(spotPrice.timestamp.toString());
items.Energi_Data_Service_Total_Price.persistence.persist(timestamp, state); |
@jlaur I would suspect that there is a bug in core or in the persistence service itself, the JS-Joda I have verified this with this simple test (run at 12:18): items.getItem('test_uom').persistence.persist(time.toZDT().plusMinutes(5), Quantity('1 m')) Now attach a debugger to openHAB and put a breakpoint into |
What persistence service is being used for this? |
I have just tried this on my dev instance without having an modifiable persistence service, if I am back home I can try with InfluxDB. |
@jlaur What persistence service do you use for this? The actual storing is done in the persistence service, and the persistence extension passes it on the the persistence service. |
@mherwege - I'm sorry if I'm causing unneeded confusion here, but now I'm even more confused. I'm using JDBC persistence with MySQL. I even just double-checked the code without finding any issue. Now going through the steps with debug logging enabled. First, in the console I did: openhab:update Energi_Data_Service_Total_Price 5 then back to the original code more or less: var totalPrice = spotPrice.quantityState.add(gridTariff.quantityState);
console.log("Now trying to store total price " + totalPrice + " with timestamp " + spotPrice.timestamp);
items.Energi_Data_Service_Total_Price.persistence.persist(spotPrice.timestamp, totalPrice); Result:
So I may already have made a mistake here: items.Energi_Data_Service_Total_Price.persistence.persist(spotPrice.timestamp, totalPrice); because it seems to actually persist the current value, not the provided one. In the logs "state=5 DKK/kWh date=null", so no new value and no timestamp. I'll try to track when happens in core. And now, when preparing one last thing for this comment, I might have found the issue to be caused by that bug you just fixed in core. If this method: Or this method: is actually called (i.e. the alias overloads), this is very likely to be the reason, since it uses current state and null as timestamp. This is the method that is expected to be called: |
Thanks for the core fix, @mherwege. And sorry for creating the issue in this repository, @florian-h05, when actually it seems not related at all. I think only the documentation could use a small update adding the persist overload with timestamp. Feel free to close the issue. |
I will update the docs and then close the issue 👍 |
@jlaur So it looks like a combination of bugs in 2 places. I think you would want to pass on the date and state in this call: https://github.com/openhab/openhab-addons/blob/43fa2c77680bf71dd554e1dcdbcae343851cd7c0/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/JdbcPersistenceService.java#L157 To me it doesn't make much sense to store the current state at the current time when called with the wrong arguments. Or you store nothing and log a warning, or you ignore the alias (maybe logging a warning) and store the item at the set timestamp and date. But if JDBC just would have ignored the alias, the bug would have gone unnoticed. So thank you for it. |
Closes #335. Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Agreed. This was a regression introduced in 4.1. I have provided a fix in openhab/openhab-addons#16845. |
@florian-h05 - oh, one last thing. 🙂 Do you have any current plans for this: openhab-js/src/items/item-persistence.js Line 168 in d81a61f
? For the use-case described in this issue, it actually would have been better to persist everything at once by preparing a time series. |
In core it is now possible to persist state with a given timestamp:
https://www.openhab.org/javadoc/latest/org/openhab/core/persistence/extensions/persistenceextensions#persist(org.openhab.core.items.Item,java.time.ZonedDateTime,org.openhab.core.types.State,java.lang.String)
I don't know if this is actually missing in openhab-js, or if it's only the documentation which is lacking (only
.persist(serviceId)
is mentioned): https://next.openhab.org/addons/automation/jsscripting/#itempersistenceLooking at https://openhab.github.io/openhab-js/items.ItemPersistence.html#persist it looks like it might be possible, so I did this attempt which was expected not to work because I didn't know how to create a
State
from scratch:However, it didn't fail like expected, but instead persisted the quantity value with current timestamp:
Because of openhab/openhab-core#3869 it would be really nice to be able to persist timestamped states.
Your Environment
The text was updated successfully, but these errors were encountered: