# Python module for easily accessing the openHAB REST API

**URL:** <https://community.openhab.org/t/python-module-for-easily-accessing-the-openhab-rest-api/19449>\
**Category:** Runtime\
**Tags:** python, openhab2\
**Created:** [January 4, 2017, 12:22am UTC](https://community.openhab.org/t/python-module-for-easily-accessing-the-openhab-rest-api/19449 "2017-01-04T00:22:57Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![sim0nx](https://community.openhab.org/user_avatar/community.openhab.org/sim0nx/32/71872_2.png) [@sim0nx](https://community.openhab.org/u/sim0nx)\
**Post date:** [January 4, 2017, 12:22am UTC](https://community.openhab.org/t/python-module-for-easily-accessing-the-openhab-rest-api/19449/1 "2017-01-04T00:22:57Z")

</div>

Quite some time ago I wrote a simple python library for interacting with openHAB1, which I am still using in various scripts for updating item states and sending commands etc.

Since I have recently switched to openHAB2, I have updated and considerably reworked and polished the library.

In case anybody is interested in using python for openHAB2 automation, give it a try:  
[https://github.com/sim0nx/python-openhab](https://github.com/sim0nx/python-openhab)

The main purpose of the library is to interact with items, thus that is currently the only part which is implemented.  
Work is in progress and any comments or requests are more than welcome.

Cheerz

---

<div class="post-metadata">

**Author:** ![tnemrap](https://community.openhab.org/user_avatar/community.openhab.org/tnemrap/32/61523_2.png) [@tnemrap](https://community.openhab.org/u/tnemrap)\
**Post date:** [March 17, 2017, 7:52pm UTC](https://community.openhab.org/t/python-module-for-easily-accessing-the-openhab-rest-api/19449/2 "2017-03-17T19:52:20Z")

</div>

Is this supporting post update?

/Mike

---

<div class="post-metadata">

**Author:** ![sim0nx](https://community.openhab.org/user_avatar/community.openhab.org/sim0nx/32/71872_2.png) [@sim0nx](https://community.openhab.org/u/sim0nx)\
**Post date:** [March 18, 2017, 9:58pm UTC](https://community.openhab.org/t/python-module-for-easily-accessing-the-openhab-rest-api/19449/3 "2017-03-18T21:58:38Z")

</div>

Both put and post are supported

---

<div class="post-metadata">

**Author:** ![Marty56](https://community.openhab.org/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@Marty56](https://community.openhab.org/u/Marty56)\
**Post date:** [March 25, 2017, 5:47am UTC](https://community.openhab.org/t/python-module-for-easily-accessing-the-openhab-rest-api/19449/4 "2017-03-25T05:47:42Z")

</div>

Hi sim0nx,

I tried your code and I am receiving the following error when using  
items = openhab.fetch\_all\_items()

```php
>>> ================================ RESTART ================================
>>> 

Traceback (most recent call last):
  File "/Users/Guest/Public/python-openhab-master/test.py", line 34, in <module>
    items = openhab.fetch_all_items()
  File "/Users/Guest/Public/python-openhab-master/openhab/openhab.py", line 145, in fetch_all_items
    items[i['name']] = self.json_to_item(i)
  File "/Users/Guest/Public/python-openhab-master/openhab/openhab.py", line 180, in json_to_item
    return NumberItem(self, json_data)
  File "/Users/Guest/Public/python-openhab-master/openhab/items.py", line 45, in __init__
    self.init_from_json(json_data)
  File "/Users/Guest/Public/python-openhab-master/openhab/items.py", line 57, in init_from_json
    self.__set_state(json_data['state'])
  File "/Users/Guest/Public/python-openhab-master/openhab/items.py", line 100, in __set_state
    self._state = self._parse_rest(value)
  File "/Users/Guest/Public/python-openhab-master/openhab/items.py", line 198, in _parse_rest
    return float(value)
ValueError: could not convert string to float: gruen

```

The item probably causing this error has the following json representation.

```csv
    "link": "http://nac:8080/rest/items/Alarm_scharf_Diode",
    "state": "gruen",
    "stateDescription": {
      "pattern": "",
      "readOnly": false,
      "options": []
    },
    "type": "Number",
    "name": "Alarm_scharf_Diode",
    "label": "Alarm scharf",
    "category": "shield",
    "tags": [],
    "groupNames": [
      "Diodendisplay"
    ]
  },

```

I am using a transformation file containing the following contents

```csv
0=aus
1=rot
2=gruen
3=orange
NULL=NULL

```

It seems that the json file is containing the transformed value and not the numerical value which is 2 in my example

---

<div class="post-metadata">

**Author:** ![sim0nx](https://community.openhab.org/user_avatar/community.openhab.org/sim0nx/32/71872_2.png) [@sim0nx](https://community.openhab.org/u/sim0nx)\
**Post date:** [March 29, 2017, 8:50pm UTC](https://community.openhab.org/t/python-module-for-easily-accessing-the-openhab-rest-api/19449/5 "2017-03-29T20:50:42Z")

</div>

Hi Marty56,

I agree with your observation. Though I don’t quite understand why openhab is doing this, that doesn’t make sense to me.  
I try to be somewhat “smart” by mapping openhab types to python types… which is also used for input validation.

That behavior of openhab breaks this.

To be hones I don’t see how I could work around this :-/

Don’t you have an identical item with real values ?

---

<div class="post-metadata">

**Author:** ![Marty56](https://community.openhab.org/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@Marty56](https://community.openhab.org/u/Marty56)\
**Post date:** [March 30, 2017, 12:43am UTC](https://community.openhab.org/t/python-module-for-easily-accessing-the-openhab-rest-api/19449/6 "2017-03-30T00:43:41Z")

</div>

Hi sim0nx,

I have been using a pretty simple approach which seems to be working for string items.

```csv
import requests

#write String Item
data = 'some data'
requests.put('http://<ip o f OH of >:8080/rest/items/<item name>/state', data=data)

# retrieve String item
r = requests.get('http://<ip o f OH of >:8080/rest/items/<item name>/state')
data = r.content

```

---

<div class="post-metadata">

**Author:** ![KataPi](https://community.openhab.org/letter_avatar_proxy/v4/letter/k/91b2a8/32.png) [@KataPi](https://community.openhab.org/u/KataPi)\
**Post date:** [July 3, 2017, 8:01am UTC](https://community.openhab.org/t/python-module-for-easily-accessing-the-openhab-rest-api/19449/7 "2017-07-03T08:01:05Z")

</div>

Hi All  
(my first post)

@sim0nx I like your work.  
I’m a noob but I’d like to learn this module.  
Can you give some exemples for switch and contact?  
I’m running openhab2 on raspberry pi 2 and I try to turn on/off a led.

My wrong code is

Items  
`Switch led "Led" (Lights) { gpio="pin:16" }`

python-openhab

```
from openhab import openHAB
base_url = 'http://localhost:8080/rest'
openhab = openHAB(base_url)
# fetch all items
items = openhab.fetch_all_items()

gg = items.get('led')
print(gg.state)
gg.on()

```

OH2 printe OFF state and turn ON my switch but don’t turn ON my led  
events.log  
`[ItemStateChangedEvent] - led changed from OFF to ON`

When I switch through BasicUI or Android app led turn ON  
events.log

```
[ItemCommandEvent] - Item 'led' received command ON
[ItemStateChangedEvent] - led changed from OFF to ON

```

…and led works.

Someone can help me?

---

<div class="post-metadata">

**Author:** ![sim0nx](https://community.openhab.org/user_avatar/community.openhab.org/sim0nx/32/71872_2.png) [@sim0nx](https://community.openhab.org/u/sim0nx)\
**Post date:** [July 16, 2017, 2:01pm UTC](https://community.openhab.org/t/python-module-for-easily-accessing-the-openhab-rest-api/19449/8 "2017-07-16T14:01:41Z")

</div>

Hi @KataPi thanks and sorry for the delay!

Feel free to open issues for any problems you encounter on my github page (i tend to be quicker to respond there):

> **[GitHub - sim0nx/python-openhab: python library for accessing the openHAB REST API](https://github.com/sim0nx/python-openhab)**
>
> python library for accessing the openHAB REST API

The problem of what you are seeing is that there are two ways to change a state.

1. sending a command
2. updating the state

on() indeed just updated the state, which was working fine for me, which is why I did not realise that this is a bug.  
I have fixed it to send a command (as happens when you use the basicui / android app), so the latest version of the library (2.2) should work fine for you.

I also added a few more examples.

Could you please give it a try and let me know ?

---

<div class="post-metadata">

**Author:** ![rbpaul](https://community.openhab.org/user_avatar/community.openhab.org/rbpaul/32/35375_2.png) [@rbpaul](https://community.openhab.org/u/rbpaul)\
**Post date:** [January 16, 2018, 5:33pm UTC](https://community.openhab.org/t/python-module-for-easily-accessing-the-openhab-rest-api/19449/9 "2018-01-16T17:33:32Z")

</div>

Marty56,

I really like your solution. I can update text in String items, receive the text state of String items and other items (Dimmer, DateTime, Number, etc).

But I can’t send a command with this approach. Have you any suggestions for sending commands with this approach?

---

<div class="post-metadata">

**Author:** ![Marty56](https://community.openhab.org/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@Marty56](https://community.openhab.org/u/Marty56)\
**Post date:** [January 16, 2018, 6:18pm UTC](https://community.openhab.org/t/python-module-for-easily-accessing-the-openhab-rest-api/19449/10 "2018-01-16T18:18:46Z")

</div>

I am afraid no. This functionality was removed from OH 2.0 as far as I recall.  
But I could be wrong. I am not using OH anymore.

---

<div class="post-metadata">

**Author:** ![sim0nx](https://community.openhab.org/user_avatar/community.openhab.org/sim0nx/32/71872_2.png) [@sim0nx](https://community.openhab.org/u/sim0nx)\
**Post date:** [January 21, 2018, 6:17pm UTC](https://community.openhab.org/t/python-module-for-easily-accessing-the-openhab-rest-api/19449/11 "2018-01-21T18:17:12Z")

</div>

@rbpaul, have a look at my library [GitHub - sim0nx/python-openhab: python library for accessing the openHAB REST API](https://github.com/sim0nx/python-openhab) … it’s a wrapper for the OH API. You can send updates as well as commands easily…which is of course still supported by the API.

---

<div class="post-metadata">

**Author:** ![Aaron23](https://community.openhab.org/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@Aaron23](https://community.openhab.org/u/Aaron23)\
**Post date:** [September 4, 2018, 10:34pm UTC](https://community.openhab.org/t/python-module-for-easily-accessing-the-openhab-rest-api/19449/12 "2018-09-04T22:34:44Z")

</div>

Georges,

Thank you for providing the python-openhab module. It’s been very helpful for a project I’m working on which is a basic watchdog/monitor for openHAB using a Pi Zero W. When I started testing various scenarios where openHAB is disconnected (host powered off or service stopped), I notice that fairly often the Python application hangs. I’ve caught a couple of stack traces using gbd and they each look a little different. In Python 2.7 the hang is in a socket receive in socket.py. In Python 3.5.3, the hang is in acquiring a lock in threading.py. Have you seen anything like this before? Are there any timeout settings I can experiment with?

Thank You,

Aaron.

---

<div class="post-metadata">

**Author:** ![Aaron23](https://community.openhab.org/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@Aaron23](https://community.openhab.org/u/Aaron23)\
**Post date:** [September 8, 2018, 4:11pm UTC](https://community.openhab.org/t/python-module-for-easily-accessing-the-openhab-rest-api/19449/13 "2018-09-08T16:11:29Z")

</div>

Turns out this is simply because timeouts aren’t being used on requests, so they may hang indefinitely. I submitted a PR that fixes this issue for me: [Add timeout capability for requests. by hexamer · Pull Request #3 · sim0nx/python-openhab · GitHub](https://github.com/sim0nx/python-openhab/pull/3)

I think the hang in threading.py was due to a separate issue in my code. In Python 3.5, I would more often get socket hangs.

---

<div class="post-metadata">

**Author:** ![sim0nx](https://community.openhab.org/user_avatar/community.openhab.org/sim0nx/32/71872_2.png) [@sim0nx](https://community.openhab.org/u/sim0nx)\
**Post date:** [September 9, 2018, 10:03pm UTC](https://community.openhab.org/t/python-module-for-easily-accessing-the-openhab-rest-api/19449/14 "2018-09-09T22:03:44Z")

</div>

Hi Aaron, I am glad you like library 🙂 and it is of use 🙂  
I have merged your PR thanks for that! Nice catch btw

If there are any features you might be missing or you have any feedback, please let me know. I am not always super responsive but I am happy to implement / fix whatever is requested.  
For the moment the lib works for what I need it for that’s why there are some parts missing (like item types)… but they should be fairly easy to add.

cheers

---

<div class="post-metadata">

**Author:** ![Aaron23](https://community.openhab.org/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@Aaron23](https://community.openhab.org/u/Aaron23)\
**Post date:** [September 10, 2018, 12:45am UTC](https://community.openhab.org/t/python-module-for-easily-accessing-the-openhab-rest-api/19449/15 "2018-09-10T00:45:31Z")

</div>

> [@sim0nx](#):
>
> For the moment the lib works for what I need it for that’s why there are some parts missing

It it just what I needed for my application. Sure, I could have just made the HTTP transactions myself, but this takes care of that for me. Maybe some day I’ll come up with another need, but until then this module is perfect.

---

<div class="post-metadata">

**Author:** ![sorenmc4](https://community.openhab.org/letter_avatar_proxy/v4/letter/s/278dde/32.png) [@sorenmc4](https://community.openhab.org/u/sorenmc4)\
**Post date:** [October 5, 2018, 12:20pm UTC](https://community.openhab.org/t/python-module-for-easily-accessing-the-openhab-rest-api/19449/16 "2018-10-05T12:20:20Z")

</div>

Continuing the discussion from [Python module for easily accessing the openHAB REST API](https://community.openhab.org/t/python-module-for-easily-accessing-the-openhab-rest-api/19449):

Hi sim0nx!  
I am having trouble with sending a command/update/state to a number item in openhab.  
I can read the state of the item perfectly fine and print it in python.  
When i try to send the data to change the number item it complains about the data type.  
i tried the following commands:

temperature.command(temp)  
temperature.update(temp)  
temperature.state = temp  
temperature.state(temp)

And i tried casting the number temp into str, float and int.  
What is the correct way to do this?

---

<div class="post-metadata">

**Author:** ![sim0nx](https://community.openhab.org/user_avatar/community.openhab.org/sim0nx/32/71872_2.png) [@sim0nx](https://community.openhab.org/u/sim0nx)\
**Post date:** [October 5, 2018, 12:28pm UTC](https://community.openhab.org/t/python-module-for-easily-accessing-the-openhab-rest-api/19449/17 "2018-10-05T12:28:36Z")

</div>

Hi Soren,

Could you please open an issue over at github ([GitHub - sim0nx/python-openhab: python library for accessing the openHAB REST API](https://github.com/sim0nx/python-openhab)) and share a bit more of your code (e.g. the real value you used) as well as the openhab item definition for me to be able to re-produce it ?

Thanks

---

<div class="post-metadata">

**Author:** ![5iver](https://community.openhab.org/user_avatar/community.openhab.org/5iver/32/21201_2.png) [@5iver](https://community.openhab.org/u/5iver)\
**Post date:** [October 5, 2018, 12:30pm UTC](https://community.openhab.org/t/python-module-for-easily-accessing-the-openhab-rest-api/19449/18 "2018-10-05T12:30:30Z")

</div>

Maybe I’m missing something, but why not use [Jython and the new rule engine](https://www.openhab.org/docs/configuration/jsr223.html) for this? Seems to me like a much easier way to interface with OH than through the REST API. To each his own, but maybe this possibility wasn’t considered?

---

<div class="post-metadata">

**Author:** ![sim0nx](https://community.openhab.org/user_avatar/community.openhab.org/sim0nx/32/71872_2.png) [@sim0nx](https://community.openhab.org/u/sim0nx)\
**Post date:** [October 5, 2018, 1:19pm UTC](https://community.openhab.org/t/python-module-for-easily-accessing-the-openhab-rest-api/19449/19 "2018-10-05T13:19:50Z")

</div>

For my part I use the library for integrating external devices and services, but also for simple scheduling which is in my case easier to do in a python script and a cron job than to run it in a rule. More so that this does not involve reloading or restarting OH.

BTW thnx for the reminder, I wasn’t aware this was released already.

---

<div class="post-metadata">

**Author:** ![5iver](https://community.openhab.org/user_avatar/community.openhab.org/5iver/32/21201_2.png) [@5iver](https://community.openhab.org/u/5iver)\
**Post date:** [October 5, 2018, 1:53pm UTC](https://community.openhab.org/t/python-module-for-easily-accessing-the-openhab-rest-api/19449/20 "2018-10-05T13:53:14Z")

</div>

> [@sim0nx](#):
>
> BTW thnx for the reminder, I wasn’t aware this was released already.

It’s been a couple years, but it’s still called Experimental Rule Engine. Just need more volunteers to use/test it so that it bcomes mainstream. My rules are all migrated to Jython and they’ve been running great. The ESH guys are pretty quick to resolve any issues too. I also have a nice update for the [rule decorators](https://github.com/OH-Jython-Scripters/openhab2-jython/pull/21) that really simplifies rule creation. Just want to get one more thing fixed up before posting in the forum.

It sounds like everything your doing could be done in the rule engine. No need to stop OH to make changes to a script, and there are a lot of scheduling options. With your Python experience, I would think you’d love to get away from the Rules DSL and into Jython.

[Next page](https://community.openhab.org/t/python-module-for-easily-accessing-the-openhab-rest-api/19449.md?page=2)
