Enable or disable the Hue Bridge module as needed without permanent log error/warn entries

OH 2.5.8
OpenHabian

Project:
Because I’m currently not using the Hue Bridge and three Things (Lampe1, Lampe2, PlugIn1) and I power off the bridge, some error messages are logged. To reduce this to a one-time event, I thought about the “module Hue Bridge”. It works, but is a bit of “spaghetti code”. Is there a better solution?

With this modul switch it is possible to disconnect the Hue Bridge from power or disable LAN. If the Hue Bridge is manually removed from the power or the bridge is not accessible for other reasons, OpenHAB sets the IsModulHueActiv item to “OFF”. If the IsModulHueActiv switch item is set to “ON”, OpenHAB will attempt to reconnect the bridge. If this does not work, IsModulHueActiv is set to “OFF” again

OpenHAB-modul-hue-on-off

OpenHAB-modul-hue-on-off-2

classic/Icon for Hue Bridge
philipshuebridge

Hue - Bridge - Things
Overview Hue Bridge

// Die Hue Bridge. Benötigt hue binding
Bridge hue:bridge:ecb5fa2f2064 [ ipAddress="192.168.x.xxx", userName="<api-id>" ] {
    0100  bulb1  "Lampe I (0100)"   @ "Studio"    [ lightId="1" ]
    0210  bulb2  "Lampe II (0210)"  @ "Studio"    [ lightId="2" ]
    0010  plug1  "Strom PlugIn 1 (0010)"  @ "Studio"    [ lightId="3" ]
}

// Ping zur Hue Bridge (Verfügbarkeit) prüfen. Benötigt netzwerk binding
network:pingdevice:huebridge [ hostname="192.168.x.xxx", retry=1, timeout=5000, refreshInterval=60000 ]

.sitemap - Hue Lamps use

Group item=C_Studio visibility=[IsModulHueActiv=="ON"] {

    Frame label="Hue Lampen Status s/w" icon="none" visibility=[IsModulHueActiv=="ON"] {
	Switch item=dimmerStudioLight1 label="Lampe 1 - Schalter" 
	Slider item=dimmerStudioLight1 label="Lampe 1 - Dimmer" 
    }

    Frame label="Hue Lampen Status farbe" icon="none" visibility=[IsModulHueActiv=="ON"]{
	Switch item=switchStudioLight2 label="Lampe 2 -Schalter" 
	Slider item=dimmerColorStudioLight2 label="Lampe 2 - Dimmer" 
	Colorpicker item=dimmerColorTempStudioLight2 label="Lampe 2 - Dimmer-Colortemp."
    }

    Frame label="Hue Stromstecker 1" icon="none" visibility=[IsModulHueActiv=="ON"] {
	Switch item=switchStudioPlugIn1
    }


}

.sitemap - Modul Settings

Frame label="Hue-Bridge" icon="none" {
Switch item=IsModulHueActiv
Text item=HueBridgeOnline icon="philipshuebridge"
Text item=Light1StateReachable valuecolor=[=="false"="red",=="true"="green",!=dummy="orange"] icon="wallswitch"
Text item=Light2StateReachable valuecolor=[=="false"="red",=="true"="green",!=dummy="orange"] icon="wallswitch"
Text item=PlugIn1StateReachable valuecolor=[=="false"="red",=="true"="green",!=dummy="orange"] icon="poweroutlet"

}

Rules
hue-bridge-changed.rules

rule "Hue Bridge Status changed (hue-bridge-changed.rules)"
when 
    Thing "hue:bridge:ecb5fa2f2064" changed from ONLINE to OFFLINE  // Verbindung zur Hue Bridge unterbrochen. 
then
    logError("hue:bridge:", "Power off or lost connection ? Hue Brigde Offline") 
    IsModulHueActiv.postUpdate(OFF)
end	

rule "Hue Bridge rule: Check state.on and state.reachable every minute (hue-bridge-changed.rules)"
when 
    Time cron "0 */1 * ? * *" // every minute. Source: https://crontab.guru/examples.html
then

   if (IsModulHueActiv.state == ON ) {
    

        // val headers = newHashMap("Cache-control" -> "no-cache")
        // https://community.openhab.org/t/solved-any-hint-how-to-disable-http-items-when-http-fails-or-catch-the-error-without-any-log-entry/104289/7

        // get from Hue Bridge: Light 1 state.on (return is String)
        val Light1JSON = sendHttpGetRequest("http://192.168.x.xxx/api/<api-id>/lights/1", 60000)
        if ( Light1JSON === null ) {
            logInfo("Light1JSON:", "return is null - Hue Brigde Offline") 

            // Disable Hue Bridge
            IsModulHueActiv.postUpdate(OFF)

            // Disable all Hue Things Items - force update sitemap
            Light1StateOn.postUpdate("false")
            Light1StateReachable.postUpdate("false")
            Light2StateOn.postUpdate("false")
            Light2StateReachable.postUpdate("false")
            PlugIn1StateReachable.postUpdate("false")

        } else {
            // logInfo("Light1JSON:","return is not null")  

            if ( transform("JSONPATH", "$.state.on", Light1JSON) == "true") {
              Light1StateOn.postUpdate("true")
            } else {
              Light1StateOn.postUpdate("false")
            }

            // get from Hue Brige: Light 1 state.reachable
            if ( transform("JSONPATH", "state.reachable", Light1JSON) == "true") {
              Light1StateReachable.postUpdate("true")
            } else {
              Light1StateReachable.postUpdate("false")
            }
        } 

        // get from Hue Bridge: Light 2 state.on (return is String)
        val Light2JSON = sendHttpGetRequest("http://192.168.x.xxx/api/<api-id>/lights/2", 60000)
        if ( Light2JSON === null ) {
            logInfo("Light2JSON:", "return is null - Hue Brigde Offline") 
            IsModulHueActiv.postUpdate(OFF)
        } else {
            // logInfo("Light2JSON:","return is not null")  

            if ( transform("JSONPATH", "$.state.on", Light2JSON) == "true") {
              Light2StateOn.postUpdate("true")
            } else {
              Light2StateOn.postUpdate("false")
            }

            // get from Hue Brige: Light 2 state.reachable
            if ( transform("JSONPATH", "state.reachable", Light2JSON) == "true") {
              Light2StateReachable.postUpdate("true")
            } else {
              Light2StateReachable.postUpdate("false")
            }
        } 

        // get from Hue Bridge: Light 3 = PlugIn1 state.on (return is String)
        val PlugIn1JSON = sendHttpGetRequest("http://192.168.x.xxx/api/<api-id>/lights/3", 60000)
        if ( PlugIn1JSON === null ) {
            logInfo("PlugIn1JSON:", "return is null - Hue Brigde Offline") 
            IsModulHueActiv.postUpdate(OFF)
        } else {
            // logInfo("PlugIn1JSON:","return is not null")  

            // get from Hue Brige: Light 3 state.reachable
            if ( transform("JSONPATH", "state.reachable", PlugIn1JSON) == "true") {
              PlugIn1StateReachable.postUpdate("true")
            } else {
              PlugIn1StateReachable.postUpdate("false")
            }
        } 



   }
   
end

modul.rules

/**
* Modul Konfiguration "ON" / "OFF" (siehe modul.items)
*/
rule "Modul Konfigiguration Init (modul.rules)"
when
    System started 
then 

    createTimer(now.plusMinutes(2), [ |  //Give the system time to load up
      if (IsModulHueActiv.state == NULL || IsModulHueActiv.state == UNDEF) { 
          IsModulHueActiv.postUpdate(OFF)
      }
    ])

end

/**
* Disable Hue Things using REST API
*/
rule "Modul Konfigiguration changed to OFF (modul.rules)"
when
    Item IsModulHueActiv changed to OFF
then 

    if (IsModulHueActiv.state == OFF) {
        sendHttpPutRequest("http://192.168.x.xxx:8080/rest/things/hue:bridge:ecb5fa2f2064/enable", "application/json", 'false') 
    }

end

/**
* Enable Hue Things using REST API
*/
rule "Modul Konfigiguration changed to ON (modul.rules)"
when
    Item IsModulHueActiv changed to ON
then 

    if (IsModulHueActiv.state == ON) {
        sendHttpPutRequest("http://192.168.x.xxx:8080/rest/things/hue:bridge:ecb5fa2f2064/enable", "application/json", 'true') 
    }

end

Items
hue.items

// Hue Bridge Service
Switch HueBridgeOnline "Bridge Status [%s]" { channel="network:pingdevice:huebridge:online" }

// Lampe I (0100) @ "Studio" 
Dimmer dimmerStudioLight1  { channel="hue:0100:1:bulb1:brightness" }

String Light1StateOn "Lampe 1 - OH Schalter [MAP(huebridge.map):%s]" 
String Light1StateReachable "Lampe 1 - Strom Aus/Einschalter [MAP(huebridge.map):%s]" 

String Light2StateOn "Lampe 2 - OH Schalter [MAP(huebridge.map):%s]" 
String Light2StateReachable "Lampe 2 - Strom Aus/Einschalter [MAP(huebridge.map):%s]" 


// Lampe II (0210) @ "Studio" 
// https://community.openhab.org/t/hue-hsb-how-to-find-correct-color-warm-cold-white/82964/8
Switch switchStudioLight2  "Colorlampe Schalter" [ "Lighting" ] { channel="hue:0210:ecb5fa2f2064:bulb2:color" }
Dimmer dimmerColorStudioLight2 "Colorlampe Dimmer" [ "Lighting" ] { channel="hue:0210:ecb5fa2f2064:bulb2:color" }
Color colorStudioLight2  "Colorlampe Farbe" [ "Lighting" ] { channel="hue:0210:ecb5fa2f2064:bulb2:color" }
Dimmer dimmerColorTempStudioLight2 "Colorlampe Farbentempersatur" [ "Lighting" ] { channel="hue:0210:ecb5fa2f2064:bulb2:color_temperature" }

// Strom PlugIn I (0010) @ "Studio" 
Switch switchStudioPlugIn1  "Strom PlugIn 1" [ "poweroutlet" ] { channel="hue:0010:ecb5fa2f2064:plug1:switch" }
String PlugIn1StateReachable "Strom PlugIn 1 - Erreichbar [MAP(huebridgeplug.map):%s]" 

modul.items

/**
* Modul Konfiguration "ON" / "OFF" (siehe modul.rules)
*/
Switch IsModulHueActiv "Modul Hue Bridge ist: [%s]" <switch> 

.transform
huebridge.map

false=AUS
true=AN
'false'=AUS
'true'=AN
-=OFFLINE
NULL=OFFLINE

Regards,
Thomas

You can rename the thing and all other files. You will then need either two sire maps or a dummy items file. That is unless you don’t need the hue binding you could log into karaf and stop it.

Why not just leave it on?

True. Manual renaming would also be a possibility. I wanted to try to realize a real switch on/off of modules by pressing a button. Furthermore, devices that are not needed at times should not consume electricity.