[SOLVED] Rules at startup

hello,

verry beginner with openhab, i would like a rule who initialize all states of my light when i stop and restart openhab, now when i restart openhab all of my light are of, or in my house their is a light on.
my system is knx

i search an i find this rule but she doesn’t work.

rule "Initialize light states"
when
System started
then
Lights?.members.forEach(light|
light.postUpdate(if(Math::random > 0.7) ON else OFF)
)
end

could you tell me where i can find the good rules.

thanks a lot

Its not clear to me what the system startup state that you are after.

The rule you have provided will only update OH state of the bulb to a random state of ON or OFF.
Are you actually trying to get OHto discovery the current state of the lights and then set its local state to match?

Or simply turn all lights OFF when you restart OH, which could be dangerous.

Or turn all lights ON when OH started which again could be dangerous.

I am not sure if the KNX system allows for the bulb to be interrogated for its current status, if it is possible then I would think at system state up, you would want to discovery the current state of the bulbs and get OH to reflect it.

Regards

Paul

hello paul,

yes i would like to get OH to discovery the current state of the lights. today after restart OH in habpannel, all lights are off. i must switch off manualy all my lights for OH learn the states.

the rule that i found is wrong. perhaps make a rule who ask the status of every lights every five minutes

i need this rule just if my computer crash or if i want change anything in the files of OH.

i hope i’m clear it’s not alway easy in english.

bruce

Did you think about persistence?

no, when i search in the forum with “initialise states at startup” i don’t found persistence.

could say me more about that or tuto

thanks.

i found that

Strategies {
  default = everyUpdate
}

Items {
  * : strategy = everyChange, restoreOnStartup
}

but it doesn’t work (it will be so easy :grin:)

You will need to setup a persistence database type that supports restoreOnStartup.

Setting up persistence will allow for OH to set the lights back to how it last knew they were before it shutdown/crashed. this may not be how you still have them when you are starting up OH.

I think you need to consider all the different use cases before you decide on a single strategy. I also think its actually a difficult issue to solve that meets all use cases, I certainly do not use persistence for lights.

If KNX supported obtaining the current status then that would be the most accurate way to proceed. Can someone wityh KNX experience please comment?

hello,

thank you all and especialy vzorglubVincent Regaud who help me.

in fact he said me to install persistence mapdb in paperui, create a folder mapdb.persist in conf/persistence and put in this file :

// mapdb persistence

Strategies {
	default = everyChange
}
Items {
	* : strategy = everyChange, restoreOnStartup
}

and restart openhab

have a good day

bruce