OH3 - Help with rules rest api broker for Sony TV integration

  • Platform information:
    • Hardware: Dell R710
    • OS: Vmware->Ubuntu->Rancher->Docker
    • openHAB version:OH3 M5

My old setup needed to be retired, so I thought I would give OH3 M5 a crack.
Its pretty awesome, alot of clicking and typing.

But on topic.

OH3 doesnt seem to have a Sony TV binding… I was using a OH1 binding in OH2. But this trick didn’t work on OH3. :frowning:

So I built a TV API broker with python and flask that you can either getStatus or setStatus. I got getStatus returning json and used new ECMA scripting to make the request and parse json response and update relevant items.

But this where I got stuck with ECMA, I want one script to be called for all TV changes.
My API supports set (power, volume) and value (on/off, 0 - 100)

Ideally I want one script that will fire on any TV item change and build a restful request.

Can I do this with ECMA scripting? I presume so - but im new to this fancy pant javascripting thing.

I have currently linked this script to one rule linked to one item (ideally I dont want to have a script for each action for each tv :frowning:

var client = java.net.http.HttpClient.newBuilder().build();
function httpGetStatus(uri, pTV, pPIN, pSet, pValue) {
try {
var request = java.net.http.HttpRequest.newBuilder()
.GET()
.uri(java.net.URI.create(uri))
.header(“TV”, pTV)
.header(“PIN”, pPIN)
.header(“SET”, pSet)
.header(“VALUE”, pValue)
.build();
var response = client.send(request, java.net.http.HttpResponse.BodyHandlers.ofString());
// print(response.body());
return response;
} catch (e) {
print(e);
}
}

var currentState = Bedroom5_TV_power.state

var result = JSON.parse(httpGetStatus( “http://192.168.1.21:7000/setStatus
, ‘192.168.1.36’, ‘1234’, ‘power’, currentState.toString
).body());

Then of course I may have gone down the complete wrong path!

Another option is getting python to integrate with MQTT… but really thought it might be interesting to do it via rest api.

True. The solution is to use that binding on an OH2 installation and connect it to OH3 using the remoteopenhab binding.

The new REST API has authentication for many items. You either need to use an API Token or enable Basic Authentication and use that.

thats one way of doing it, didnt think about that as an option.

I think considering no one has updated the Sony TV binding in OH2 from OH1 then this option will not be a great long term solution. dont want to be running OH2 for years just for TV integration.

Any ideas on how to get the new rules engine to do some of the lifting? I feel like im pretty close.
I need to have a trigger off multiple items or items in a group TV_power or TV_item name… and map the item names to IPs (that can be hard coded).
Then call the python api to trigger the action.

But struggling to use the rules engine to do this.

Since you are familiar with Python take a look at HABapp it interfaces with OH through the REST API bu uses Python 3 classes for rules. @Spaceman_Spiff can provide more details.

Yes - take a look at HABApp. You should be easly able to integrate your python program there and make it interact with openhab.

1 Like

One option is you can run a separate OH 2.5 instance to run this binding. Then use the Remote openHAB binding in OH 3 which will let you mirror the OH 2.5 Items in your OH 3 instance.

Sure. How do you represent all this in your Items? That will drive the approach for the rule.

However, I might offer a different approach. You can make your code that builds the request as a library function(s) (see OH 3 Examples: Writing and using JavaScript Libraries in MainUI created Rules). Then you can make multiple rules that call this one function with the right arguments. That might be easier to write and understand and still avoid duplicating lots of code.

Another approach might be to set this all up in the HTTP binding and use transformations. Then you might not even need Rules at all. From what you’ve described I’m not convinced you even need Rules for this. At most you might need a JavaScript transformation. Then you can model your TV using separate Items (which is more OH appropriate).

The Member of trigger is still supported by all openHAB rules langauges.

See Design Pattern: Encoding and Accessing Values in Rules

I have a rule based on group change.

I want to get the new state of the item that triggered the group change.
I presume this is possible?

var currentState
if (this.State == ON) {
  currentState = 'on'
} else {
  currentState = 'off'
}

or

var currentState = this.State

I would like to return a string of “on” or “off” based on the newly set state

How do I do this - I havent really found much examples… for ECMA script.

Assuming a Member of trigger, the variable triggeringItem in rules DSL is the Item that caused the rule to trigger. In the other languages, event.itemName is the name of the Item that triggered they rule.

Hi @Jade_Baxter did you get working, would you care to share. I had the sony binding a while back, but removed it while trying to isolate a performance issue with my instance. Now I’m on OH3, I’m keen to control the TV again.

Yes I did get it working.

I created a docker python api. That you send config to from oh3.
Python code will then call sony tvs and return states in json.
Oh3 would then update item states.

Same process for sending commands

Worked fine.
I also couldn’t get garage door working either with oh3.

I have moved to home assistant to trial that.

But if your interest in my code then I can send publish it on GitHub.

Jade

Hi @Jade_Baxter, would be good if you can share, I’ll give it a go thanks.