Hi,
Thanks for all the pointers…valuable lesson learnt, never code whilst tired…
Fresh eyes this morning and it’s sorted:
import org.eclipse.smarthome.model.script.ScriptServiceUtil
import java.util.concurrent.ConcurrentLinkedQueue
val logName = "GateKeeper"
val deviceQueue = new ConcurrentLinkedQueue()
var Timer timer = null
val delay = 1000 // Delay in milliseconds between commands
// GK for Living Room Lights Brightness
rule "GK Living Room Lights Brightness"
when
Item vLightsGatekeeper_B received command
then
// Add the commands to the Queue
val GKcommand = vLightsGatekeeper_B.state.toString
logInfo(logName, "GK_B GKCommand - " + GKcommand)
val groupName = GKcommand.split("#").get(0)
logInfo(logName, "GK_B groupName - " + groupName)
val cmd = GKcommand.split("#").get(1)
logInfo(logName, "GK_B cmd - " + cmd)
val group = ScriptServiceUtil.getItemRegistry.getItem(groupName) as GroupItem
logInfo(logName, "GK_B group - " + group)
// Get group members
group.members.forEach[ light | deviceQueue.add(light) ]
// Timer
if(timer === null)
{
timer = createTimer(now.plusMillis(delay), [ |
if(deviceQueue.peek !== null)
{
val light = deviceQueue.poll
sendCommand(light,cmd)
logInfo(logName, "deviceQueue - " + light + cmd + " - SENT")
timer.reschedule(now.plusMillis(delay))
}
else
{
timer = null
}
])
}
end
Trimmed it down a little and added a delay variable so I can control the delay rather than hard coding it in via a time calculation.
All polls correctly, I had to declare the group as a GroupItem for it to pass the members and I will probably comment out all the logs at some point when it’s all working and just leave the final one in but that’s for a little later on.
Next question, I have 2 commands to pass each member, brightness and ColorTemp, would it make sense to create a duplicate rule for ColorTemp to do the same process or am I at risk of undoing the whole point of the timer here, if I have 2 timers running at the same time I’ll have twice the commands thrown at the hue api and so back to square 1.
Should I just append the ColorTemp part to the bottom of this rule so that the system sorts out the brightness and then sorts out the color temp, or is there a better way?