Hey Everyone!
It’s my first time posting here and I’m new to this coding language, so please excuse any newbie coding mistakes I’ve made while playing around with this Candle Flicker smartapp idea I had. But I wanted to ask for some help on getting this to work. I have a bunch of new Hue bulbs, and I know there are a bunch of ios apps that do this exact things better, but I wanted to take a stab at making it a Smartapp so I can trigger it automatically. My goal is to make them dim up and down at a random value and then offset that action by a random constrained value as well.
This is what I have so far today, but I obviously haven’t gotten it to work yet. Could one of the experts on these forums let me know what I don’t?
Thanks in advance, it’s a fun new language to hack around with!
/**
- Candle Flicker
- Copyright 2015
- Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except
- in compliance with the License. You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
- on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
- for the specific language governing permissions and limitations under the License.
*/
definition(
name: “Candle Flicker”,
namespace: “”,
author: “Doug Hogan”,
description: “Flicker your lights like Candles using a constrained random dimming value.”,
category: “My Apps”,
iconUrl: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png”,
iconX2Url: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png”)
preferences {
section("Select Lights...") { input "dimmers", "capability.switchLevel", title: "Dimmers", required: false, multiple: true }
}
def installed() {
log.debug “Installed with settings: ${settings}”
initialize()
}
def updated() {
log.debug “Updated with settings: ${settings}”
unsubscribe() initialize()
}
def initialize() {
subscribe(app, appTouchHandler)
}
def eventHandler(evt) {
log.debug “eventHandler $evt hit”
//Get Random Number def ranNum = getRandom(30,80) log.debug "Flame Intensity is $ranNum"
//Dim lights to a random value def delay = 1L for (int i=1; i < getRandom(30,80); i++) { lights?.on(delay: delay) delay += ranNum * 1000 lights?.off(delay: delay) delay += ranNum * 1000 }
}
def appTouchHandler(evt) {
//takeAction(evt)
}
def getRandom(int min, int max) {
return Math.abs(new Random().nextInt() % max + min)
}