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()
}