-
-
Notifications
You must be signed in to change notification settings - Fork 428
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 getLastStateChange
, getLastStateUpdate
, and getLastState
to GenericItem
#4351
base: main
Are you sure you want to change the base?
Conversation
2cadc5e
to
75bc9c9
Compare
I like the idea of this information being part of the Item. Of course it won't go back beyond OH startup but something is better than nothing. It certainly handles this use car better than persistence I think given all the different ways perspective can be configured. How does this interact with restoreOnStartup? If an item is restored and not changed does this return the restored time or null? What makes the most sense? I'm not sure I know (it's close to bed time here so maybe I'll sleep on it). |
75bc9c9
to
0c7eeed
Compare
It is not aware of persistence restoration, so I guess once persistence restores the state, it will return the time the state was restored. |
Imho it should be possible to query these values from the db so restore on startup should work as expected. |
@spacemanspiff2007 would you mind elaborating this please?
Good point. I will add them there too. |
It's possible to query the last persisted state from persistence and subsequently restore this state as the current state on startup. It's also possible to query the persisted state before that. |
Yes, isn't this what
I don't quite understand the above statements. This PR doesn't interfere with persistence's |
I think it's a misunderstanding. |
…icItem Signed-off-by: Jimmy Tanagra <[email protected]>
0c7eeed
to
178df65
Compare
@spacemanspiff2007 I've added it to the REST response {
"link": "http://192.168.1.10:8484/rest/items/TestSwitch1",
"state": "ON",
"previousState": "NULL",
"lastUpdate": 1723872081231,
"lastChange": 1723871965049,
"editable": false,
"type": "Switch",
"name": "TestSwitch1",
"tags": [],
"groupNames": [
"TestSwitches"
]
} |
That depends on the persistence service. And it is not the case for mapDB, the most used persistence service for restoreOnStartup. I would argue that, when the item state is updated as a consequence of restoreOnStartup, it should not update these lastChange and lastUpdate fields because now is probably the wrong time. But I am not sure this is feasible. I doubt the item knows anything about the source of the update/change. In general, I think having the extra fields is a good idea. I just feel like we are still not there to give an answer to a simple question: when did my item last change?
In an ideal world, mapDB would just store the lastChange time of an item with the state and we could restore that together with the state. I am just not sure it is easily feasible. We can keep adding layers to this, but there are always issues and exceptions. I like the idea of keeping lastChange and lastUpdate with the item. But I think the real simplification comes when we have a simpler restore mechanism on startup. Because than, you always get the right answer. Other persistence services then have the focus on data for graphing and calculations, which makes sense in my view. |
OK, I think I now understand what @spacemanspiff2007 was alluding to, thanks to @mherwege's explanation.
Not a bad idea. |
Why would this not be feasible? I beleive all the apis already exists in persistence to get the time from persistence. Once you have that it's just a matter of adding the last change and last update time as a parameter on the Item.
I'm not sure we can make that case that's it's always only used for restoreOnStartup. It's useable if you only care about the most recent change to the Item in a rule (given it's current default strategies). One could change the strategies and it would work for most recent update. But if these properties are added to the Item and we go down a path where those are made available and accurate on the the Item this use case is handled.
Be careful here. Not all uses want all Items restored. Not all users want all Items restored to their most recent change or update (e.g they use a rule with .persist to control when the state gets saved). These use cases are now currently supported. Any move of restoreOnStartup to core would need to handle these use cases too. How the Item's state is saved and which Items are restored needs to be configurable on an Item by Item basis like it is now in persistence. |
It would be trivial to modify mapDB persistence service to save the additional three values. A quick glance at the docs shows it should be possible. If not it's always possible to name mangle additional values with a separator that's not a valid item name (e.g.
No - it should return
I strongly agree. For many devices that report the state themselves I explicitly do not save the item state so I don't have the wrong state after startup. If the restore makes sense depends strongly on the device, the user and how the rules are written. Just a hint: That would also result in a proper behavior when restoring from persistence.
So I am basically suggesting the opposite of what I wrote above 🙈 🤣 |
Each item can have a special metadata |
I think, what @rkoshak also implied is that one may want to use different rules for persisting values, not just on every change, but e.g. persist it once and always use that same value at startup. And that would mean we also need to support that part of the configuration in core. And then we are back to doing it the way it is done now anyway. Thinking further about it:
With the above logic in place, it also would make sense to revisit the persistence extension actions to not return null, but return what is actually in the database, and document it as a breaking change, advising users to use the Item methods for this use case. |
Looking at MapDB code, it is actually not that difficult. The item state and time are already stored as a JSON string. Adding extra fields should not be a problem. |
Since a user needs a rule for calling This new core service doesn't need to do all the use cases. We just need to ensure that all the use cases are covered somehow. With #4324 I think we will reliably be able to count on persistence being there by runlevel 100. So maybe a system started rule with
Maybe we are thinking too hard about this. What do we intend these new properties to represent? I see two options:
If it's just 1, we don't really need to worry about most of these complexities. If an Item doesn't change after OH startup, If we want to make 2 happen then it gets much more complicated, as we can see and we probably need something built into core. But do we really need to solve 2 here? Maybe 1 is sufficient? I think users who prefer not to use persistence at all would love to have 1. We also need to thing of second tier impacts like the increase in writes for those running on SD cards and the like if this core restoreOnStartup feature becomes the default behavior. |
So what's the next step?
If we are going to proceed in this direction, I would suggest:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
I am fine with this. |
I think this is the best way, too. |
This pull request has been mentioned on openHAB Community. There might be relevant details there: https://community.openhab.org/t/lastupdate-and-null-values/158134/6 |
This pull request has been mentioned on openHAB Community. There might be relevant details there: https://community.openhab.org/t/how-to-monitor-if-item-is-frequently-updated/158260/6 |
how do we resolve the access to these methods vs the persistence methods with the same name within rulesDSL? |
Maybe I miss something here, but what gets called would be determined by the imports I would think. As both items and persistence are imported by default, it indeed leads to a conflict which will force changing the rules to have a fully qualified name. The options then become:
In both cases, rules would work without being changed, but 2 would have a slight change in behaviour. I would still prefer 2 because it is more natural not to call persistence if not needed. If one absolutely wants the value from persistence the rule would need to be changed, but it would be very clear what is expected. At the same time, the persistence actions could be modified again to not return null, but the last effective persisted value. |
That's no necessarily true. openhab-js can be installed independently so one can have a newer version of openhab-js with an older version of the add-on. Obviously there are limitations to this as breaking changes happen which limit how far back openhab-js can go. But I beleive the goal is to maintain backwards compatability as much as possible. In this case it shouldn't be too hard. I've not looked at the code but I would expect something like the following could work:
|
@jimtng Your outlined impact on JS Scripting is correct, except for what @rkoshak said: Users can install new openhab-js versions on older openHAB versions, so openhab-js needs to provide backward compatibility, but it should be do-able like in Rich's code snippet. A more general though though: IMO it feels wrong to rename a PersistenceExtensions method because there will be a new method on the Item with the same name. It sounds a bit awkward to have I however really love the new functionality provided by this PR - the architecture has to be decided by the core maintainers. |
I agree with this. The change of name is purely for the benefit of rulesdsl. Is there another way to solve it? |
Probably have a wrapper class of the persistence extensions for rules DSL so rules DSL can have non-colliding names. |
Do you know a way to do that without breaking all persistence extensions? At the moment, they are just called with itemName.methodName syntax, whereby itemName is the first argument and this is standard Xtend. And of course that forces use of unique names with item methods or there would be a name conflict (both persistence extensions and methods on items are imported by default). It would have been cleaner to be explicit from the start in the call about using persistence, but it is not. And I don’t like the idea to introduce something different just for these methods. Everything is solved if we use another name for the new methods, different from what is proposed. But I don’t know anything that is clearer. And in DSL context, it would make a lot of sense to rename the old methods on persistence to make the difference in usage very clear and obvious. Doing so even allows old DSL rules to run without change as they will get the same info from the item rather than persistence. |
Here's an idea: how about removing them from persistence. |
My proposal with the wrapper class was targeting at that. I should probably have stated clearer what I meant to say. I my proposal is to leave the PersistenceExtensions as they are, i.e. not rename methods, and introduce a new class just for rules DSL that just maps its method calls to the PersistenceExtensions. This way we decouple the method naming in rules DSL from the PersistenceExtensions. |
Sorry, it is probably me, but I still don’t get it. Instead of using the PersistenceExtensions class it would be possible to use a wrapper class that calls the methods in PersistenceExtensions. It would need to duplicate all of the static methods in PersistenceExtensions with calls to these (and most are only there for DSL, all the parameter variations). That new wrapper class would then be used for DSL. The methods would then be renamed for DSL in this wrapper class to avoid the naming conflict caused by Xtend. Is this what you mean? And if I understand it well (I don’t use jruby myself), just like in DSL and unlike JS, persistence methods are directly on the item in jruby. Also there, it is not immediately clear where the response comes from if there is some overlap in the function. So something similar needs to be put in place there. |
That’s very drastic and completely ignores use cases where it might actually be useful to get this info from persistence. Although I must admit that all use cases I have will be served much better with the new item level methods and I will not use the PersistenceExtension methods for this. Dropping or renaming will have the same effect. |
Whilst in JRuby the PersistenceExtension methods are attached directly to the item object, this is done selectively and explicitly by the helper library. It is easy to ensure that the call goes to the GenericItem instead of PersistenceExtensions and it can also rename the methods from PersistenceExtensions if needed. So there's zero issues with JRuby. With my proposal for dropping the methods from PersistenceExtensions:
Sure, we'll lose the functionality from persistence, but would anyone really miss them? A wrapper class cannot remove the need for the various permutations of arguments for every method, because that would be API breaking. However, It would double the maintenance chore for PersistenceExtensions, which is already huge as it is, as I'm sure @mherwege is well aware. Dropping/renaming them would also be API-breaking, I guess. |
The one thing we would need to solve at the same time then is to restore lastChange, lastUpdate and previousState from persistence at startup. I know how to do it, and it is possible to enhance mapDB to support it. In that case, I don't see a disadvantage going that way. I was already working on a PR to change the persistence extension methods. One thing I stumbled upon is that we do not only have I would like some input from a core maintainer here. Would it be OK to drop some methods from persistence if they get replaced by equivalent item methods offering more consistent results? We believe the API changes will be minimal, no impact on DSL, and can be hidden from the end users by changing the jruby and JS helper libraries. |
I've just realised, it's not that simple, because we have variants of these methods that take an extra argument for serviceId. In light of this, perhaps the easiest avenue is to rename the new GenericItem methods. It would avoid all compatibility problems. The only catch is, to benefit from the new methods, users would need to modify their scripts. However, I haven't got an idea what the new method names should be. |
Proposal for naming the GenericItem methods in this PR: |
Indeed. But then again, the reason to sometime do that is to force use of another persistence service to get these values if we know the default service (like e.g. RDB4J) does not provide accurate enough info. If it is being used that way, I would argue it would still be better to change the script and use the item method. The helper methods could ignore serviceId. The Persistence Extensions could actually also call the item methods and ignore the serviceId in that case. I would prefer it over making the name of the new methods more complex. |
And there is another complicating factor. The PersistenceExtensions previousState methods return a HistoricItem, not a State. Changing that will force changes in DSL rules anyway. |
So do you agree that renaming the new methods is a better idea? |
Yes, I do in the end. |
getLastChange
, getLastUpdate
, and getPreviousState
to GenericItemgetLastStateChange
, getLastStateUpdate
, and getLastState
to GenericItem
@mherwege Perhaps with no naming changes in persistence, at least now instead of returning null, the persistence method can call the Item's method to fulfil the request when such data is not available from the persistence service. Or another strategy is to try Item's method first, and if it's null (when it was first loaded and still UNDEF), then it can query the persistence service. This will solve the issues linked in the first post above, without requiring users to change their existing scripts! |
Agreed. That makes sense. |
Signed-off-by: Jimmy Tanagra <[email protected]>
9beb230
to
cccb8d0
Compare
I've renamed the methods to getLastStateChange, getLastStateUpdate, and getLastState to avoid conflicts with the PersistenceExtensions methods. There should be no issues going forward now, I hope. I don't think these values need to be "restored on startup" like persistence. If people need that restoration feature, they can call the equivalent PersistenceExtensions method. I am still not 100% satisfied with the naming, but I can't think of any better names. |
You could simply call the original ones 'xyz' and the new ones 'xyz2' ... |
Not required, true. But that means a user has to explicitely create startup rules if they want to do this, or have a check in every rule if the values are there to retrieve from persistence if not. So at some point I would like to have it.
Same for me. That's part of the reason why renaming in the PersistenceExtensions class was attractive. It then clearly conveyed the purpose. Putting '2' behind the name does not solve that either. By lack of better idea, I think what you propose is fine. |
I have created:
|
This pull request has been mentioned on openHAB Community. There might be relevant details there: |
These methods are handy in many situations and isn't possible with mapdb persistence, so previously it would require something like influxdb.
It also solves the issue of needing to wait until persistence write operation is finished (if it was at all set up to persist on change), so this gives us a cheap and reliable / dependable way of getting the last change / last update time.
See discussions in
https://community.openhab.org/t/persistence-on-off-transitions-with-influx/156271/
https://community.openhab.org/t/blockly-persistence-last-changed-returns-no-result-quite-often/157842