[SOLVED] Controlling channel via REST api?

This is likely a dumb question since I’ve just started using OpenHab but I have a super simple software integration problem. I have a single thing (switch) and a simple channel (switch it on / off) and I would simply like to switch the switch on / off using the REST api from external software. The REST api looks nice and I can see the thing etc and channel types, but I can’t see an API to actually control the channel. Is it possible to switch the switch from external software via the REST api? If not, how can I do it? I control external software so can write any arbitrary code, just not sure how to control openHAB?


You need to link your channel to an item (see the docs if you’re unsure how to do this) and use the REST item API.

Try a HTTP POST such as http://(openhab server):8080/rest/item/(MySwitchItem) with payload of OFF or ON and it should control your device.

Thanks Daniel - that’s mostly my poor understanding of the data model. The paper UI had nicely created the item and I was just looking in the wrong place. I am successfully switching on / off now. I’m including here the final CURL command that worked for the next person as a reference as the URL structure is slightly different and it took me a while to get the incantation right. Overall thanks for helping with a fine product. This is a small test, and thinking I will probably move off our beloved but end of life staples connect hub once I work out how to do lutron devices. Danke!

curl --data-ascii OFF --header "Content-Type: text/plain" --request PUT http://mylocalhost:8080/rest/items/SprinklerSwitch_Switch/state

CORRECTION: that only changes the state of the switch on the panel, it doesn’t actually switch in on :frowning: This actually switches the switch ON/OFF

curl --data-ascii OFF --header "Content-Type: text/plain" --request POST http://mylocalhost:8080/rest/items/SprinklerSwitch_Switch

Well I spoke too soon. I was testing everything remotely and the curl statement above was Successfully toggling the switch ( as far as I could see from the REST api). Now that I am home I can see that it is actually not changing the state of the connected switch.Using the rest API updates the electronic state (and it changes the paperUI Control panel), but the switch itself doesn’t change. If I manually toggle the control panel, the physical switch changes state, but the rest API only electronically updates the switch panel. Is this a bug or am I doing something wrong?

I think you need to use POST instead of PUT (PUT updates item state, POST sends a command). See here and here.


Ahhh - thanks, you are of course right (you need post and you need the remove the ‘state’ from the end of the URL). In what situations would I want to change state of item but not affect connected channel/ thing? Everything is working now, so just curious, and thanks for prompt reply.

You would use POST when an external system is sending a command to OH e.g. an external system is trying to turn on/off a switch.

You would use PUT if an external system is communicating a status update e.g. an external system is updating the state of a sensor.