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
hmerk
(Hans-Jörg Merk)
September 30, 2017, 9:42pm
2
Remove { } cause they are not needed.
rossko57
(Rossko57)
September 30, 2017, 10:14pm
3
You could use a group based approach, writing a rule that operates on members of a group ‘gOffAtStart’ or similar, iterating through group members.
Edit: Updates for OH 4, inclusion of JS Scripting and Blockly examples, removed the doors example as it’s superfluous.
Please see Design Pattern: What is a Design Pattern and How Do I Use Them for how to read and use DPs.
Problem Statement
One powerful way to consolidate code in rules is to use array/list manipulation operations on the members of a Group. For example, filter down to just those members that are ON, get a list of the labels of those Items that are NULL, etc.
Concept
[image]
T…
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
rlkoshak
(Rich Koshak)
October 1, 2017, 11:51pm
6
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.