Initialising items in persistence

Took me quite some time to figurre out.
Had a rule like below and it simply didn’t work. And that was only because of the comparsion
if(k_vent_light.state==OFF)
This item was never switched on or off and is a virtual switch. So the rule dodn’t know what state is it in.
Is there a way to initialishe items for example so that the have a start value of 0/OFF for example?

rule "Button Kitchen Vent light" when Item k_hood_bp received update then if(k_vent_light.state==OFF) { k_vent_light.sendCommand(ON) k_hood_light.sendCommand(ON) } else if (k_vent_light.state==ON) { k_vent_light.sendCommand(OFF) k_hood_light.sendCommand(OFF) } end

Use a “system started” rule and set the desired initial value of the item.

or use persistance, than the value will be updated or reinitialized after system start

Thomas

You also could a more common if clause:

if (k_vent_light.state==ON)
{
    k_vent_light.sendCommand(OFF)
    k_hood_light.sendCommand(OFF)
}
else
{
    k_vent_light.sendCommand(ON)
    k_hood_light.sendCommand(ON)
}
end
1 Like

That is the problem. I have the persistance via mysql.
The problem was that after i created the virtual switch k_vent_light - i never used it froom the web interface (although it is tied to a physical button).

Rules simply didn’t work. Openhab restart didn’t help either. Only after i used the web interface to switch it on and of the magic happened. This is also somehting i see in logs.

If you create the item and it has never been changed - persistance is empty like in the old log

2016-05-22 14:14:57.911 [ERROR] [.p.m.i.MysqlPersistenceService] - mySQL: Unable to find table for query 'k_lg_MediaState'. 2016-05-22 14:14:57.911 [ERROR] [.p.m.i.MysqlPersistenceService] - mySQL: Unable to find table for query 'k_lg_Input'. 2016-05-22 14:14:57.936 [ERROR] [.p.m.i.MysqlPersistenceService] - mySQL: Unable to find table for query 'k_vent_super'. 2016-05-22 14:14:57.936 [ERROR] [.p.m.i.MysqlPersistenceService] - mySQL: Unable to find table for query 'k_vent_semi'.

Yes, you can’t persist an uninitialized :slight_smile:

That is correct. One thing you can do to boot-strap and Item like that is by using a temporary System started rule to initialize the Item the first time after it is created. The persistence can take over and you can remove the System started rule. This is the approach I use for Items that can’t be initialized from the sitemap.

Failing that, you need to do what @Udo_Hartmann suggests and make your conditionals more able to deal with Undefined states.

Shouldn’t it be possible to create a rule like this:
G_Switches all Switches in one Group
G_Numbers all Numbers in one Group

rule "init switches"
when
    system started
then
val String tmp = G_Switches .members.filter [ i | i.state == NULL ]...
and then set all to OFF if state == NULL !?

the same for numbers set to 0