I am now a couple months into using and setting up openhad and I love it and the possabilities it leaves open! Ive used the Rest API through myopenhab and tasker already to fix inconsistencies with my partners phone and presence and instantly got thinking about other uses.
The project I want to jump on now is using eventghost to tell openhab when netflix/mpc-bc is open in the form of flipping a Now_Playing switch item. This would be used in conjunction with a Theater_Lighting switch item in my sitemap and a rule to decide if the Livingroom_Light should be switched OFF. I’ll also flip the Now_Playing switch anytime the space bar is pressed while these programs are open!
When I google eventghost and openhab I got a lot of posts about Mqtt but i dont know anything about how that works plus most post seem to refer to using it to tell eventghost when openhab does something rather then the other way around. If Mqtt really is the best option I don’t mind learning how it works I’d just need some major pointers on how to set it up for openhab and eventghost to communicate back and forth doing simple things like checking the state of a switch item from eventghost and how to change the state of a switch through eventghost!
I found the Rest API really simple to use through tasker and I think its the best option for this application but when I search how to do a http post with eventghost I found nothing about openhab and the one I attempted to alter didnt work for me.
If anyone more firmiliar with eventghost or python could help clarify how I can use a one line python command to switch an item exposed to myopenhab that’d be amazing and much apreciated! If you also know how the Rest API can be used to check the state of an item thatd be a bonus that I’ll use for an Ambiance rule. That part would be used so eventghost could check if Openhabs Ambiance_Lighting switch was ON and if it was it would launch an ambiance program I found any time netflix or mpc-bc is open and then disable the same program any time sapecbar is pressed!
I think at least my first request is super simple and its just my lack of actually understanding how python and the Rest API work thats making things hard so any help or pointers is much appreciated thanks for your time and help!
I know nothing about eventghost but if it can receive MQTT messages it stands to reason that it can publish them as well.
That isn’t a correct openHAB 2 REST call. I recommend installing the REST API Docs (under the Misc tab in PaperUI) and experiment there with the calls. For one it needs to be a POST (sendCommand) or a PUT (postUpdate), not a GET. Secondly, the URL would be something like: “https://10.10.1.52:8080/rest/items/MyItemName” with a body of “ON” to send the command ON to MyItemName.
The REST API is very powerful but a lot of people end up setting up and using MQTT because it has become a lingua franca for home automation systems to talk to each other. If you aren’t using it yet, you probably will eventually.
I apologize I was at work so I didn’t bother changing the http address inside the brackets! Actually that’s my only hurtle when it comes to the REST API and eventghost. Eventghost allows me to easily run any python command or script when say a program is launched or virtually any other system event happens. It has plugins which add listeners for different events be it key presses, system events, MQTT messages and pretty much anything else you can think of!
So for clarification I attempted to use this python command though eventghost and would love to know what is wrong and how I should change it to be a proper POST command and what changes from the POST command need to be made to make it a GET! I knew adding ON to the end was the wrong way but I don’t know how to declare the body of this http post in python.
I’m also going to take the time to look over the MQTT tutorial as I really appreciate that and want to understand it sooner rather then later even if I don’t use it here! If I can get this up and running using the REST API, I will and if as I learn how MQTT works I find that better I’ll switch over!
I recommend looking for some python tutorials on how to make http calls. The percentage of people on the forum who are proficient in python is well below 100%. It has been well over a year since I’ve done anything with it and I didn’t do much with http in the first place.
But I do know you will probably have to use a different library. And you will have to create an object that you populated with the proper headers and body and then make the proper call with the URL. If I remember correctly, there is a separate method for GET, POST, and PUT.
Okay, so this topic is ancient, but it was the top result when I looked for an answer to this question today, and the topic didn’t actually have an answer – so here is the answer for the next person to need it.
from urllib2 import Request, urlopen
req = Request('http://openhab.lan:80/rest/items/mezzaninependants_Switch');
data = str("OFF").encode('ascii');
Working perfectly on 2022-06-08 with Eventghost 0.5.0-rc6 and OpenHAB 3.2.0.
We do that on this forum but it’s because 99% of the threads that get resurected in this way is someone saying “I’ve got a problem that kinda looks like one mentioned somewhere in this tread around post 127. I know this is a ten year old post but can someone help?” The noise isn’t worth the one solution in a thousand that gets posted.
Unfortunately there was no way to apply the policy retroactively so really old posts remain open.