AndrewFG
(Andrew Fiddian-Green)
December 11, 2022, 5:03pm
1
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);
rossko57
(Rossko57)
December 11, 2022, 5:37pm
2
You have to do it indirectly, using REST API
This is OH2 posting, in OH3 you may need to take additional access security steps.
Here is the solution how to turn ON/OFF Thing via rules with help of RestAPI
sendHttpPutRequest("http://openhabianpi:8080/rest/things/network:pingdevice:15e46ce9/enable", "application/json", 'true')
where this network:pingdevice:15e46ce9 is thing ID taken for example from here
[thnigsn]
AndrewFG
(Andrew Fiddian-Green)
December 12, 2022, 1:18am
3
@rossko57 many thanks for the suggestion. Is the “sendHttpPutRequest” a standard built in method, or do I need to import it from somewhere?
rpwong
(Russ)
December 12, 2022, 2:25am
4
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
AndrewFG
(Andrew Fiddian-Green)
December 12, 2022, 3:23pm
5
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
rpwong
(Russ)
December 12, 2022, 4:31pm
6
Oops, yeah. Sorry about that. I’ve fixed it.
psych
(Mike)
October 21, 2023, 1:37pm
7
AndrewFG:
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)
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