Storing item states for later recall

Hello All,
I’m trying to work on what I think is a cool idea for my OpenHAB setup. I have a lot of switch items and a lot of dimmer items in my Items file and I’m working on setting up reliable presence detection. Once I have that working what I’d like to do is set up a rule that, when I leave the house, stores the state of each item before setting it to its away state. Then on return I’d like the states to be restored. The issue I’m running into is I don’t know how to store the state of these items. I’d also like to be able to store states as preferences for me and my significant other.

Has anyone figured out a good way to store items states without creating a second copy of the items in the Items files? I’m using OH 1.8.3 and anyone’s experience is appreciated.

Thanks!
Matt

I’ll provide a couple of helpful links because this topic has been discussed at nauseum, but I understand that sometimes it’s hard to find links to good documentation!

Once you’ve looked at these resources and have specific questions feel free to post and there are many people here that will be able to help!

Great! Thanks! Yeah I tried searching the topics and the documentation but got a little lost. Thanks for the help!

@jflarente’s answer will get you part of the way to your goal. The part that will be missing is setting up preferences. You have a couple of options to do this and it depends on whether you want to easily be able to edit the presets from your sitemap or not.

If you are OK with hard coding the presets, you can use global vals in your Rules.

However, if you want to change the presets from your sitemap, you will have to create a separate Item for each preset.

I’m thinking about implementing something similar. What do you think about recording in a variable the time/date that you want to save and recalling from persistence to set those items back to the desired values?

For Example:

  1. Before setting to away, a rule records the time/date that you want to recall in a variable
  2. Set items to desired away mode.
  3. When you return home, another rule recalls from persistence database the states by using the time/date recorded by using item.historicState(time/date)

That is a good way to do it. Just be wary of which persistence engine you use for this. If you use rrd4j, for example, as that time gets further into the past the values may be changed as rrd4j “compresses” the data. This wouldn’t work with MapDB at all because it only saves the most recent value.

@mladams922
If you want to study a more complicated example, here there is something that you could reuse/adapt. Look for the LEARN function in the rule: this function save status of all lights, in order to reuse it later.

In the rule code search for :

// SAVE ACTUAL LIGHTS STATUS TO SELECTED SCENE 	

https://community.openhab.org/t/configurable-scenarios/10140/3

2 Likes

Thanks for the good replies everyone!

So I’ve read through a lot of the documentation and I get the idea of what I need to do. I only had one question regarding how to trigger the persistence. I only want it to persist when I trigger it, not on a cron expression, or every change, or restore on startup. Do I need to enter a strategy? Or can I just use the item.persist() method and not specify a strategy?

Thanks!

@villaRob That example is amazing. Have you amended the code at all since April? Very nice work.

You’ve inspired me to have a go at adding some (all?) of that functionality into my system. It really would be nice to have the house behave differently for my wife than for me.

Yes, I rewrote all my rule using jython and jsr223 engine, but the concept has remained the same…

Do not enter a strategy and use item.persist() method. NOTE that if you don’t want to use your default persistence for this you need to specify which persistence engine in the call, e.g. item.persist(“mysql”).

Awesome. Thanks for the replies and the awesome demo’s and documentation! I’m gonna give it a shot and see what I can come up with.

Matt