Switch off power plug if no Ping from PC comes back

Hi,

i need some help with my little simple rule.
I want to power off my remote controll wall socket for the PC if the PC is powered off and no ping comes back.

Here is my rule:

import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*
import java.lang.Math
import java.util.Calendar
import java.util.Date
import java.util.TimeZone
import java.text.SimpleDateFormat
import org.joda.time.*
import org.openhab.model.script.actions.Timer

rule "SteamLink aktiv und SteamMachine anschalten"
when
        Item harmonyhub_hub_Wohnzimmer_currentActivity changed to SteamLink
then
        sendCommand(r135c, ON)
end


rule "SteamMachine auschalten"
when
        Item harmonyhub_hub_Wohnzimmer_currentActivity changed to PowerOff
then
        sendCommand(Wake_SteamMachine, OFF)
        postUpdate(Wake_SteamMachine, OFF)

        if(Online_Desktop.state = "OFF") {
                createTimer(now.plusSeconds(10)) [| sendCommand(r135c, OFF) ]
        }
end

Here is my item for my PC:

Switch Online_Desktop   "SteamMachine"          {nh="172.16.17.50"}

Here is the log

2017-01-22 12:55:57.351 [ItemStateChangedEvent     ] - harmonyhub_hub_Wohnzimmer_currentActivity changed from PowerOff to SteamLink
2017-01-22 12:55:58.117 [ItemCommandEvent          ] - Item 'r135c' received command ON
2017-01-22 12:55:58.122 [ItemStateChangedEvent     ] - r135c changed from OFF to ON
2017-01-22 12:56:30.546 [ItemStateChangedEvent     ] - Online_Desktop changed from OFF to ON

2017-01-22 12:59:37.998 [ItemStateChangedEvent     ] - harmonyhub_hub_Wohnzimmer_currentActivity changed from SteamLink to PowerOff
2017-01-22 12:59:38.006 [ItemCommandEvent          ] - Item 'Wake_SteamMachine' received command OFF
2017-01-22 12:59:51.424 [ItemStateChangedEvent     ] - Online_Desktop changed from ON to OFF

The PC goes correct down with a Script in Windows, the Status with Network-Health is also correct.
Only the remote power plug (r135c) doesn´t power off after 10 seconds if no ping comes back.
There is no r135 OFF command recieved.

So there is something wrong in my rule…?
Thanks for any help :slight_smile:

Hi,

I think the garbage collector has already garbage collected your timer before it triggers. I have updated your code so that the timer object is now stored in a global variable. Once the timer triggers the global variable is set to null so that the garbage collector can properly remove it from memory.
I also added a log statement which should appear in the openhab.log when the timer is being triggered.

Hope this helps.

import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*
import java.lang.Math
import java.util.Calendar
import java.util.Date
import java.util.TimeZone
import java.text.SimpleDateFormat
import org.joda.time.*
import org.openhab.model.script.actions.Timer

var Timer timerSteamMachine = null

rule "SteamLink aktiv und SteamMachine anschalten"
when
        Item harmonyhub_hub_Wohnzimmer_currentActivity changed to SteamLink
then
        sendCommand(r135c, ON)
end


rule "SteamMachine auschalten"
when
        Item harmonyhub_hub_Wohnzimmer_currentActivity changed to PowerOff
then
        sendCommand(Wake_SteamMachine, OFF)
        // no need to do a postUpdate after sendCommand - postUpdate(Wake_SteamMachine, OFF) 

        if(Online_Desktop.state = "OFF") {
                timerSteamMachine = createTimer(now.plusSeconds(10)) [|
                        logInfo("timerSteamMachine", "Powering off steammachine power plug") // it's always nice to add a little logging so that we can actually see the the timer is being triggered
                        timerSteamMachine = null // cleanup timer object since we no longer need it
                        sendCommand(r135c, OFF)
                ]
        }
end
1 Like

Hello marcel_erkel and thanks for your answer. I removed my rule and added your modified version.
I understood the time variable now and also the logging part - thanks for it :slight_smile:

But the OFF command for the r135c is still not sent. Is this part correct ?
Do i need to set the state inside " " ?

if(Online_Desktop.state = "OFF")

Thank you.

Try == instead of =

if (Online_Desktop.state == OFF) {
1 Like