REST/HTTP integration

Pretty new to OpenHAB2. Before I go to far I was looking for some advice on how to best integrate with HTTP/REST services. I have a system I built that brokers requests to different devices. The requests can be either status/sensor “get-value” requests or action events (turn on fireplace). My system works via REST. How do I create things to represent the various REST endpoints?

Could you provide a sample CURL command, which triggers your fireplace?

I have to flavors of URLs, both just use GETs

http://pizero1/fireplace/on (just return a status but turns fireplace on)
http://pizero1/temp/ (returns JSON with a value attribute)

Install HTTP binding and add the following item:

Switch outputmodule10 { http=">[ON:GET:http://pizero1/fireplace/on] >[OFF:GET:http://pizero1/fireplace/off]" }

change the name to whatever you like, I copied it from mine.

Your temp values can only be loaded in intervalls. Direct push is only possible, if you can modify the application which is running on your “pizero1”. Just wondering: What’s a pizero? lol

1 Like

If I was OK with just polling on intervals how would I configure that, or even better yet, where can I go to learn how I would configure it :slight_smile:

There are different ways:

  1. Modify the IOT Device.
    You could modify your application running on the rpi zero to push the values. That’s a good idea if you plan to use motion sensors, contacts or switches with the rpi zero. If a state changes, it gets pushed instant to openhab. You won’t have any delays. You can explore the REST api of openhab on your openhab webpage. Just click on REST instead of habpanel or basic UI. You can then see all the different api commands.

I use this option to push states of doors to openhab. My script is written in python and uses the following functions:

def basic_header():
    """ Header for OpenHAB REST request - standard """
    auth = base64.encodestring('openhab:openhab').replace('\n', '')
    return {
            "Authorization" : "Basic %s" %auth,
            "Content-type": "text/plain"}

def SendUpdate(type, id, state):
	if type == 1: 
		url = 'http://192.168.100.101/rest/items/inputmodule1%s/state'%(id)
		if state==1:
			state="OPEN"
		else:
			state="CLOSED"

	else:		
		url = 'http://192.168.100.101/rest/items/inputmodulean1%s/state'%(id)
	req = requests.put(url, data=str(state), headers=basic_header())
	if req.status_code != requests.codes.ok:
		req.raise_for_status()  

You get get the idea of what’s happening. Modify it, so that it fits your needs.

  1. Configure openhab to pull states every x minutes
    This way you will always have a delay. For temperature it’s okay, but I won’t use it for motion detectors or similar devices. I never did this by myself. At least not with json transformation.
    Check out those pages:
https://openhabdoc.readthedocs.io/de/latest/Binding_HTTP/
https://github.com/openhab/openhab1-addons/wiki/Transformations#java-script-transformation-service

EDIT: I will try to translate the most importaant parts into english:
In the shown example the TV tuner has a json interface. If you HTTP GET http://10.90.30.100:8080/tv/getTuned you will receive the following output:

{ "callsign": "KSBW",
   "date": "20131126",
   "duration": 3600,
   "isOffAir": false,
   "isPclocked": 3,
   "isPpv": false,
   "isRecording": false,
   "isVod": false,
   "major": 8,
   "minor": 65535,
   "offset": 3157,
   "programId": "11269591",
   "rating": "No Rating",
   "startTime": 1385485200,
   "stationId": 3910459,
   "status": {     "code": 200,     "commandResult": 0,     "msg": "OK.",
   "query": "/tv/getTuned"   },
   "title": "Today"
 }

To handle this, you need the following item:

String DirecTV1_Channel "current channel" { http="<[http://10.90.30.100:8080/tv/getTuned:30000:JS(getValue.js)]" }

You need a transformation rule. Put a file called getValue.js in your transform directory. Put the following contents in it:

JSON.parse(input).title;

title is the JSON field.

Modify everything to match your needs. I did not try this by myself, like I said. Good luck!
BTW: It fucked up my markup. Sorry for that, hope you can find your way throught my post.

1 Like