[SOLVED] Multiple IF Statements with Multiple Actions

Put the nStatus variable as a global variable.
When the timer checks it, the rule has finished and the variable “disapeared”
And initialise strPlaysound

var strPlaySound = ""

I’m pretty sure that the code should work as intended.

tStatus as well as nStatus are defined as global (please confirm that you put the definition at top of the file)

strPlaysound doesn’t need to be initialized, as it’s set in the switch case construct.

But there is something going wrong with nStatus, as no case other than default is executed.
Please add some logging and an .intValue:

// always define global var and val at top of the file!

var Timer tStatus = null                                                                                          // timer for house status
var Number nStatus = 0                                                                                            // step for playing status 

rule "House Door Status"
when
    Item HouseCheck received command ON                                                                           // maybe better use received command ON
then
    Kitchen_Volume.sendCommand(40)                                                                                // set volume
    tStatus?.cancel                                                                                               // delete existing timer
    nStatus = 0                                                                                                   // reset timer
    tStatus = createTimer(now.plusSeconds(3), [ |                                                                 // start initial timer
        var String strPlaysound = ""                                                                              // which file to play
        nStatus = nStatus + 1
        logInfo("statuscheck","Timer gets executed. nStatus = {}",nStatus)                                        // some logging...
        switch (nStatus.intValue) {                                                                               // just to be sure, it's an Integer
            case 1: {                                                                                             // first run
                strPlaysound = if (Front_Door_Status.state == OPEN) "FrontisOpen.mp3" else "FrontisClosed.mp3"    // ternary operator is way more simple
            }
            case 2: {                                                                                             //second run...
                strPlaysound = if (Garage_Door_Status.state == OPEN) "GarageisOpen.mp3" else "GarageisClosed.mp3"
            }
            case 3: {
                strPlaysound = if (SB_02_P1.state == OPEN) "ShedisOpen.mp3" else "ShedisClosed.mp3"
            }
            case 4: {
                strPlaysound = if (Front_Lights.state == ON) "FrontLightsOn.mp3" else "FrontLightsOff.mp3"
            }
            case 5: {
                strPlaysound = if (Back_Lights.state == ON) "BackLightsOn.mp3" else "BackLightsOff.mp3"
            }
            case 6: {
                strPlaysound = if (Back_Door_Status.state == OPEN) "BackDoorOpen.mp3" else "BackDoorClosed.mp3"
            }
            default: {                                                                                            // last run
                Kitchen_Volume.sendCommand(20)                                                                    // reset volume
                HouseCheck.sendCommand(OFF)                                                                       // reset triggering item
                tStatus = null                                                                                    // reset timer
            }
        }
        if (nStatus < 7) {                                                                                        // if not last step
            playSound(strPlaysound)                                                                               // play sound
            tStatus.reschedule(now.plusSeconds(3))
        }                                                                                                         // reschedule timer
    ])
end

Hi Udo, I didn’t put the above in the rule but I did do what it said with

var Timer tStatus = null
var Number nStatus  

being at the top.
The rule is exactly as per posted and sorry to say I don’t know how to add logging.
I also made changes to the brackets from your original reply as it would not load as is but may have got it wrong.
James

Hi Again, with the last code you posted I get "No viable alternative at input ‘rule’ 6,1. I can’t see the culprit bracket.

Sorry to keep posting, Im not home to hear it but it looks like its working. The previous error "No viable alternative at input rule was due to the = sign after the var number nStatus. I removed it and manually fired it off. here is the log.

2019-09-12 10:24:34.030 [ome.event.ItemCommandEvent] - Item 'HouseCheck' received command ON

2019-09-12 10:24:34.047 [vent.ItemStateChangedEvent] - HouseCheck changed from OFF to ON

2019-09-12 10:24:35.731 [ome.event.ItemCommandEvent] - Item 'Kitchen_Volume' received command 40

2019-09-12 10:24:35.749 [vent.ItemStateChangedEvent] - Kitchen_Volume changed from 20 to 40

==> /var/log/openhab2/openhab.log <==

2019-09-12 10:24:38.744 [INFO ] [e.smarthome.model.script.statuscheck] - Timer gets executed. nStatus = 1

==> /var/log/openhab2/events.log <==

2019-09-12 10:24:40.475 [vent.ItemStateChangedEvent] - KitchenDisplay_MediaTitle changed from UNDEF to Notification

2019-09-12 10:24:40.520 [vent.ItemStateChangedEvent] - Gateway_AmbientLight changed from 851 to 828

2019-09-12 10:24:42.746 [vent.ItemStateChangedEvent] - KitchenDisplay_MediaTitle changed from Notification to UNDEF

==> /var/log/openhab2/openhab.log <==

2019-09-12 10:24:44.882 [INFO ] [e.smarthome.model.script.statuscheck] - Timer gets executed. nStatus = 2

==> /var/log/openhab2/events.log <==

2019-09-12 10:24:45.292 [vent.ItemStateChangedEvent] - KitchenDisplay_MediaTitle changed from UNDEF to Notification

2019-09-12 10:24:46.272 [vent.ItemStateChangedEvent] - KitchenDisplay_MediaTitle changed from Notification to UNDEF

==> /var/log/openhab2/openhab.log <==

2019-09-12 10:24:49.202 [INFO ] [e.smarthome.model.script.statuscheck] - Timer gets executed. nStatus = 3

==> /var/log/openhab2/events.log <==

2019-09-12 10:24:49.668 [vent.ItemStateChangedEvent] - KitchenDisplay_MediaTitle changed from UNDEF to Notification

2019-09-12 10:24:50.717 [vent.ItemStateChangedEvent] - KitchenDisplay_MediaTitle changed from Notification to UNDEF

2019-09-12 10:24:53.457 [vent.ItemStateChangedEvent] - Gateway_AmbientLight changed from 839 to 790

==> /var/log/openhab2/openhab.log <==

2019-09-12 10:24:53.592 [INFO ] [e.smarthome.model.script.statuscheck] - Timer gets executed. nStatus = 4

==> /var/log/openhab2/events.log <==

2019-09-12 10:24:54.196 [vent.ItemStateChangedEvent] - KitchenDisplay_MediaTitle changed from UNDEF to Notification

2019-09-12 10:24:55.208 [vent.ItemStateChangedEvent] - KitchenDisplay_MediaTitle changed from Notification to UNDEF

==> /var/log/openhab2/openhab.log <==

2019-09-12 10:24:58.101 [INFO ] [e.smarthome.model.script.statuscheck] - Timer gets executed. nStatus = 5

==> /var/log/openhab2/events.log <==

2019-09-12 10:24:58.543 [vent.ItemStateChangedEvent] - KitchenDisplay_MediaTitle changed from UNDEF to Notification

2019-09-12 10:24:59.557 [vent.ItemStateChangedEvent] - KitchenDisplay_MediaTitle changed from Notification to UNDEF

==> /var/log/openhab2/openhab.log <==

2019-09-12 10:25:02.442 [INFO ] [e.smarthome.model.script.statuscheck] - Timer gets executed. nStatus = 6

==> /var/log/openhab2/events.log <==

2019-09-12 10:25:02.893 [vent.ItemStateChangedEvent] - KitchenDisplay_MediaTitle changed from UNDEF to Notification

2019-09-12 10:25:03.883 [vent.ItemStateChangedEvent] - KitchenDisplay_MediaTitle changed from Notification to UNDEF

==> /var/log/openhab2/openhab.log <==

2019-09-12 10:25:06.802 [INFO ] [e.smarthome.model.script.statuscheck] - Timer gets executed. nStatus = 7

==> /var/log/openhab2/events.log <==

2019-09-12 10:25:06.817 [ome.event.ItemCommandEvent] - Item 'Kitchen_Volume' received command 20

2019-09-12 10:25:06.841 [ome.event.ItemCommandEvent] - Item 'HouseCheck' received command OFF

2019-09-12 10:25:06.846 [vent.ItemStateChangedEvent] - Kitchen_Volume changed from 40 to 20

2019-09-12 10:25:06.859 [vent.ItemStateChangedEvent] - HouseCheck changed from ON to OFF
1 Like

So the Timer works as suggested, but there is no message played?

So more logging:

// always define global var and val at top of the file!

var Timer tStatus = null                                                                                          // timer for house status
var Number nStatus = 0                                                                                            // step for playing status 

rule "House Door Status"
when
    Item HouseCheck received command ON                                                                           // maybe better use received command ON
then
    Kitchen_Volume.sendCommand(40)                                                                                // set volume
    tStatus?.cancel                                                                                               // delete existing timer
    nStatus = 0                                                                                                   // reset timer
    tStatus = createTimer(now.plusSeconds(3), [ |                                                                 // start initial timer
        var String strPlaysound = ""                                                                              // which file to play
        nStatus = nStatus + 1
        logInfo("statuscheck","Timer gets executed. nStatus = {}",nStatus)                                        // some logging...
        switch (nStatus.intValue) {                                                                               // just to be sure, it's an Integer
            case 1: {                                                                                             // first run
                strPlaysound = if (Front_Door_Status.state == OPEN) "FrontisOpen.mp3" else "FrontisClosed.mp3"    // ternary operator is way more simple
            }
            case 2: {                                                                                             //second run...
                strPlaysound = if (Garage_Door_Status.state == OPEN) "GarageisOpen.mp3" else "GarageisClosed.mp3"
            }
            case 3: {
                strPlaysound = if (SB_02_P1.state == OPEN) "ShedisOpen.mp3" else "ShedisClosed.mp3"
            }
            case 4: {
                strPlaysound = if (Front_Lights.state == ON) "FrontLightsOn.mp3" else "FrontLightsOff.mp3"
            }
            case 5: {
                strPlaysound = if (Back_Lights.state == ON) "BackLightsOn.mp3" else "BackLightsOff.mp3"
            }
            case 6: {
                strPlaysound = if (Back_Door_Status.state == OPEN) "BackDoorOpen.mp3" else "BackDoorClosed.mp3"
            }
            default: {                                                                                            // last run
                Kitchen_Volume.sendCommand(20)                                                                    // reset volume
                HouseCheck.sendCommand(OFF)                                                                       // reset triggering item
                tStatus = null                                                                                    // reset timer
            }
        }
        logInfo("statuscheck","Chose file {} to play.",strPlaysound)                                              // some more logging...

        if (nStatus < 7) {                                                                                        // if not last step
            playSound(strPlaysound)                                                                               // play sound
            tStatus.reschedule(now.plusSeconds(3))
        }                                                                                                         // reschedule timer
    ])
end

Hi Udo,
I tested last night and you are correct nothing played. I could hear the volume go up and then when all timers finished the volume went down. I changed my default audio sync device at it works perfectly. I changed back and it does not play again. So, your rule works and I will work out why my Kitchen display will not work as it used to.
Thank you so much for all your patience and help.
James

Ah. Of course you have to set the audio sink if you are using more than one. See

for details.