Controlling a HS103 smart socket LED with OH3

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?

Thanks for any insights.

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.

Post a use case and we can work on a rule.

Right Zooka, I too can do what you describe with my hs103. But can you turn the LED on and off while the power switch remains off?

Yes.

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

So I took another look at the devices.

The HS100 has two LEDs; wifi status LED, power switch status LED.
The HS103 appears to only have the one power switch LED?

When I toggle with the power OFF, the wifi only will flash.
when I toggle with the power ON, the wifi AND the power LEDs will flash.

So, without a HS103 to test, I would assume that this particular device will not flash the LED as you require, but a HS100 will.

Used a cronjob to test my HS100. This toggles the (wifi) LED every 6 seconds, if the lamp is off.

triggers:
  - id: "1"
    configuration:
      cronExpression: 0/6 * * * * ? *
    type: timer.GenericCronTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: |-
        if (OfficeLamp_Power.state == OFF)
        {
          if (OfficeLamp_LED.state == ON)
            OfficeLamp_LED.sendCommand(OFF)
            
          OfficeLamp_LED.sendCommand(ON)
        }
    type: script.ScriptAction


I reached the same conclusion re. the HS103. Thanks for looking.

1 Like