Conditional Messaging

Hi Guys
I’m trying to write a smartapp that sends me a message on certain events
These events are from a water sensor which I’m using to interface with my wired home alarm
(I have them setup to trigger an event when the alarm zone becomes active)

BUT…Only if a virtual switch is on - If the switch is off then I don’t want the messages

The events part of the app works perfectly - I get the required sms or push message every time
What I’m having problems with is the disabling of the messages when the virtual switch is off

This is what I have so far…

Here is my switch selection

    section("Select switch to control messages"){	
    paragraph "Select the switch to turn on/off (Used to enable/disable messages)"
    input "switch1", "capability.switch", title: "Notification on/off", required: true, multiple: false

Here is my subscription for the switch

subscribe(switch1, "switch.on", onHandler1)
subscribe(switch1, "", offHandler1)

Here are my event handlers for the switch

def onHandler1(evt) {
log.debug "$switch1 is switched On - Enabling notifications"
def CurrS1 = switch1.currentSwitch
log.debug “$switch1 is $CurrS1”

def offHandler1(evt) {
log.debug "$switch1 is switched off - Disabling notifications"
def CurrS1 = switch1.currentSwitch
log.debug “$switch1 is $CurrS1”


Here is one of my event handlers for the main events

def dryHandler1(evt) {

if ($CurrS1 == “on”) {
log.debug “$water1 shows dry - $switch1 enabled - sending message to user(s)”

 if ($phone1 != 'null'){
	sendSms(phone1, "$dryText1")}
else if ($phone2 != 'null'){
	sendSms(phone2, "$dryText1")}
else if ($phone3 != 'null'){
	sendSms(phone3, "$dryText1")}
else if ($phone4 != 'null'){
	sendSms(phone4, "$dryText1")}
else {

else log.debug “$water1 shows dry but the $switch1 is disabled so I’m not sending any message”


I need to be able to disable the messages when the alarm is not set (I have a connection in the panel to control this)

Where am I going wrong??
(Be gentle this is my 1st app!)


Your CurrS1 variable is scoped only within each handler function. You need to use state to have the value persist. E.g.

def onHandler1(evt) {
    log.debug "$switch1 is switched On - Enabling notifications"
    state.currS1 = switch1.currentSwitch
    log.debug "$switch1 is $CurrS1"

You could probably simplify things by having one handler subscribed to all switch events:

subscribe(switch1, "switch", switchHandler)

def switchHandler(evt) {
   state.currS1 = evt.value  // Note, better to reference the passed evt rather than switch1

Then in the dryHandler

def dryHandler1(evt) {

    if (state.currS1 == "on") {
1 Like

@zcapr17 Thanks very much for this, I’ll give it a go


Thanks this works perfectly!