I am using openHAB 3.0.1 with some tp-link smartHome hs103 smart sockets. I would like to control a hs103 LED from openHAB. For example, have the LED glow when the power is OFF, and conversely have the LED off when the power is ON. Or, make the LED slowly flash ON and OFF, independent of the power’s state.
Using the openHAB MainUI and the hs103 Switch LED Point, I can only enable or disable the LED in its traditional indicator role. Being discouraged, I have not tried to write a custom rule/script to do what I desire.
Is what I want even possible? Could someone skilled in the art of openHAB Bindings programming provide this functionality? Is the hs103 too stupid to do that? Or, is it just me getting discouraged too early and too easily?
Hey Bob, I can control my HS100 switch and LED independently. Basically just two switches on the equipment endpoint. The LED does come on when the switch turns on but I can turn it (the LED) off again while the switch remains on.
So the binding looks good, just need the rules to support your use cases.
Below is my script. It does not succeed at flashing the led when the power is OFF.
/**
* Prototype for flashing a tp-link hs103 smart socket's LED
* light when the power of the smart socket is turned OFF. This
* is invoked via Rule that says
* When xxx_Power changed
* where xxx_Power and xxx_SwitchLED are Points obtained from clicking
* Create Points From Thing button in the Main UI for Thing xxx.
*
*/
var flashingOffIndicator = flashingOffIndicator || (function(context) {
var ScriptExecution = Java.type("org.openhab.core.model.script.actions.ScriptExecution");
var ZonedDateTime = Java.type("java.time.ZonedDateTime");
var OPENHAB_CONF = Java.type("java.lang.System").getenv("OPENHAB_CONF");
var JS_CORE = OPENHAB_CONF + "/automation/lib/javascript/core/";
load(JS_CORE + "rules.js");
var logger = getLogger("LED flasher");
var powerSuffix = "_Power";
var ledSuffix = "_SwitchLed";
var onOffTime = 6 * 1000 * 1000 * 1000; // nano seconds
/* Construct a Flasher object that flashes the named LED OFF and ON. */
var Flasher = function(powerName, ledName) {
var dis = this;
var state = OFF;
/*
* Called when the timer expires. Toggle the LED,
* only if it is still interesting.
*/
var toggle = function() {
if (Flasher.managed.indexOf(dis) >= 0) {
state = (state === OFF) ? ON : OFF;
events.sendCommand(ledName, state);
ScriptExecution.createTimer(ZonedDateTime.now().plusNanos(onOffTime), toggle);
}
}
dis.powerName = powerName;
Flasher.managed.push(dis);
toggle(); // Let's light this candle!
};
Flasher.managed = []; // managed array of constructed Flasher objects
/* Get rid of the Flasher object associated with powerName. */
Flasher.stop = function(powerName) {
for (var flashers = Flasher.managed, i = 0; i < flashers.length; i++) {
if (flashers[i].powerName === powerName) {
flashers.splice(i, 1); // snip
logger.warn("snipped away Flasher associated with " + powerName);
}
}
};
/*
* Main event processor.
* If the item's state transitioned to OFF, start flashing
* the item's associted LED. Otherwise, stop any flashing
* associated with the item.
*/
return function() {
if (event && event.getItemName) {
var powerName = event.getItemName();
if (event.itemState === OFF) {
var ledName = powerName.replace(powerSuffix, ledSuffix);
var ledItem = (ledName !== powerName) && items[ledName] && ir.getItem(ledName);
if (ledItem) {
new Flasher(powerName, ledName);
} else {
logger.warn("Cannot find a " + ledSuffix + " associated with " + powerName);
}
} else {
/* Cleanup... */
logger.warn(powerName + " " + event.itemState + " trying to stop flashing.");
Flasher.stop(powerName);
}
delete context.event; // clear this stale event
} else {
logger.warn("No event (or item name) means no actions");
}
};
})(this);
flashingOffIndicator();