Resurrecting this thread (I also moved it to the Scripts & Rules category :)) to ask some simple “technical” questions (for me to understand better the state stuff):
Why do I have to use state.toString
in the following rule:
var int days = 7
rule "Simulate Lights on Vacation"
when
Time cron "0 0/5 * * * ?"
then
if (Vacation_Mode.state == ON) {
gGF_Lights?.members.forEach[item |
logInfo("Vacation",item + " got " + item.historicState(now.minusDays(days)).state)
item.sendCommand(item.historicState(now.minusDays(days)).state.toString)
]
}
end
The above rule works fine (with .state.toString
). It cycles through all (21) members of the gGF_Lights group and “replays” the state from 1 week behind (stored in my default persistence = mapdb)
Without .toString
in the sendCommand line, I get:
2017-07-14 11:05:00.144 [INFO ] [ipse.smarthome.model.script.Vacation] - P03_Wh_Light (Type=SwitchItem, State=OFF, Label=P03 Wh. Light, Category=switch, Groups=[gGF_P03_Warehouse, gGF_Lights, gKNX]) got OFF
2017-07-14 11:05:00.145 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule Simulate Lights on Vacation: Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.sendCommand(org.eclipse.smarthome.core.items.Item,java.lang.String) on instance: null
Interestingly enough, the logInfo entry displays past week’s state without adding .toString
(but it adds alot of not needed info for the item (like Type= etc)… is there a way to filter those out?)
Ps: Also, should I use: gGF_Lights?.members.forEach
or gGF_Lights.members.forEach
in my rule? (I think that with and/or without the question mark, it works)
Ps2: Another type of question here: The rule fires every 5 mins. Is there a way to “enable” such rule only when I flick to “Vacation_Mode” switch to ON? Meaning that the when trigger condition
would change to Item Vacation_Mode changed from OFF to ON
… afterwards… maybe find a way to run the cron job.
I am just thinking to avoid the recurrent execution of the rule when I don’t really need it.
Ps3: OH 2.2.0 Snapshot Build #985
Ps4: Thanx to @rlkoshak & @watou for the examples they provided in this thread. I build my rule based on their inputs.