Filtering a group and storing it in a variable

  • openHAB version: 2.5.2-1 (released version)

Currently I am using Insteon as my lightning system and it works correctly with OH. However when the power goes out and comes back the lights turn on even though they were off before the power outage. OH does recognice these lights that are ON what I want to do is to notify me in case I;m not home when OH is back up again and also send me the number of lights on.

I found this post where they manage to count the lights turned on, however I have used it on a rule in the following way.

rule “reboot”
when
System started
then
var luceson = {{(itemsInGroup(‘gL_LucesSwitches’) | filter:{type:‘Switch’, state:‘ON’}).length + (itemsInGroup(‘gL_LucesDimmers’) | filter:{type:‘Dimmer’, state:‘!0’}:true).length}}
sendBroadcastNotification(“¡Tú smarthome está devuelta nuevamente!”+luceson)

end

However the following error appears

2020-09-24 15:47:57.609 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘notificaciones.rules’ has errors, therefore ignoring it: [68,55]: no viable alternative at input ‘|’

[68,57]: missing ‘)’ at ‘filter’

[68,63]: mismatched input ‘:’ expecting ‘}’

[68,69]: mismatched input ‘:’ expecting ‘}’

[68,78]: mismatched input ‘,’ expecting ‘}’

[68,85]: mismatched input ‘:’ expecting ‘end’

My groups are as follow:
Items:

Group:Switch gL_LucesSwitches
Group:Dimmer gL_LucesDimmers

If I understand correctly this is not the format in which I have to write it down to store it in a variable. How can I accomplish this?

Any other reccomendation is appreciated. Thank you!

Would you rather setup persistance to have the lights be in the state that they were in when the power went out?

That post is in the HABPanel category. That is JavaScript web page code and is completely invalid syntax for Rules DSL. See Design Pattern: Working with Groups in Rules

1 Like

This might be an interesting solution.

I have persistances setup.

However once openhab is up and running the status has already changed to ON both in my OH Device and in my insteon controller, I would believe that is the state currently saved to be persisted. How am I able to get that persistance before the power outage? my file looks like this.

//mapdb.persist

// persistence strategies have a name and definition and are referred to in the “Items” section
Strategies {
everyHour : “0 0 * * * ?”
everyDay : “0 0 0 * * ?”

    // if no strategy is specified for an Item entry below, the default list will be used
   default = everyUpdate

}

/*

  • Each line in this section defines for which Item(s) which strategy(ies) should be applied.
  • You can list single items, use “" for all items or "groupitem” for all members of a group
  • Item (excl. the group Item itself).
    */
    Items {
    // persist on every change and restore them from the db at startup
    * : strategy = everyUpdate, restoreOnStartup
    }

I tried this

rule “reboot”
when
System started
then
var luceson = (gL_LucesSwitches.members.filter[ i | state==ON ]).length + (gL_LucesDimmers.members.filter[ i | state==!0 ]).length
sendBroadcastNotification(“¡Tú smarthome está devuelta nuevamente!”+luceson)

end

and got the following error.

2020-09-24 19:03:06.580 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Error during the execution of startup rule ‘reboot’: null

Change mapdb.persist file to only contain

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

Yes, where you use strategies like everyChange or everyMinute you risk having “the system” trampling over the data you want.
One avoidance is not use those strategies. You can persist entirely on demand from a rule. The trick then becomes knowing when to persist. “Just before the power fails” is difficult.

If you want your Items strategy-persisted for other business like charting, you could have special Items for persist-on-demand use.

While that’s all an interesting problem to tackle …

this is just bad behaviour at the Insteon end, that you are trying to sticking-plaster over. Why isn’t that fixable?

In a map you can reference the state of the item the way you are trying to here but I don’t know if it works in filters.

First, try breaking it up into multiple lines and log as you go so you can see where the failure occurs.

Then follow the examples in the DP for filter.

val switches = gL_LucesSwitches.members.filter[ i | i.state == ON ]
logInfo("Test", "Filter returned switches " + switches)
val dimmers = gL_LucesDimmers.members.filter[ i | i.getStateAs(OnOffType) == ON ]
logInfo("Test", "Filter returned dimmers " + dimmers)
val luceson = switches.length + dimmers.length
logInfo("Test", "Total number on is " + luceson)

James and rossko57 have you handled for the persistence question.