How to push Item (temperature) value to python script?

Hello guys,

I´m running Openhab 3 and using some python scripts to control my Samsung Air Conditioners.

Switching it on/off e.g. is working as expected but now it comes to one difficult part - regarding the tempearture.

I have a setpoint in my sitemap to define the temperature for the AC unit. My goal is to transfer this value (e.g. “23”) to my python script to set the temperature accordingly.

The python scripts executes rest APi calls and for the temperature there is also an “argument” for the temp value:

import requests

url = "https://api.smartthings.com/v1/devices/e240af1e-061b-537b-1dad12355456asdde/commands"

payload = "[{\"component\":\"main\",\"capability\":\"thermostatCoolingSetpoint\",\"command\":\"setCoolingSetpoint\",\"arguments\": [23.0]}]"
headers = {
  'Authorization': 'Bearer 13432b20-43a31-2410-1853-xxxbadef0qwerty',
  'Content-Type': 'text/plain'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

The rule which enables the Air conditioner contains also a call for the temperature set API - which looks like this:

executeCommandLine("sudo", "python3", "klima_eg_set_temperature.py")

Maybe you can help me to get this working. I guess I need to adapt the python script to “listen” to an input value and also the executeCommandLine to “push” the value to the python script.

Many thanks in advance, Mario

The first question is why are you not using the HTTP binding or sendHttpPostRequest rules actions to eliminate the need for the script?

Yes, if you stick with the Python script you’ll have to update the script to accept and use command line arguments. Then you’ll have to change your call to executeCommandLine to pass the value to the script.

But if you keep it all inside openHAB none of that will really be necessary.

Hello!

Thanks for your reply. I tried to achieve this APi Restcalls with http binding but I was not able to do so and with python scripts it was much easier for me?

I am using postman to setup my rest calls and there is also a convert tool to http available. But I don´t know how to setup the http things with all that informations (while creating new http things via openhab ui)

  • Where to put the URL? Which URL?
  • How does the authentication work? Where to put the password?
  • Where to put the command / state query?

Thats the result from “Postman”:

POST /v1/devices/easdfd31e-0323b-3234-1dad-0e74004774dc1/commands HTTP/1.1
Host: api.smartthings.com
Authorization: Bearer asdfasdf-asdf-asdf-937a-47cb8950b5c4
Content-Type: text/plain
Content-Length: 59

[{"component":"main","capability":"switch","command":"on"}]

What I´m also missing - when the http Thing is setup - how to activate / trigger it?

Thanks!

Well, either way you’re going to have to learn how to do something. Either you have to learn how to write a Python program that accepts and uses command line arguments which you will have to find on some other website, or you will have to learn how to use the tools openHAB provides.

To be successful at this sort of low level stuff you have to have at least a little bit of understanding of how the technology you are using works. In this case a REST API and HTTP calls. It would be worth while looking for some tutorials and basic information on this before continuing. Just copying and pasting stuff from forums and the web without understanding how they work means you’ll be helpless when things don’t work as expected.

Beyond that, you have everything you need to make the HTTP request defined and clear in your Python script. You know the url, the payload, the headers, and the type of request.

Looking at the docs for the HTTP Binding we see places to enter:

  • baseURL: the URL that will be called when the Thing is triggered
  • username and password: the authentication credentials
  • commandMethod: you’ll use “POST” to indicate when the command channel is triggered with a command it will issue the POST to the configured URL
  • encoding: you’ll use “text/plain”
    -stateContent : you’ll use the “payload” contents from your Python script.

So you have all the information you need. You’ll create a new HTTP Thing and fill in all this information. Then you’ll have to create the Items and link them to the proper channels.

I don’t use this binding so read on in the binding docs and the many examples here on the forum for more details.

Or you can use [sendHttpPostRequest](https://www.openhab.org/docs/configuration/actions.html#http-actions) which takes as arguments:

  • url: the URL from your python script
  • contentType: the Content-Type from your python script
  • content: the payload from your python script
  • headers: a Map (search the forum for “createHashMap” for hundreds of examples) containing one entry with “Authorization” as the key and the “Bearer …” as the value
  • timeout: how long to wait for the request to complete before declaring it an error.

You’ve only got the one URL. You put it in the field/argument named “url”.

As shown above, the HTTP binding has a username and password field. The rule action does it exactly the same way as your Python script.

You mean the payload? In the binding it’s called the “stateContent” and in the rule action it’s called the “content”.

I don’t use this binding but I strongly suspect that you have a Switch Item that when you send an ON command to it it will cause the binding to make the POST request. If not that, then you’ll have an Item linked to the stateContent and you’ll sendCommand the “payload” to that Item to trigger the POST request.

As with all Things in openHAB, you cause something to happen by sending a command to an Item linked to the Channel that causing something to happen.

1 Like

Hello! Thank you so much for your detailed reply - that helps a lot! At least I am now aware that my scenario will work with http binding. In the very beginning I was not sure about it so this was also one reason why I decided to go for python script.

But I´m willing to learn about http binding - that shouldn´t be a problem at all.

Cheers,
Mario