definition( parent: "Mark-C-UK:Whats On Parent", name: "Whats On Child", namespace: "Mark-C-UK", author: "Mark C", description: "check what is on", category: "Convenience", iconUrl: "", iconX2Url: "" ) preferences { page(name: "mainPage") } Map mainPage() { dynamicPage(name: "mainPage", title: "Setup", uninstall: true, install: true) { section { input "appName", "text", title: "Name this instance whats on", submitOnChange: true if(appName) app.updateLabel(appName) input "switches", "capability.switch", title: "Switches to check", multiple: true input "therms", "capability.thermostat", title: "Therm to check", multiple: true input "contats", "capability.contactSensor", title: "contacts to check for open", multiple: true paragraph "For the trigger use a Virtual Switch with auto-off enabled, turning it on checks switches above" input "trigger", "capability.switch", title: "Trigger switch" input "dataName", "text", title: "Enter a name for the Data Device", required:false, submitOnChange:true paragraph "A device will automaticaly be created for you as soon as you click outside of this field" if(dataName) createDataChildDevice() input "dataDev", "capability.actuator", title: "Data Device", required:true, multiple: false, submitOnChange: true input "logEnable", "bool", title: "Enable debug logging", defaultValue: false, submitOnChange: true } } } void updated() { unsubscribe() initialize() } void installed() { initialize() } void uninstalled(){ unschedule() unsubscribe() } void initialize() { subscribe(trigger, "switch.on", handler) log.info "${app.label} starting up, running handler in 5" runIn (5, handler) } void handler(evt) { if (logEnable == true) log.trace "${app.label} - handler processing" String whichOn = "
"
    
    if (therms != null){
        //Map thermsMap = [:]
        List thermsMap = []
        for (com.hubitat.app.DeviceWrapper dev in therms) {
            if (dev.currentThermostatMode !='off'){
                //thermsMap << ["${dev.device.displayName}":"${dev.currentThermostatMode}"]
                thermsMap << dev.device.displayName
            }
        }
//        therms?.each {
//            thermsMap << ["${it.device.displayName}":"${it.currentThermostatMode}"]
//        }
        whichOn += 'Therm\n'
        //thermsMap?.sort().each{ key, value ->
        thermsMap?.sort().each{
            if (logEnable == true) log.debug "${app.label} - sorted $thermsMap"
            //if (value != 'off'){
            //whichOn += "${key}\n"
            whichOn += "${it}\n"
            //}
        }
        whichOn += ''
    }

    if (switches != null){
        //Map switchesMap = [:]
        List switchesMap = []
        for (com.hubitat.app.DeviceWrapper dev in switches) {
            if (dev.currentSwitch == 'on'){
                //switchesMap << ["${dev.device.displayName}":"${dev.currentSwitch}"]
                switchesMap << dev.device.displayName
            }
        }
//        switches?.each {
//            switchesMap << ["${it.device.displayName}":"${it.currentSwitch}"]
//        }
        whichOn += 'Switches\n'
        //switchesMap?.sort().each{ key, value ->
        switchesMap?.sort().each{ 
            whichOn += "${it}\n"
            if (logEnable == true) log.debug "${app.label} - sorted $switchesMap"
            //if (value == 'on'){
                //whichOn += "${key}\n"
           // }
        }
        whichOn += ''
    }
    
    if (contats != null){
        //Map contatsMap = [:]
        List contatsMap = []
        for (com.hubitat.app.DeviceWrapper dev in contats) {
            //contatsMap << ["${dev.device.displayName}":"${dev.currentContact}"]
            if (dev.currentContact == 'open'){
                contatsMap << dev.device.displayName
            }
        }
//        contats?.each{
//            contatsMap << ["${it.device.displayName}":"${it.currentContact}"]
//       }
        whichOn += 'Contacts\n'
        //contatsMap?.sort().each{ key, value ->
        contatsMap?.sort().each{
            if (logEnable == true) log.debug "${app.label} - sorted $contatsMap"
            //if (value == 'open'){
            //    whichOn += "${key}\n"
            //}
            whichOn += "${it}\n"
        }
        whichOn += ''
    }
    
    whichOn    += '
' state.theCount = whichOn.size() if (logEnable == true) log.debug "${app.label} - char count ${state.theCount}" if (state.theCount > 1024) { whichOn = 'to many devices' log.warn "${app.label} - char count ${state.theCount}" } if (logEnable == true) log.debug "${app.label} - final data $whichOn" dataDev.parse(whichOn) } def createDataChildDevice() { if (logEnable == true) log.debug "${app.label} - In createDataChildDevice" String statusMessageD = '' if(!getChildDevice(dataName)) { if (logEnable == true) log.debug "${app.label} - In createDataChildDevice - Child device not found - Creating device: ${dataName}" try { addChildDevice("Mark-C-UK", "Whats On Tile", dataName, 1234, ["name": "${dataName}", isComponent: false]) if (logEnable == true) log.debug "${app.label} - In createDataChildDevice - Child tile device has been created! (${dataName})" statusMessageD = "Device has been been created. (${dataName})" } catch (e) { log.debug "${app.label} - unable to create data device - ${e}" } } else { statusMessageD = "Device Name (${dataName}) already exists." } return statusMessageD }