[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

Thank you for this app.

Requests:

  1. make it clearer what the on/off number and the seconds to randomize mean. Something like "Each on or off should happen every x minutes with each on or off event randomized by +/- y seconds. For example, 20 minutes with 90 second randomization means the light would turn on between 18 1/2 minutes to 21 1/2 minutes from the start. The light would then turn off between 18 1/2 minutes to 21 1/2 minutes later. The cycle would then repeat."

  2. Make it clearer that when choosing multiple lights, the lights will be controlled in in sequential order: Light 1 on/off, Light 2 on/off, light 3 on/off, then repeat.

  3. Feature request to have the ability to do all selected lights sequentially (as currently in #2), simultaneously, or independently from each other. e.g. if 3 lights are selected, they all turn on/off at the same time as each other. OR each of the 3 lights turn on/off 20min+-90 seconds independently.

Updated as requested

  1. descriptions updated
  2. Either one light will be randomly selected from the list and operated or all lights will be operated per cycle
  3. either one random or all toggle added

2 Likes

Thank you for the updates.

Hopefully quick questions:

  1. what is "belt and braces". I understand the option presented - after the conditions end, the lights should be turned off at their next "scheduled" time, instead of leaving them on. Is the option there in case the previous option to turn them off didn't work?

  2. If you choose modes at the bottom, do the cycles get triggered on their own, e.g. if the mode becomes Away, then the app triggers on/off cycles? Or is it "the switch will only trigger on/off IF the hub is in these modes?

A request with no rush: 1) Put the download URL in the app, making it easier to copy/paste to update. 2) would like to see this very useful app in HPM.

Finally, one thing to consider: Changing the name (At Home) to something that's more intuitive to the users. Sorry, I don't have a proper suggestion on that other than Randomize Lights, or Simulate Being at Home.

I have tried to emulate the random lights feature from the Wemo phone app into Hubitat for a couple of years, so I don't have to manually turn on/off those automations in the Wemo app. Even though there are other community light simulators for Hubitat, this app finally works in the nearly the same way Wemo did.

I have finally retired the Wemo automations, and the kludgy Rules on Hubitat. I am down to one virtual switch and one At Home app for each light involved, and one overall Schedule Manager. It's all been implemented, based on my assumptions on the questions I have previously. Now waiting to see it in action when we're away.

Thank you, @mark.cockcroft for sharing this app.