Jython "Settings page" sitemap savebutton persistence

for persistence I use MAPDB
working on a sitemap to keep settings (start and end time to turning on/off lightsbulbs)
What I have:
a sitemap with virtual items endhour, endminute (setpoint item=v_start_hour label:"…)
a virtual switch item visibility ON when it’s value is ON and mappings=[ON=“Save”]
When endhour or endminute changes, I do a postupdate of that switch item to ON, so it becomes visible.
When I click on the virtual switch, I postupdate the virtual switch to OFF, so it disappears again.

so far so good.
What I would like to achieve :
only when I click on the virtual switch, save the values of the endhour and endminute (persistent)
so, when I change the endhour or endminute, and go away of the settings page, wthout clicking virtual switch, i shouldn’t be saved…
How can I do that ? Can anyone show me the way or are there some similar examples?

Since this isn’t itself a tutorial or solution I’ve moved it to a more appropriate category.

Remove the hour and minute Items from being saved to MapDB automatically.

Trigger a Rule when your save Switch receives a command. In that Rule call HourItem.persist("mapdb") and MinuteItem.persist("mapdb") to save their current state to the database. Obviously, use the actual names of your Items.

This gives you the control over when the Items are saved. You can keep the Items configured with restoreOnStartup if required, just remove them from being saved by a policy in mapdb.persist and only let the Rule save their states.

thx! gonna try that!
after a lot of searching:
using Jython
apparently I have to add
from org.eclipse.smarthome.model.persistence.extensions import PersistenceExtensions


Did some testing:
HourItem (veto autoupdate), setpoint minValue0 MaxValue=23 step=1
Save button: switch item=savbutton label=“Save” ocon=“save” mappings=[ON='Save"]
Jython: when HourItem received command, do a postUpdate of the item.
(reason: first I thought, when I have vetoUpdate, and I go away from the page, and come back, I have the original value. But, that didn’t work. No value was displayed in the sitemap. Only a postUpdate (or I guess Force Update) displays a value.
=> change value of HourItem (using the sitemap), … postUpdate happens, switch savebutton is turned ON (value of HourItem=12)
=> Save the value by clicking savebutton=> PersistenceExtensions.persist(HourItem) executed.
then … change value of HourItem again (using sitemap) postUpdate happens, switch savebutton is turned ON (value of HourItem=13)
=> NOT clicking savebutton, but restarting OPENHAB
when openhab is restarted, the value in the sitemap of Houritem =12.
so, this works…
next problem: How to retrieve the persited value from mapdb and put it back?
say… I change the value of HourItem, save it, change it again, and don’t save it. The item still has the last changed value, which is not the value that is in mapdb until a restart of openhab is done… Is there a solution for it?

Calling previousState will pull a HistoricState from the database so you can get at the value in the database using


then you can update the Item with that state.

The gotcha is how to trigger such a Rule. No events get generated when you navigate BasicUI so your best bet I think is to have a reset Switch that you manually toggle to cause this restoration.

Was trying the same :slight_smile: Seems to work. you’re right, I guess I’ll have to use a reset switch.
Thanks for your help !

This will work, but the package name will change in OH 3.0, along with many others. You could use the core.actions module to simplify things…

from core.actions import PersistenceExtensions


that’s very interesting info.

About OH3: While searching the internet about openhab… I’ve found some articles and nice screenshots of OH3
I guess there’s no image yet I can just install like for oh2 on raspberry.
Did someone already made an install document ? Would love to play with it…even if it has still errors…