[OH2] Rule help to play a random sound file

“Oh drat. Back to the drawing board.”

import java.util.concurrent.ThreadLocalRandom;

rule "Lights off at 2300"
when
	Time cron "0 00 23 * * ?"
then
	FrontDoorLight.sendCommand(OFF)
	FrontDoorDecorations.sendCommand(OFF)
	FoyerLanternLoadLevelStatus.sendCommand(0)
	MasonJarLight.sendCommand(OFF)
	PlantAccent.sendCommand(OFF)
	GreatRoomFrontWindowPlug.sendCommand(OFF)
	GreatRoomSideWindowPlug.sendCommand(OFF)
	GreatRoomSideWindowPlug.sendCommand(OFF)
	DiningRoomFrontWindowPlug.sendCommand(OFF)
	MasterBedroomFrontWindowPlug.sendCommand(OFF)
	OfficePlug.sendCommand(OFF)
	FoyerPlug.sendCommand(OFF)

        switch ThreadLocalRandom::current().nextInt(0, 2+ 1): {
            case 0: play AAA.wav
            case 1: play BBB.wav
            case 2: play CCC.wav
        }
end

Note: If you have more than one place that you want to do this I recommend the Separation of Behaviors Design Pattern which will let you code this once and call it from multiple rules (not a lambda).

It would look something like this:

Switch PlayRandom
rule "Play random sound"
when
    Item PlayRandom received command
then
        switch ThreadLocalRandom::current().nextInt(0, 2+ 1): {
            case 0: play AAA.wav
            case 1: play BBB.wav
            case 2: play CCC.wav
        }    
end

And you would call it with

PlayRandom.sendCommand(ON)

Also, just for an idea of what you can do, if you look at my Time of Day Design Pattern for calculating time periods and the following code I use to control my lighting you will see some things you can do to make the rules slight more flexible. I’m not advocating you do it this way, just showing what is possible.

Group:Switch:OR(ON,OFF) gLights_ALL "All Lights"

Group:Switch:OR(ON, OFF) gLights_ON
Group:Switch:OR(ON, OFF) gLights_OFF
Group:Switch:OR(ON, OFF) gLights_ON_MORNING    (gLights_ON)
Group:Switch:OR(ON, OFF) gLights_OFF_MORNING   (gLights_OFF)
Group:Switch:OR(ON, OFF) gLights_ON_DAY        (gLights_ON)
Group:Switch:OR(ON, OFF) gLights_OFF_DAY       (gLights_OFF)
Group:Switch:OR(ON, OFF) gLights_ON_AFTERNOON  (gLights_ON)
Group:Switch:OR(ON, OFF) gLights_OFF_AFTERNOON (gLights_OFF)
Group:Switch:OR(ON, OFF) gLights_ON_EVENING    (gLights_ON)
Group:Switch:OR(ON, OFF) gLights_OFF_EVENING   (gLights_OFF)
Group:Switch:OR(ON, OFF) gLights_ON_NIGHT      (gLights_ON)
Group:Switch:OR(ON, OFF) gLights_OFF_NIGHT     (gLights_OFF)
Group:Switch:OR(ON, OFF) gLights_ON_BED        (gLights_ON)
Group:Switch:OR(ON, OFF) gLights_OFF_BED       (gLights_OFF)

Switch aFrontLamp "Front Room Lamp"
  (gLights_ALL, gLights_ON_MORNING, gLights_OFF_DAY, gLights_OFF_AFTERNOON, gLights_ON_EVENING, gLights_OFF_NIGHT, gLights_OFF_BED)
  { channel="zwave:device:dongle:node3:switch_binary" }
  
Switch aFamilyLamp "Family Room Lamp"
  (gLights_ALL, gLights_OFF_MORNING, gLights_OFF_DAY, gLights_ON_AFTERNOON, gLights_ON_EVENING, gLights_OFF_NIGHT, gLights_OFF_BED)
  { channel="zwave:device:dongle:node10:switch_binary" }
  
Switch aPorchLight "Front Porch"
  (gLights_ALL, gLights_OFF_MORNING, gLights_OFF_DAY, gLights_OFF_AFTERNOON, gLights_ON_EVENING, gLights_OFF_NIGHT, gLights_OFF_BED)
  { channel="zwave:device:dongle:node6:switch_binary" }

Above there is an ON group and an OFF group for each Time of Day. The lights themselves are added to the appropriate groups base don whether the light should be ON or OFF during their time period. Then those groups are added to the appropriate gLights_ON or gLights_OFF group.

val logName = "lights"

rule "Set lights based on Time of Day"
when
  Item vTimeOfDay changed
then
    val offGroupName = "gLights_OFF_"+vTimeOfDay.state.toString
    val onGroupName = "gLights_ON_"+vTimeOfDay.state.toString

    logInfo(logName, "Turning off lights in " + offGroupName)
    val GroupItem offItems = gLights_OFF.members.filter[g|g.name == offGroupName].head as GroupItem
    offItems.members.filter[l|l.state != OFF].forEach[l | l.sendCommand(OFF)]

    logInfo(logName, "Turning on lights for " + onGroupName)
    val GroupItem onItems = gLights_ON.members.filter[g|g.name == onGroupName].head as GroupItem
    onItems.members.filter[l|l.state != ON].forEach[l | l.sendCommand(ON)]
  
end

The above rule gets triggered when vTimeOfDay changes. It pulls the lights ON group from gLights_ON based on the current Time of Day. It turns all of those ON. It pulls the OFF group from gLights_OFF based on the current Time of Day and turns all of those OFF.

You could expand that to work with Dimmers fairly easily a number of ways (exercise left to the student).

Thus, the entirety of my lighting is controlled by a mere 12 lines of code (not counting the Time of Day code).