Switch off power socket when usage less than 100W

Hi, I got idea to switch off power socket after I switch of the PC. I did check PC usage and choose the limit: if usage less than 100W switch of plug after 1min.
I started with Thread::Sleep but I found out its not good idea, because sometimes I was not able turn PC on because socket read less then 100W and keep switching it off :slight_smile:

So I turned into smarter solution: Timer

When usage less than 100W start timer, when again over 100W cancel timer (when you want switch PC again on after you switched it off).

Question: Isnt this script resources hungry, when it keep checking/trigger if timer is not active everytime when power value changed (I change power socket things reading from 2s to 10s) ALL the time while is Power Socket ON?
Any better solution?

Using Openhabian on RPi and TPLink HS110

var Timer TPL3PowerSocketTimer = null

// -- POWER SOCKET CONTROL -----------------------------------------------------
// -----------------------------------------------------------------------------

rule "Power socket low usage PC main OFF"
when 
  Item TPL3_Power changed
then

  // -- less than 100W and switch is ON
  if (TPL3_Power.state < 100 && TPL3_Switch.state == ON) {
     
     // -- timer already counting down
     if (TPL3PowerSocketTimer !== null) {       
       logInfo("LogPowerSocket","Power usage less than 100W (" + TPL3_Power.state + "W) on TPL3: TIMER COUNTDOWN RUNNING: " + TPL3PowerSocketTimer)
     }
     
     // -- timer not started, start
     else {
       
       logInfo("LogPowerSocket","Power usage less than 100W (" + TPL3_Power.state + "W) on TPL3: TIMER STARTED: " + TPL3PowerSocketTimer)
       TPL3PowerSocketTimer = createTimer(now.plusMinutes(1), [|  
         
         TPL3_Switch.sendCommand(OFF)
         TPL3_Switch_Led.sendCommand(OFF)
         Thread::sleep(500)
         
         logInfo("LogPowerSocket","Power usage less than 100W (" + TPL3_Power.state + "W) on TPL3: TIMER FINISHED, Socket state: " + TPL3_Switch.state)
         
         TPL3PowerSocketTimer = null              
       ]) 
     }      
  }
  
  // -- more than 100W
  else if (TPL3_Power.state >= 100) {
    
    if (TPL3PowerSocketTimer !== null) {
      TPL3PowerSocketTimer.cancel()
      TPL3PowerSocketTimer = null
      logInfo("LogPowerSocket","Power usage more than 100W (" + TPL3_Power.state + "W) on TPL3: TIMER CANCELED")
    }
    //else {
    // logInfo("LogPowerSocket","Power usage more than 100W (" + TPL3_Power.state + "W) on TPL3: TIMER IS OFF")
    //}      
  }

end

I don’t know about it being resource hungry, but a slightly different way to do it would be to use your PC’s network status as the trigger (using the network binding) instead of reacting to power fluctuations. So if your PC goes offline, wait for one minute, and turn off the TP-Link switch if power is less than 100W.

I’m unclear as to why you would want to completely shut off power to your PC when it’s already off. There’s a minor power savings, but doesn’t that mean you have to turn the TP-Link switch back on before you can use your PC the next time?

The rule probably uses less resource than is taken up processing the messages that result in Item power changed. I wouldn’t worry about it, this is the kind of event-driven action openHAB is meant for.

You can configure some PCs to auto power up when external power is “restored”, which would make it a simpler job.

1 Like

TPlink socket control ext cord with 8 socket. Stuff like Speakers (each have active amplifier), monitors vacuum cleaner robot etc. So instead of swithching all around table one by one off or manually (pressing button or going into app) switch tplink socket off, it will happend automatically :slight_smile:

When PC is off other devices pull 30W + I just switch it all off for night … its all like computer related.

When i need turn it all back on i just turn on TPlink socket and PC start automaticaly. Yes power on when power restored function in BIOS.

Thnx, I wanted to hear something like this :joy:
I guess I will comment out all the loginfo, it was more like for testing …

I thought that might be the case, but didn’t want to assume (I do that too much instead of just asking). I have a power bar for my bedroom that detects when the TV is off and then turns off two of its outlets, which is the same principle. And I cut power to a lot of my devices when I leave the house or go to bed.

I totally forgot about the BIOS setting to resume power, since I haven’t had a desktop PC for a long time.

Thanks for reply guys!