How to change an openhab switch with http-commands?

Hi,
my IP-Doorphone can send http-commands, when someone is ringing.

Can i control a switch inside openhab with an external http command?

Now i use the fritzbox binding, but there is a delay from pressing the button on my doorphone until the switch-status is changed.

Install the REST API Documentation from under the Misc tab in PaperUI. A new UI will show up on your landing page which includes the fully documented and interactive REST API calls that are possible.

Ok, i did this and it works, when i use the web-fields filled.

With this command i get the state:

http://192.168.xxx.xxx:8080/rest/items/light_switch_1/state

But how can i change the state? I get a long curl command, but when i enter this to my browser, then it doens´t work.

This will not work from a browser but only with curl, because you need to send a post or put.

To elaborate on Udo’s response. HTTP has a number of commands built into the protocol. When you put a URL in your address bar it is using a GET command. To command an Item you must use a POST and to update and Item you use a PUT.

1 Like

So it will not work?

Here is some explanation of the action url of the door phone.
http://wiki.snom.com/Category:HowTo:Action_URL

The phone can send only http get requests. But with this it is able to control something.

How is this working? To use this, do i have to set up a small script which will make the curl POST or PUT?

There is an unofficial workaround I think that somehow uses URLs associated with ClassicUI to send commands to Items. I couldn’t find the discussion of them with a quick search. You might find them with more diligent searching. Note that this is not a supported nor suggested way to do it and do not be surprised if it stops working at some point.

Beyond that, it won’t work. you must use POST or PUT to command or update an Item. You would have to set up a server that can receive the GET commands and turn around and make the appropriate POST or PUT commands to OH which is no small task. Not impossible but not as easy as just writing a shell script.

Here is the reference on how to still do that in OH2 from a standard GET request.
example from https://github.com/openhab/openhab1-addons/wiki/Samples-REST

http://192.168.1.1:8080/classicui/CMD?My_Switch=OFF

(the CMD moved to /classicui/CMD)

3 Likes

I know this thread is old, but I too was in need of a http GET - way to change the state of an item. I didn’t want to accept having to install the classicUI to do that, so here is another approach that works well for me:
If you happen to use the nginx proxy, you can add rules there:

location ~ /statechanger/POST/([^/]+)/([^/]+) {
    proxy_pass https://10.0.0.3:8443/rest/items/$1;
    proxy_method POST;
    proxy_set_body $2;
}

location ~ /statechanger/PUT/([^/]+)/([^/]+) {
   proxy_pass https://10.0.0.3:8443/rest/items/$1/state;
   proxy_method PUT;
   proxy_set_body $2;
}

after a “systemctl restart nginx”, you can change the state of Masterbedroom_Lights to OFF like this:
http://youropenhab:8443/statechanger/POST/Masterbedroom_Lights/OFF or PUT…

Update: for OH3, I had to add a line: (after proxy_set_body $2; ) to avoid errors stating something with unsupported media

    proxy_set_header 'content-type' 'text';
9 Likes