System started trigger

Hi !

I was using system start trigger in a rule, when I initialize variables, but now I recognized that this condition is met on even when I change rules file.

Which trigger should be used to initialize var once, when just system starts on reboot ?

System based triggers in rules

OH has no way to distinguish between its loading the rules files because OH itself restarted or because the file itself has changed. So there is no separate trigger.

Thanks Rich !

So any other way or idea to implement it once, when we start it ?

There is a System shuts down trigger. If you have persistence and always shutdown openHAB properly (i.e. no kill -9) you can create an Item that acts as a flag. Set the flag when System shuts down and it should be restored to that state when openHAB comes back up. In the System started rule check the flag and if it is set you know that openHAB actually shut down instead of just reloaded the files.

NOTE: I’ve not done this. It won’t work if System shut down gets called when rules are reloaded which is feasible. It also won’t work if persistence doesn’t save the values set during a Systems shut down rule. Finally it won’t work if openHAB is shut down in a way the prevents the rule from executing.

Personally, I would either update the rule to handle this case or modify my other code so it doesn’t matter whether the rule is running because of an openHAB restart or a rules edit. Right now I’m hard pressed to think of a reason why I would ever need to distinguish between an openHAB restart or a rule edit in a System started rule.

Answer is easy: I am just starting with OH, so I do many modifications, restart raspberry, etc. So when my config loads I have undefined values, ie temp setpoints, switches. So I simply put initialization rule which is doing those things for me, but now I can see it is not ideal :frowning:

Yesterday I tried to play with persistent and I didn’t find simple parameter /command which I can use to remember a value (before reset) and then recover it during initialization. I have config persistence file with everychange parameter, but can’t recover it. I tried use:
postUpdate(Heater_Counter, Heater_Counter.persist)
but designer tells me that incompatible type and it is void :frowning:

Set up persistence and use a reloadOnStart strategy for all your items. Then what ever state they were in before the restart will be restored automatically. No more undefined items.

You didn’t look at the wiki page for persistence. Use the restoreOnStartup strategy for all your items, as documented in the wiki.

Be sure to review the wiki for all of these sorts of questions. More often then not your answer will be there.

Here is an rrd4j.persist file that will capture everything and restore them on a restart.

Strategies {
    default = everyChange

Items {
    *: strategy = everyChange, restoreOnStartup

That’s because that line makes no sense. Please see the wiki page linked to above for the valid calls you can make that have to do with persistence. Per the wiki, to do what you seem to want to do, save the current state, you would call:


all by itself. But if you set up your persistence like the above you don’t ever have to make this call because any change to your items will automatically be saved. And restored when openHAB restarts.

[Rant] This isn’t really directed solely at you. If everyone would ask how to solve their actual problem as opposed to asking some low level technical question that helps them solve the problem the way they THINK it should be solved it would be a whole lot easier for everyone involved. [End Rant]

I missed this strategy.

I have add restoreOnStartup according your suggestion / wiki and it seems to be working, but I have a problem after service restart via ctrl+c on windows (stop restapi, stop classic UI are executed) and start again.

It seems to do not restore data after such restart, even I used also strategy: everyChange. In result in normal usage, when I just update config I need to del initialization rule (which set particular values for certain variables), otherwise after change and refresh this rule will be executed and variables again will have default values. If I delete this rule I can not restart openhab, because var will be undefined.
Is is some configuration problem that service do not save values and keep them after restart or just openhab keep them as long service is life ?

If you are using rrd4j be aware that it will only store numerical values. So Numbers, Switches, Contacts, etc. will be saved but DateTimes and Strings will not be saved.

If you are trying to restore non-numerical data you need to use a different persistence engine (e.g db4o).

Also, sometimes rrd4j has problems if you don’t save data using an everyMinute strategy. I’ve never not used everyMinute so I can’t say for sure if this is causing your problem or not.

Finally, you need to make sure that your items and persist files are loaded before your rules file. you can adjust the order files get loaded by adjusting the polling period for each file type at the top of your openhab.cfg file.

I’ve changed to db4o. Persistence refresh order I have as follows: items, sitemap, persist, rules - I think it is default.

Still I got same problem, it looks like it works when I just refresh some file, but it doesn’t when I ctrl+c script start script under Win or “service openhab restart” under Linux.

As I understand data should be also recover after service restart, otherwise it makes no sense.

How much time are do you have between the various items in your refresh? You can tell if it isn’t enough if you see a log statement loading a rules file before all of the log statements for the persist files have finished loading. This is my config:


Notice I have 8 seconds between my persist and my rules.

Yes, if you have “restoreOnStartup” strategy in the Items section of your persist file. For example:

* : strategy = everyChange, restoreOnStartup

will save every change to every Item and restore the state of every Item when openHAB starts or a rules file is reloaded.

hmm, hard to say, if it is enough time.
I am loading:

2015-12-29 21:31:16.244 [INFO ] [.o.core.internal.CoreActivator] - openHAB runtime has been started (v1.7.1).
2015-12-29 21:31:21.162 [INFO ] [o.o.i.s.i.DiscoveryServiceImpl] - mDNS service has been started
2015-12-29 21:31:29.685 [INFO ] [c.internal.ModelRepositoryImpl] - Loading model ‘moj.sitemap’
2015-12-29 21:31:31.152 [INFO ] [c.internal.ModelRepositoryImpl] - Loading model ‘db4o.persist’
2015-12-29 21:31:31.217 [INFO ] [o.o.i.s.i.DiscoveryServiceImpl] - Service Discovery initialization completed.
2015-12-29 21:31:31.298 [INFO ] [] - Started REST API at /rest
2015-12-29 21:31:31.343 [INFO ] [c.internal.ModelRepositoryImpl] - Loading model ‘moj.items’
2015-12-29 21:31:41.686 [INFO ] [.o.u.w.i.servlet.WebAppServlet] - Started Classic UI at /
2015-12-29 21:31:51.843 [INFO ] [c.internal.ModelRepositoryImpl] - Loading model ‘moj.rules’

between items and rules, all data was read via bidding, ie Onewire. Then 21:31:54, so +3s one var was set - via init function in rules.

config looks like follows:

Do you mean that not an order but a number (ie 10) in this config tells when load particular module ? I understood that it is executed one by one and a number tells how offen folders will be scan.

yes, the number is how many seconds between polls of the files for changes. it also is how long after startup tge fikes are loaded. so to force tge rulrs to be loaded last it needs a larger number than everything else. though your logs indicate the rules are loading last anyway, but a further delay may be needed to make sure persistence finishes before rules start.

I have changed rules trigger, but still same problem.
Maybe the problem is how I am closing process (ctrl+C on win, and service restart on Linux), but I don’t know more humanitarian method to kill/restart it :smile:
Is it any file where OH writes those data ?

EDIT. I think I did it, when I changed pers. type I forgot to install addons db4o.
Now seems to be working fine, even after ctrl+c or service restart.

Thanks Rich for you help !!!