[SOLVED] HTTP disable thing from rule authentication error

I would like to disable/ enable things from a rule
This is the command I use
sendHttpPutRequest(“http://192.168.123.10:8080/rest/things/ipcamera:onvif:OnvifCamera/enable”, “text/plain”, ‘false’)
And this is the error I am getting in OH log file
2022-02-23 14:18:00.935 [ERROR] [enhab.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

Using API explorer enable/disable the thing works fine.
As usual I expect the problem to be in front of the computer. What am I doing wrong?

Interaction with any part of the REST API except for Items requires authentication. You either need to create:

  • an API token and append that as a header in your request
  • use the Karaf console and the Exec binding instead of the REST API
  • using ECMAScript you have access to the ThingManager where you can enable/disable the Thing without leaving your rule at all.

It works from the API explorer because you’ve already logged in.

Thank you for the hint! I could make it work.

val headers = newHashMap(“Authorization” → “Bearer oh.Things.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”, “WWW-Authenticate”-> “Basic”)
sendHttpPutRequest(“http://192.168.123.10:8080/rest/things/ipcamera:onvif:OnvifCamera/enable”, “text/plain”, ‘false’, headers, 5000)

Hi Rich,

could you give me a hint how to use the ThingManager in ECMAScript? I was not able to find a documentation or sample on it.
This would be awesome

Which one? 5.1 or 2021?

Its OH 3.2. So I think 2021

It tells you.

You have to install a separate add-on for 2021.

In ECMAScript 5.1 you can access the ThingManager. You’ll need to pull that service through the OSGI APIs. It would be something like

var FrameworkUtil = Java.type("org.osgi.framework.FrameworkUtil");
var _bundle = FrameworkUtil.getBundle(scriptExtension.class);
var bundle_context = _bundle.getBundleContext();
var ThingManager = bundle_context.getServiceReference("org.openhab.core.thing.ThingManager");
var ThingUID = Java.type('org.openhab.core.thing.ThingUID');

ThingManager.setEnabled(new ThingUID('thing:uid:string'), false); // disable Thing

Thanks a lot. I tried now with ECMAScript 5.1 but get the following Error.
TypeError: ThingManager.setEnabled is not a function in at line number 7

This is my code
var FrameworkUtil = Java.type(“org.osgi.framework.FrameworkUtil”);
var _bundle = FrameworkUtil.getBundle(scriptExtension.class);
var bundle_context = _bundle.getBundleContext();
var ThingManager = bundle_context.getServiceReference(“org.openhab.core.thing.ThingManager”);
var ThingUID = Java.type(‘org.openhab.core.thing.ThingUID’);

ThingManager.setEnabled(new ThingUID(‘homematic:bridge:8472febe2e’), false); // disable Thing

Then I don’t know what could be wrong. That’s how it’s supposed to work but I haven’t used ECMAScript 5.1 in quite some time. It’s way easier in ECMAScript 2021 and since 5.1 is definitely going to go away at some point it’s probably not the best use of one’s time.

The Java Docs can be found at ThingManager (openHAB Core 3.3.0-SNAPSHOT API)

Thank you so much for your help. I am a kind of newby to ECMA scripts.
Do you perhaps have a short sample how it should be done in ECMAScript 2021?
I only get items and so on but when I start with things. I do not get any completion suggestions

Well, it’s not really usual to mess with Things from rules, which is why the API isn’t really there and straight forward. What are you really trying to accomplish? Typically you would be working with Items, not Things.

With the JS Scripting add-on installed it should be something like

var thingMgr = osgi.getService('org.openhab.core.thing.ThingManager');
var ThingUID = Java.type('org.openhab.core.thing.ThingUID');
thingMgr.setEnabled(new ThingUID('thing:uid:string'), false);
5 Likes

That works. Thank you so much.
after reboot my Homematic takes too long to come up. So I need to disable and enable it again after a couple of seconds.

thanks, works great! Should be part of the “But How Do I…?”, much easier than the REST workaround. :wink:

Hallo.
Habe das selbe Problem wie Du. Kannst Du mir sagen welche Dateien ich anlegen muss usw. um das Thing robonect.things, bzw den String"robonect:mower:automower" über einen Schalter Disable und danach wieder Enablen kann?
Danke.
Gruß Tobi

This doesn’t work for me:
14:47:19.359 [ERROR] [.internal.handler.ScriptActionHandler] - Script execution of rule with UID 'test-1' failed: The name 'osgi' cannot be resolved to an item or type; line 32, column 20, length 4 in test

With what add-on? That post from over a year ago was for Nashorn JavaScript. For the newer add-on, see the add-on docs: JavaScript Scripting - Automation | openHAB

1 Like