DSL rule example for enabling/disabling a thing

The ‘How Do I…’ page has an example for enabling/disabling a thing. But it only shows the Python code and the Rules DSL example is ‘TO DO’.

=> Can anyone please help me convert that example to Rules DSL? In particular I am struggling with importing the OSGI package, and using it to get the ThingManager…

import org.openhab.core.thing.ThingUID
// TODO how to import .. core osgi ??

val klfHome = new ThingUID("velux:klf200:home")

// TODO how to get the Thing Manager 
val thingMgr = osgi.get_Service("org.openhab.core.thing.ThingManager")

thingMgr.setEnabled(klfHome, true);

You have to do it indirectly, using REST API

This is OH2 posting, in OH3 you may need to take additional access security steps.

@rossko57 many thanks for the suggestion. Is the “sendHttpPutRequest” a standard built in method, or do I need to import it from somewhere?

It’s in the core.

Here’s what you need for RulesDSL:

val headers = newHashMap("Authorization" -> "Bearer INSERT_API_TOKEN", "WWW-Authenticate"-> "Basic")
//enable the thing
sendHttpPutRequest("http://INSERT_IP:8080/rest/things/INSERT_THING/enable", "text/plain", 'true', headers, 1000)
//disable the thing
sendHttpPutRequest("http://INSERT_IP:8080/rest/things/INSERT_THING/enable", "text/plain", 'false', headers, 1000)
2 Likes

Many thanks @rpwong for the advice. I think however that your suggestion to send disable = true is not correct, but should rather be enable = false.

The solution that works for me (on 3.4.Mx) is as follows…

val headers = newHashMap("Authorization" -> "Bearer XXXXXX", "WWW-Authenticate"-> "Basic")
val url = "http://192.168.x.xxx:8080/rest/things/velux%3Aklf200%3Ahome/enable"
// enable
sendHttpPutRequest(url, "text/plain", "true", headers, 5000)
// disable
sendHttpPutRequest(url, "text/plain", "false", headers, 5000)
1 Like

Oops, yeah. Sorry about that. I’ve fixed it.

This fails for me with the following error:

15:35:19.984 [ERROR] [penhab.core.model.script.actions.HTTP] - Fatal transport error: java.util.concurrent.ExecutionException: org.eclipse.jetty.client.HttpResponseException: HTTP protocol violation: Authentication challenge without WWW-Authenticate header