/* * Bathroom Humidity Fan (Parent) * * I just wanted a parent/child version. * * */ def setVersion(){ state.version = "1.1.43" // Version number of this app state.InternalName = "BathroomHumidityFan" // this is the name used in the JSON file for this app } definition( name: "Bathroom Humidity Fan", namespace: "Craig.Romei", singleInstance: true, author: "Craig Romei", description: "Control a fan (switch) based on relative humidity. - Parent", category: "Convenience", iconUrl: "", iconX2Url: "", iconX3Url: "", importUrl: "https://raw.githubusercontent.com/napalmcsr/Hubitat_Napalmcsr/master/Apps/BathroomHumidityFan/BathroomHumidity.src") preferences { page(name: "mainPage") } def mainPage() { return dynamicPage(name: "mainPage", title: "", install: true, uninstall: true) { if(!state.ShfInstalled) { section("Hit Done to install Bathroom Humidity Fan App") { } } else { section("Create a new Bathroom Humidity Fan Instance.") { app(name: "childApps", appName: "Bathroom Humidity Fan Child", namespace: "Craig.Romei", title: "New Bathroom Humidity Fan Instance", multiple: true) } section("Create a combined humidity sensor.") { input "humidSensors", "capability.relativeHumidityMeasurement", title: "Select Humidity Sensors", submitOnChange: true, required: false, multiple: true input name: "Create", type: "button", title: state.createCombinedSensorButtonName, submitOnChange:true, width: 5 if(humidSensors) paragraph "Current average is ${averageHumid()}%" } } } } def installed() { state.ShfInstalled = true initialize() } def updated() { initialize() if (state?.created == null) {(state.created = false)} } def initialize() { def averageDev = getChildDevice("AverageHumid_${app.id}") if (getChildDevice("AverageHumid_${app.id}")) {(getChildDevice("AverageHumid_${app.id}")).setHumidity(averageHumid())} if (humidSensors) {subscribe(humidSensors, "humidity", handler)} setCreateCombinedSensorButtonName() } def averageHumid() { def total = 0 def n = humidSensors.size() humidSensors.each {total += it.currentHumidity} return (total / n).toDouble().round(1) } def handler(evt) { def avg = averageHumid() if (getChildDevice("AverageHumid_${app.id}")) {(getChildDevice("AverageHumid_${app.id}")).setHumidity(averageHumid())} } def setCreateCombinedSensorButtonName() { if (getChildDevice("AverageHumid_${app.id}")) { state.createCombinedSensorButtonName = "Delete Combined Humidity Sensor" } else if (!getChildDevice("AverageHumid_${app.id}")) { state.createCombinedSensorButtonName = "Create Combined Humidity Sensor" } } def appButtonHandler(btn) { state.created = !state.created if (!getChildDevice("AverageHumid_${app.id}")) { addChildDevice("hubitat", "Virtual Humidity Sensor", "AverageHumid_${app.id}", null, [label: thisName, name: thisName]) } else if (state.createCombinedSensorButtonName =="Delete Combined Humidity Sensor") { (deleteChildDevice("AverageHumid_${app.id}")) } setCreateCombinedSensorButtonName() }