I'm assuming i'm not the only one trying to workout how to wright drivers or port/ fix them. So I'm hoping the community can help me and possibly others with some questions. This isn't about directly porting as that is covered in other threads, more about the crux of getting things working from scratch or for copying others.
In my case in a driver I have these lines of code under preferences:
input name: "IP1Type", type: "enum", title: "Input 1 Type",
options: ["Contact", "Motion"], defaultValue: "Contact", displayDuringSetup: true
input name: "IP2Type", type: "enum", title: "Input 2 Type",
options: ["Contact", "Motion"], defaultValue: "Contact", displayDuringSetup: true
these works well and where needed i use it for stuff like this
if (IP2Type == "Contact") sendEvent(name: "contact2", value: "closed", descriptionText: "$device.displayName - IP2 was set to closed", type: "digital")
if (IP2Type == "Motion") sendEvent(name: "contact2", value: "active", descriptionText: "$device.displayName - IP2 was set to active", type: "digital")
But i also want to define a bool in the driver for this:
if (IP1Type == "Contact" || IP2Type == "Contact") { ContactUsed = true
if (txtEnable) log.info "At least 1 Contact selected"
} else { ContactUsed = false
if (txtEnable) log.info "Contact's not selected"
}
if (IP1Type == "Motion" || IP2Type == "Motion") { MotionUsed = true
if (txtEnable) log.info "At least 1 Motion selected"
} else { MotionUsed = false
if (txtEnable) log.info "Motion not selected"
}
I think this code works (the logs seem to say the correct thing) but how do i create the bool ContactUsed and MotionUsed?
i've tried quite a few things but nothing works currently im trying under def updated() I tried this:
input name: "ContactUsed", type: "bool"
input name: "MotionUsed", type: "bool"
but i'm litery stabbing in the dark here
currently i'm doing this to get over it
if (IP1Type == "Motion" || IP2Type == "Motion") childDevice.sendEvent(name: "motion", value: motionstate, descriptionText: "IP${cmd.sourceEndPoint} has become ${motionstate}", type: "physical")
if (IP1Type == "Contact" || IP2Type == "Contact") childDevice.sendEvent(name: "contact", value: currentstate, descriptionText: "IP${cmd.sourceEndPoint} has ${currentstate}ed", type: "physical")
rather than
if (MotionUsed) childDevice.sendEvent(name: "motion", value: motionstate, descriptionText: "IP${cmd.sourceEndPoint} has become ${motionstate}", type: "physical")
if (ContactUsed) childDevice.sendEvent(name: "contact", value: currentstate, descriptionText: "IP${cmd.sourceEndPoint} has ${currentstate}ed", type: "physical")
and it works, but to me the driver working out this if (IP1Type == "Contact" || IP2Type == "Contact")
20 times must be quite expensive so doing it once and sticking it in a variable is better?