[Released] At home lights

Simulates you being at home
Really simple just operates from a switch which you can set up to turn on how you like using rule machine for sunset or presence.

Parent

/**
 
*/


definition(
    name: "At Home Lights Parent",
    namespace: "Mark-C-UK",
    author: "Mark C",
    description: "At Home Lights Parent app",
    category: "Convenience",
	iconUrl: "",
    iconX2Url: "",
    iconX3Url: "")


preferences {
     page name: "mainPage", title: "", install: true, uninstall: true
}
def installed() {
    log.info "Installed with settings: ${settings}"
    initialize()
}
def updated() {
    log.info "Updated with settings: ${settings}"
    unsubscribe()
    initialize()
}
def initialize() {
    log.info "There are ${childApps.size()} child apps, ${childApps.label}"
}
def installCheck() {         
	state.appInstalled = app.getInstallationState()
	if (state.appInstalled != 'COMPLETE') {
		section{paragraph "Please hit 'Done' to install '${app.label}' parent app "}
  	}
  	else {
    	log.info "Parent Installed OK"
  	}
}

def mainPage() {
    dynamicPage(name: "mainPage") {
    	installCheck()
		
		if (state.appInstalled == 'COMPLETE') {
			section("Average") {
				paragraph "This simulates lights for when you are away"
			}
  			section("At Home") {
				app(name: "At Home Lights", appName: "At Home Lights", namespace: "Mark-C-UK", title: "<b>Add child</b> ", multiple: true)
			}
		}
	}
}
2 Likes

Child

definition(
    parent: "Mark-C-UK:At Home Lights Parent",
    name: "At Home Lights",
    namespace: "Mark-C-UK",
    author: "Mark C",
    description: "switch things on and off at random",
    category: "Convenience",
    iconUrl: " ",
    iconX2Url: "",
    pausable: true
)

preferences {
    section("Set your input and output devices"){
        input "appName", "text", title: "Name this instance", submitOnChange: true, required: true
			if(appName) app.updateLabel(appName)
		input 'mastSwitch', 'capability.switch', title: 'master switch to control the app', required: true, multiple: false
		input 'lightControl', 'capability.switch', title: 'lights to control', required: true, multiple: true
        input 'time', 'number', title: 'time in min to have lights on or off' , required: true
        input 'timeRan', 'number', title: 'time in seconds to randomise the lights + or - (time in seconds MUST be smaller than time to above)', required: true
        input "logEnable", "bool", title: "Enable info logging", defaultValue: false, submitOnChange: true
        input "immediatOff", "bool", title: "When master switch turns off immediately turn off lights", defaultValue: false, submitOnChange: true
        input "DoubleCheckAllOff", "bool", title: "after it is deactived resend the off commands at next schdauled run", defaultValue: false, submitOnChange: true
    }
}

def installed() {
	installer()
}
def uninstalled(){
	unsubscribe()
    unschedule()
}
def updated() {
    log.info "${app.label} Updated"
    unsubscribe()
    installer()
}

def initialize() { // Runs on startup
    int randomSixty = 1 + (Math.random() * 58)
    log.info "Initialize running ${app.label} installer in ${randomSixty} seconds"
    runIn(randomSixty,installer)
}
    
def installer(){
    state.timeMin = time*60
    log.info "${app.label} installer - Master = $mastSwitch, controled = $lightControl, frequency ${state.timeMin}, random range $timeRan +/-"
    if (mastSwitch != null) {
        subscribe(mastSwitch, 'switch', mastSwitchState)
        log.info "${app.label} installer subscribing"
    }
}

void mastSwitchState(evt){
    //log.debug "${app.label} mastSwitchState - ${evt.value}"
    if (evt.value == 'on'){
        state.running = true
        //log.debug "${app.label} mastSwitchState - Evt ON starting the sim"
        lightSwitching()
    }
    else{ //evt off
        state.running = false
        //log.debug "${app.label} mastSwitchState - Evt OFF"
        if (immediatOff == true){
            unschedule(switchControlOn)
            unschedule(switchControlOff)
            for (com.hubitat.app.DeviceWrapper dev in lightControl) {
                dev.off()
                if(logEnable == true) log.info "${app.label} mastSwitchState - Master off, immediately turning off $dev"
                pause(3000)
            }
        }
        else{ // immedate turn off is off
            if(logEnable == true) log.info "${app.label} mastSwitchState - Master off, immediate off - run its couse and shut down"
        }
    }
}

void lightSwitching(){
    //log.debug "${app.label} lightSwitching - Running = $state.running"
    if (state.running == true){
        //log.debug "${app.label} lightSwitching True - running the Sim"
        int randomTime = (0 - timeRan) + (Math.random() * (timeRan*2)) + state.timeMin //random time
        //log.debug "random time $randomTime"
        if (randomTime < 2) randomDev = 3
        int randomDev = Math.random() * lightControl.size() // pick random device from the list
        runIn(randomTime, switchControlOn, [data: [dev:randomDev]])
        runIn(randomTime*2, switchControlOff, [data: [dev:randomDev]]) 
        if(logEnable == true) log.info "${app.label} lightSwitching - schedule on and off for device ${lightControl.get(randomDev)}, on in ${randomTime}s, off in ${randomTime*2}s"
    }
    else {
        if(logEnable == true) log.info "${app.label} lightSwitching false - all off to be safe"
        if(DoubleCheckAllOff == true) {
            for (com.hubitat.app.DeviceWrapper dev in lightControl) {
                dev.off()
                if(logEnable == true) log.info "${app.label} turning off $dev"
                pause(3000)
            }
        }
    }
}

void switchControlOn(data){
    if(state.running == true) {
        lightControl.get(data.dev).on()
        if(logEnable == true) log.info "${app.label} switchControlOn - ${lightControl.get(data.dev)} switched on"
    }
    else { 
        //log.debug "master switch must be off" 
    }
}
void switchControlOff(data){
    lightControl.get(data.dev).off()
    if(logEnable == true) log.info "${app.label} switchControlOff - ${lightControl.get(data.dev)} switched off"
    lightSwitching()
}


Updated logic and Dimmer control capability

I'm not getting what this app does, "At home light" can mean a lot of things. Can you provide a more verbose description or the use case it?

If you look at the code at the top the description details it “switches things on and off at random” so it appears that your home. You could just use a timer, but that didn’t fool the Wet Bandits….

Christmas Wet Bandits GIF by Home Alone

2 Likes