Simple HTTP Binding help?

Hello all!

I’ve been reading this board quite a bit and the helpfulness on this board amazes me compared to others I must say! kudos! Very nice to see people taking the time to help one another!

With that said :wink: I currently have my garage setup as follows,

http://ip/status.php shows a 0 or 1 based on if the door is open or closed.
http:///ip/garage=1 opens and closes the door.

I am looking to create a http sensor and switch that basically shows the following based on the above info,

Garage door: OPEN

Is this doable? can someone get me started on this then i should be able to replicate going forward…

Thanks for your time!!


I’ve a very similar setup. You need two Items, a Switch (to control the door) and a Contact (to get the status). I use HTTP to control the door and MQTT to report the door’s status but you could easily change it to use the HTTP binding or use the HTTP actions in a rule. I trigger the doors through a Rule rather than using the HTTP binding.

Here is my setup unedited to make it simpler. There are a lot of advanced concepts in the code below so ask questions.

The theory of operations is as follows:

I have two garage door openers and each opener has a reed switch wired up to a raspberry pi and a relay wired up to that same raspberry pi which is creatively named “garage”. On garage I have a script that polls the GPIO pins and reports any change in state of the reed switches to OH using MQTT. (For your approach you will need to instead set up an Item bound to an HTTP Binding that polls your status URL). To trigger a relay I issue a REST API call similar to what you describe. The big difference is because I have two openers I do some advanced rules stuff (lambdas) so I only have to write the code once. On the sitemap I use the visibility feature to change the icon to indicate whether the door is open or not and use mappings to provide just the one button to press instead of having a switch.


Switch  T_D_Garage1     "Garage Door 1"                 <garagedoor>
Switch  T_D_Garage2     "Garage Door 2"                 <garagedoor>

Contact N_D_GarageDoor1              "Garage Door 1 [MAP(]"          <garagedoor> (gDoorSensors, gRemindDoorSensors, gGarageSensors) { mqtt="<[mosquitto:entry_sensors/main/garage/door1:state:default]" }

Contact N_D_GarageDoor2              "Garage Door 2 [MAP(]"          <garagedoor> (gDoorSensors, gRemindDoorSensors, gGarageSensors) { mqtt="<[mosquitto:entry_sensors/main/garage/door2:state:default]" }


import org.openhab.core.library.types.*
import org.openahb.core.persistence.*
import org.openhab.model.script.actions.*
import org.joda.time.*
import org.eclipse.xtext.xbase.lib.*

val Functions$Function1 openGarage = [ String garageNum |

        logInfo("Garage Controller", "The garage door " + garageNum + " has been triggered")

    var url = ""
    switch garageNum {
        case "1" : url = url + "17"
        case "2" : url = url + "22"
        default : url = null

    if (url == null) {
        Notification_Proxy_Info.postUpdate("Received cmd to trigger unknown garage door: " + garageNum)
        else {
                url = url + "/sequence/500,01"
            logDebug("Garage Controller", "The URL is " + url)

                logInfo("Garage Controller", "HTTP Post result " + sendHttpPostRequest(url).toString)
                if (Present.state != ON) {
                        Notification_Proxy_Alarm.postUpdate("Garage door " + garageNum + " has been triggered.")
                if (gGarageNet.state != ON) {
                        Notification_Proxy_Alarm.postUpdate("The garage controller may be offline, trigger may have failed!")

rule "Trigger Garage Door Opener 1"
        Item T_D_Garage1 received command
//      Item T_D_Dash_Tide received update or
//      Item T_D_Dash_Bounty received update

rule "Trigger Garage Door Opener 2"
        Item T_D_Garage2 received command

Sitemap fragment:

                // Garage Door Openers
                Switch item=T_D_Garage1 label="Garage Door 1" icon="garagedoor-closed" mappings=[ON=Open] visibility=[N_D_GarageDoor1!="OPEN"]
                Switch item=T_D_Garage1 label="Garage Door 1" icon="garagedoor-open" mappings=[ON=Close] visibility=[N_D_GarageDoor1=="OPEN"]
                Switch item=T_D_Garage2 label="Garage Door 2" icon="garagedoor-closed" mappings=[ON=Open] visibility=[N_D_GarageDoor2!="OPEN"]
                Switch item=T_D_Garage2 label="Garage Door 2" icon="garagedoor-open" mappings=[ON=Close] visibility=[N_D_GarageDoor2=="OPEN"]

Well maybe, maybe there is a different approach based on your setup since they are similar,

So my garage door is controlled with a banana Pro and GPIO. GPIO pin 23 activates a relay to open and close the door. Another GPIO senses 3v from a magnet switch on the door. I also have the php scripts just running the gpio commands for additional integration. It sounds like you have the same setup with the PI reporting to OH via MQTT which I know nothing about. lol

SO maybe there is another approach lol mqtt stuff?

Just installed mosquito on the banana pro… :wink:

and my php code…

<?php if(isset($_GET['trigger']) && $_GET['trigger'] == 1) { error_reporting(E_ALL); exec('gpio mode 7 out'); exec('gpio write 7 1'); usleep(1000000); exec('gpio write 7 0'); usleep(4000000); } ?> Garage Opener


            <div class='awrap'>
  <a href='/?trigger=1'></a>
<?php // Set up valid status list $state[1] = "-=Open=-"; $state[0] = "-=Closed=-"; ?> Door is currently: <?php $pinStatus = trim(shell_exec("gpio -g read 23")); //returns 0 = low; 1 = high echo $state[$pinStatus];?>

You can find my MQTT reporting script here. It is written in Python and depends on WebIOPi for both the Python script and the REST API that OH calls to trigger the relay.

AHHH ok scratch that idea. WebIOPi wont compile on banana due to cross compiler issues. Guess its HTTP then lol. Do you have any HTTP samples for the GET of the door open/close and status?


There is a PHP MQTT library (probably more than one, this is just the first I found). You already have the PHP/GPIO stuff working so you can just update your script to report the changes instead of requiring a poll.

Or you can configure your PHP script to push the updates to OH through OH’s REST API.

I’m firmly of the opinion that it is far better to push data than poll for data over the network. Also, with MQTT you can set up a Last Will and Testament message so if your client ever goes offline the MQTT Broker will send a message and your OH can pick that up and alert you to your controller being offline. This has been a life saver for me.

Thanks. Ill dig around a bit and see what I come up with.

SO yea the REST API seems A LOT easier. I’m thinking after i create the items in open hab I should be able to update the status as such “http://localhost:8080/CMD?Garage=OPEN” ? kinda sorta. Then I just have to create a bash loop script with curl or wget to send the update as i rather not use the overhead of Apache and PHP if I don’t have to.

Yes, that is one of the reasons I went with my Python script. I can set my loop to be really tight (hundreds of milliseconds) so when it changes state I find out almost immediately yet I’m not using up lots of network bandwidth and server processing time with polling.

Once you get it to work you might consider doing the little extra work necessary to make it run as a service. Personally I found it easier to make it a service, keep track of state so I know when it changes, and monitor and control it using a higher level language. I chose Python but you could do the same in most anything (Ruby, Perl, Node.js, etc.).

Yea agreed. I can just convert it to a daemon with error and crash reporting and some watchdog. No biggy there. The simplicity of the API just blows my mind though after all the php and other crap i have had to do to get this going lol. That seems a hell of a lot easier.

Ok, got everything working and threw this script together for now…

#Get data from garage door
doorstatus="/usr/local/bin/gpio -g read 23"

echo Checking Garage Door Status
STATUS=$( $doorstatus | grep 0 )
if [ $STATUS -eq 0 ]; then
echo "Door is closed!"
echo "Door is open!"

exit 0

My question now is is their anyway to update the “CLOSED” or “OPEN” message on OH without refreshing the page?

then we just run - while sleep 2; do /etc/doorcheck; done for now :wink:

NM, the phone app updates immediately so thats all i need :wink: