Making rule with multiple ifs

It’s been a few months since I’ve made any changes to my system so I’m kind of dusting off the cob webs if you will. I have a z-wave enabled doorbell and I’m trying to make it so I can easily change what sound it plays in a sitemap. I have the sitemap set up fine and can toggle between the different selections. However I’m have a problem with the rule. Here is what I have written up

rule "Door Bell 2"
when
    Item DoorbellNotification received update 1
then
    if (DoorbellNotification.state == 0) {
        playSound("db00.mp3")
        pushover("Doorbell Rang") }
    else if (DoorbellNotification.state == 1){
        playSound("db01.mp3")
        pushover("Doorbell Rang") }
    else if (DoorbellNotification.state == 2){
        playSound("db02.mp3")
        pushover("Doorbell Rang") }
    else if (DoorbellNotification.state == 3){
        playSound("db03.mp3")
        pushover("Doorbell Rang") }
    else if (DoorbellNotification.state == 4){
        playSound("db04.mp3")
        pushover("Doorbell Rang") }
    else if (DoorbellNotification.state == 5){
        playSound("db05.mp3")
        pushover("Doorbell Rang") }
    else if (DoorbellNotification.state == 6){
        playSound("db06.mp3")
        pushover("Doorbell Rang") }
end

The result is that no matter what the doorbellnotification is set to, the db01.mp3 plays

Any ideas of what might be causing this?

i think you should go with

when
    Item DoorbellNotification changed

As the trigger is

Item DoorbellNotification received update 1

That’s it. I knew it would be something silly like that. Thanks

Another thing… you might like switch - case:

rule "Door Bell 2"
when
    Item DoorbellNotification received update
then
    switch (DoorbellNotification.state) {
        case 0: playSound("db00.mp3")
        case 1: playSound("db01.mp3")
        case 2: playSound("db02.mp3")
        case 3: playSound("db03.mp3")
        case 4: playSound("db04.mp3")
        case 5: playSound("db05.mp3")
        case 6: playSound("db06.mp3")
    }
    pushover("Doorbell Rang") }
end

And as the files are named in that simple way, you even could use:

rule "Door Bell 2"
when
    Item DoorbellNotification received update
then
    val String soundfile = "db0" + DoorbellNotification.state.toString + ".mp3" 
    playSound(soundfile)
    pushover("Doorbell Rang") }
end
1 Like

That looks promising, definitely cleaner looking. Not sure how that would work with the 2nd thing I’d like to implement. I was waiting until I got the first part done to try it out because you know, one step at a time. I was thinking about trying to make it so the sounds rotate each time the door bell is pressed. Here is my working code, with comments where I’m picturing the command to change the next soundfile, and back to 0 after the 6.

rule "Door Bell 2"
when
    Item DoorbellNotification received update 1
then
    if (DoorbellNotif.state == 0) {
        playSound("db00.mp3")
        pushover("Doorbell Rang") 
        //Change DoorbellNotif.State to 1
        }
    else if (DoorbellNotif.state == 1){
        playSound("db01.mp3")
        pushover("Doorbell Rang") 
        //Change DoorbellNotif.State to 2
        }
    else if (DoorbellNotif.state == 2){
        playSound("db02.mp3")
        pushover("Doorbell Rang") 
        //Change DoorbellNotif.State to 3
        }
    else if (DoorbellNotif.state == 3){
        playSound("db03.mp3")
        pushover("Doorbell Rang") 
        //Change DoorbellNotif.State to 4
        }
    else if (DoorbellNotif.state == 4){
        playSound("db04.mp3")
        pushover("Doorbell Rang") 
        //Change DoorbellNotif.State to 5
        }
    else if (DoorbellNotif.state == 5){
        playSound("db05.mp3")
        pushover("Doorbell Rang")
        //Change DoorbellNotif.State to 6
        }
    else if (DoorbellNotif.state == 6){
        playSound("db06.mp3")
        pushover("Doorbell Rang")
        //Change DoorbellNotif.State to 0
        }
end

Not sure what the best way to implement that is

Use @Udo_Hartmann rules:
And add a int variable to cycle through the chimes

var int currentChime = 0

rule "Door Bell 2"
when
    Item DoorbellNotification received update
then
    val String soundfile = "db0" + String.valueOf(currentChime) + ".mp3" 
    playSound(soundfile)
    pushover("Doorbell Rang")
    currentChime = currentChime + 1
    if (currentChime > 6) {
        currentChime = 0
    }
end

Note that after a restart on OH, the first chime will always be db00.mp3

Thanks for the feedback. I got an error when I tried loading that code. It says

[100,31]: mismatched input '}' expecting 'end'

Line 100 is the part of the code you provided saying the following

pushover("Doorbell Rang") }

My guess is that I’m getting that error because I don’t have a preceding { in that rule, I could be wrong though, I have a rather elementary understanding of this coding stuff. Trying to learn though. I tried inserting a { in a few different spots trying to troubleshoot it myself but just kept getting different errors instead.

Awesome, finally got it. Thanks. Had to insert () around currentChime > 6 but other than that it was all good. Thanks again to all.