Skip to content

Instantly share code, notes, and snippets.

@cramforce
Last active April 24, 2020 04:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cramforce/b794cc62f155881f85a15dd924eb7c40 to your computer and use it in GitHub Desktop.
Save cramforce/b794cc62f155881f85a15dd924eb7c40 to your computer and use it in GitHub Desktop.
Adjusts accent light dimming to maintain a ~constant relative brightness compared to ambient luminance
definition(
name: "Ambient Illuminance Dimmer",
namespace: "cramforce",
author: "Malte",
description: "Adjusts accent light dimming to maintain a ~constant relative brightness compared to ambient luminance",
category: "Convenience",
iconUrl: "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4b/Candle.jpg/1200px-Candle.jpg",
iconX2Url: "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4b/Candle.jpg/1200px-Candle.jpg"
)
preferences {
section("Devices") {
input "dimmer", "capability.switchLevel", title: "Dimmers", required: true, multiple: true
input "illuminance", "capability.illuminanceMeasurement", title: "Illuminance sensor", required: true
input "illuminanceConfigure", "capability.configuration", title: "Illuminance sensor configuration", required: true
}
section("Dim range") {
input "minDim", "number", title: "Min dim level", defaultValue: 20
input "maxDim", "number", title: "Max dim level", defaultValue: 40
}
section("Illuminance range") {
input "minIllu", "number", title: "Min illuminance level", defaultValue: 0
input "maxIllu", "number", title: "Max illuminance level", defaultValue: 30
}
}
def installed() {
log.debug "Installed/updated"
unsubscribe()
unschedule()
subscribe(illuminance, "illuminance", illuChanged)
subscribe(dimmer, "switch", update)
runEvery1Minute(minute)
minute()
update()
}
def updated() {
installed()
}
def illuChanged(evt) {
log.debug "$evt.name: $evt.value"
update()
}
def update() {
if (!anyOn()) {
log.debug "Ignoring because switches are off"
return;
}
def illu = illuminance.currentValue("illuminance");
log.debug "Illuminance value: $illu"
illu = Math.min(illu.floatValue(), maxIllu.floatValue());
illu = Math.max(illu.floatValue(), minIllu.floatValue());
log.debug "Adjusted value: $illu"
def illuPercent = (illu - minIllu) / (maxIllu - minIllu)
// The brighter it is, the higher the level
def level = minDim + (illuPercent) * (maxDim - minDim)
log.info "setLevel($level) $minDim + ($illuPercent) * ($maxDim - $minDim)"
dimmer.each {
if (it.currentValue("switch") == "on") {
log.debug "$it.label setLevel($level)"
it.setLevel(level);
}
}
}
def minute() {
if (!anyOn()) {
return;
}
// This triggers a new measurement because otherwise we only get a valu every 8 minutes
log.debug "Configure illu sensor"
illuminanceConfigure.configure();
}
def anyOn() {
def on = dimmer.currentValue("switch").any { it == "on" }
log.debug "On: $on";
return on
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment