All Switch to Off on Startup

On startup I’ve like all my switch items to turn off (ie also go Red on GUI in Basic). I created a script, but it doesnt actually work. Anyone know what I’ve missed?

rule “Default Status”

when

System started

then

{
TimeStateOvernight.sendCommand(OFF)
TimeStateEarlyMorning.sendCommand(OFF)
TimeStateMorning.sendCommand(OFF)
TimeStateMidday.sendCommand(OFF)
TimeStateAfternoon.sendCommand(OFF)
TimeStateEarlyEvening.sendCommand(OFF)
TimeStateEvening.sendCommand(OFF)
TimeStateLateEvening.sendCommand(OFF)

TimeStateBoilerOveride.sendCommand(OFF)

PresenceKIT.sendCommand(OFF)
PresenceLVR.sendCommand(OFF)
PresenceHAL.sendCommand(OFF)
PresenceSSB.sendCommand(OFF)
PresenceGB2.sendCommand(OFF)
PresenceGB3.sendCommand(OFF)
PresenceBR1.sendCommand(OFF)
PresenceBR2.sendCommand(OFF)
PresencePOR.sendCommand(OFF)
PresenceFGD.sendCommand(OFF)
PresenceRGD.sendCommand(OFF)
PresenceDSB.sendCommand(OFF)
PresenceDST.sendCommand(OFF)
PresenceDSH.sendCommand(OFF)
PresenceDSK.sendCommand(OFF)
PresenceDSL.sendCommand(OFF)
PresenceDSG.sendCommand(OFF)
PresenceMHG.sendCommand(OFF)
PresenceGEN.sendCommand(OFF)

}

end

Remove { } cause they are not needed.

You could use a group based approach, writing a rule that operates on members of a group ‘gOffAtStart’ or similar, iterating through group members.

That would also easily allow you to add a small delay between items, rather than flooding OH event bus

I haven’t really added any groups to anything in items. What would you say are the real benefits are I couldn’t really see one - it’s only really useful for short cutting turning things off together isn’t it?

Please compare this code with your code:

rule "OFF @ startup"
when
    System started
then
    gAllOff.forEach[i|
        i.sendCommand(OFF)
    ]
end

To be honest, you would have to add one line in an .items file

Group:Switch gAllOff

plus a (gAllOff) to all Items which should be set to OFF on startup.

But I guess you got the point :wink:

You can do even better if all the members accept OFF then you just need

gAllOff.sendCommand(OFF)

Clearly you haven’t read the link rossko provided nor any of the other design patterns. Groups are vital to the writing of generic, concise, and maintainable rules.