Yes, GET URL. Your output seems to be correct, just that the humidity is currently measured with 0.
I assume your AC is turned on and humidity is not 0 though right?
Correct. My AC was on when I ran the GET URL, and humidity was most likely not 0
Maybe it takes a few minutes from the moment the AC powers ON till the humidity sensor takes a reading?
Also make sure to whitelist your Exec commands in the following file: (whitelist is an “old” version here - I ´m working with input arguments while using the python scripts already…)
If the AC unit is on - the “Status update” things gets active (through my rule) and then updates the temperature / operating mode status every 5 seconds.
EDIT: If you want to use python with input arguments (not having a script for setting temperature to 21 degree and another script to set the temperature to 22 degree) you can use the following example:
Next will be creating all the Items and finally the rules!
Last but not least: I know that my solution is (as I already mentioned) not state of the art and maybe the choice to use python (instead of http) is bad but for me it was easier to use as I was not able to use http binding with authorization bearer and so on…
Always open minded for tips / advices to improve my solution.
For setting the temperature I am not using the input argument of the python script anymore because I had problems with my rule and tried it this way. I need to change it back again as the issue was not from this lines of code.
Initially I wanted to use a hysterese to turn the AC units on & off but finally I´m using now the average temperature value of the last 45 minutes.
So variables are also available for hysterese - if you prefer it that way.
var Number setpoint_eg
var Number setpoint_kz
var Number setpoint_sz
var Number hysteresis_eg = 1
var Number hysteresis_sz = 1
var Number hysteresis_kz = 1
var String mode
val headers = newHashMap("Authorization" -> "Bearer oh.restapi.yourTOKENID", "WWW-Authenticate"-> "Basic")
//Klimaautomatik für das Erdgeschoss abhängig von aktueller Raumtemperatur und gesetztem Wert (Setpoint)
rule "Klimaanlage EG automatisch steuern"
when Item Temperatur_Wohnzimmer changed or
Item Klima_EG_Setpoint changed or
Item Klimaautomatik_EG changed or
Item Klimaautomatik changed
then
//Temperatur im EG auslesen
Temperatur_Wohnzimmer_Average.state = Temperatur_Wohnzimmer.averageSince(now.minusMinutes(45))
//Setpoint aus App auslesen
setpoint_eg = (Klima_EG_Setpoint.state as Number)
var temp_wohnzimmer_average = (Temperatur_Wohnzimmer_Average.state as Number)
var setpoint_eg_hyst = setpoint_eg
logInfo("INFO","Wohnzimmer Durchschnitt letzte Stunde: " + temp_wohnzimmer_average)
logInfo("INFO","Wohnzimmer Setpoint: " + setpoint_eg_hyst)
//wenn jemand zuhause ist, nicht geschlafen wird und die Klimaautomatik-Regel aktiviert ist dann schalte die Klimaanlage ein!
if (Home_Mode.state == ON && Sleep_Mode.state == OFF && Klimaautomatik.state == ON && Klimaautomatik_EG.state == ON)
{
if (temp_wohnzimmer_average >= setpoint_eg_hyst)
{
if (Klima_EG_Switch.state == OFF) {
executeCommandLine("sudo", "python3", "/home/exec/klima_eg_power_switch.py")
Thread::sleep(500)
executeCommandLine("sudo", "python3", "/home/exec/klima_eg_cool_mode.py ")
Thread::sleep(500)
executeCommandLine("sudo", "python3", "/home/exec/klima_eg_set_temperature.py ")
Thread::sleep(500)
//Klima EG immer im Windfree Modus betreiben
executeCommandLine("sudo", "python3", "/home/exec/klima_eg_set_operating_mode.py", "windFree")
logInfo("INFO","Klima im EG wird aktiviert da es zu warm ist!")
Klima_EG_Switch.sendCommand(ON)
Thread::sleep(300)
Klima_EG_Temp.postUpdate(Klima_Essbereich_Temp_Status.state.toString.substring(28,32))
}
}
else {
if (Klima_EG_Switch.state == ON) {
executeCommandLine("sudo", "python3", "/home/exec/klima_eg_power_off_switch.py")
logInfo("INFO","Klima im EG wird deaktiviert da Zieltemperatur erreicht!")
Klima_EG_Switch.sendCommand(OFF)
}
}
}
end
//Klimaanlage ein- und ausschalten über Openhab (Swich = Power)
rule "Klimaanlage EG Switch"
when Item Klima_EG_Switch changed
then
if (Klima_EG_Switch.state == ON) {
executeCommandLine("sudo", "python3", "/home/exec/klima_eg_power_switch.py")
Thread::sleep(500)
executeCommandLine("sudo", "python3", "/home/exec/klima_eg_cool_mode.py ")
}
if (Klima_EG_Switch.state == OFF) {
executeCommandLine("sudo", "python3", "/home/exec/klima_eg_power_off_switch.py")
}
end
//Prüfen mit EXEC-Klima_EG_Power_Status_Abfrage in regelmäßigem Intervall ob Klima ggf. per Fernbedienung/App ein-/ausgeschalten wurde
rule "Check Klima EG Fernbedienung Power"
when Item Klima_Essbereich_Switch_Status changed
then
if (Klima_Essbereich_Switch_Status.state.toString.contains("off")) {
Klima_EG_Switch.sendCommand(OFF)
}
else if (Klima_Essbereich_Switch_Status.state.toString.contains("on")) {
Klima_EG_Switch.sendCommand(ON)
}
end
//eingestellter Temperaturwert vom Klimagerät auslesen
rule "Check Klima EG Temperatur"
when Item Klima_Essbereich_Temp_Status changed or
Item Klima_Essbereich_Temp_Akt changed
then
Klima_EG_Temp.postUpdate(Klima_Essbereich_Temp_Status.state.toString.substring(28,32))
end
//Setpoint Temperatur an Klimaanlage EG übergeben
rule "Setpoint Temp an Klimaanlage EG uebergeben"
when Item Klima_EG_Setpoint changed
then
if (Klima_EG_Setpoint.state == 20.0 | °C || Klima_EG_Setpoint.state == 20.5 | °C) {
executeCommandLine("sudo", "python3", "/home/exec/klima_eg_set_temperature.py" ,"20.0")
}
if (Klima_EG_Setpoint.state == 21.0 | °C || Klima_EG_Setpoint.state == 21.5 | °C) {
executeCommandLine("sudo", "python3", "/home/exec/klima_eg_set_temperature.py" ,"21.0")
}
if (Klima_EG_Setpoint.state == 22.0 | °C || Klima_EG_Setpoint.state == 22.5 | °C) {
executeCommandLine("sudo", "python3", "/home/exec/klima_eg_set_temperature.py" ,"22.0")
}
if (Klima_EG_Setpoint.state == 23.0 | °C || Klima_EG_Setpoint.state == 23.5 | °C) {
executeCommandLine("sudo", "python3", "/home/exec/klima_eg_set_temperature.py" ,"23.0")
}
if (Klima_EG_Setpoint.state == 24.0 | °C || Klima_EG_Setpoint.state == 24.5 | °C) {
executeCommandLine("sudo", "python3", "/home/exec/klima_eg_set_temperature.py" ,"24.0")
}
if (Klima_EG_Setpoint.state == 25.0 | °C || Klima_EG_Setpoint.state == 25.5 | °C) {
executeCommandLine("sudo", "python3", "/home/exec/klima_eg_set_temperature.py" ,"25.0")
}
//executeCommandLine("sudo", "python3", "/home/exec/klima_eg_set_temperature.py" ,Klima_EG_Setpoint.state.toString)
logInfo("INFO", "Klima EG Setpoint:" + Klima_EG_Setpoint.state.toString)
end
//eingestellter Kühlmodus vom Klimagerät auslesen
rule "Check Klima EG Mode"
when Item Klima_Essbereich_Op_Mode_Akt changed
then
var mode_eg = Klima_Essbereeich_Op_Mode_Status.state.toString.substring(154)
if (mode_eg.contains("off")) {
Klima_EG_Mode.postUpdate(0)
}
if (mode_eg.contains("windFree")) {
Klima_EG_Mode.postUpdate(1)
}
if (mode_eg.contains("speed")) {
Klima_EG_Mode.postUpdate(2)
}
if (mode_eg.contains("windFreeSleep")) {
Klima_EG_Mode.postUpdate(3)
}
end
//Operating Mode an Klimaanlage von Openhab übergeben
rule "Operating Mode an Klimaanlage EG uebergeben"
when Item Klima_EG_Mode changed
then
if (Klima_EG_Mode.state == 0)
{
executeCommandLine("sudo", "python3", "/home/exec/klima_eg_set_operating_mode.py", "off")
}
else if (Klima_EG_Mode.state == 1)
{
executeCommandLine("sudo", "python3", "/home/exec/klima_eg_set_operating_mode.py", "windFree")
}
else if (Klima_EG_Mode.state == 2)
{
executeCommandLine("sudo", "python3", "/home/exec/klima_eg_set_operating_mode.py", "speed")
}
else if (Klima_EG_Mode.state == 3)
{
executeCommandLine("sudo", "python3", "/home/exec/klima_eg_set_operating_mode.py", "windFreeSleep")
}
end
//Things Auto Status Update (Power, Mode, Temp) deaktivieren/aktivieren um unnötige Systemlast/Log Einträge zu verhindern
rule "Things Klima EG Status Update steuern"
when Item Klima_EG_Switch changed
then
if (Klima_EG_Switch.state == ON) {
sendHttpPutRequest("http://192.168.1.199:8080/rest/things/exec%3Acommand%3Abccd4f91a7/enable", "application/json", 'true', headers, 1000)
sendHttpPutRequest("http://192.168.1.199:8080/rest/things/exec:command:141ebdc02b/enable", "application/json", 'true', headers, 1000)
}
if (Klima_EG_Switch.state == OFF) {
sendHttpPutRequest("http://192.168.1.199:8080/rest/things/exec%3Acommand%3Abccd4f91a7/enable", "application/json", 'false', headers, 1000)
sendHttpPutRequest("http://192.168.1.199:8080/rest/things/exec:command:141ebdc02b/enable", "application/json", 'false', headers, 1000)
}
end
Thank you, Mario, for taking the time to explain everything so thoroughly.
I will try to implement your method next week. I just hope it’s not too advance for my skills
Hi friends,
I’m still struggling to find the proper POST commands to control all my Samsung AC’s capabilities/commands.
For example, I wasn’t able to find the right POST command to enable/disable the display light and all the other commands I mentioned in the quotes.
This is the list of components, capabilities, categories I got when I ran the GET function in Postman.
Could I trouble one of you to tell me how to find my missing POST commands?
Hi @Mario084 , since I’m not a developer and I don’t fully understand your process, I tried to copy your method exactly, while substituting your Device ID’s with my own Samsung AC’s device id’s to create the GET and POST commands, and by substituting your Things identifiers with the ones my OpenHAB system created in the Items file.
Unfortunately, I must have done something wrong, because when I tried to press the new AC’s buttons in my sitemap they did not change any settings on my AC’s.
I must be doing something wrong
Hi @Mario084 and @mjeshurun,
Just to report back on progress as I am investing some time today.
Below an example using the HTTP binding and “GET”'ing the AC status:
HTTP Thing Definition
Replace the black bars with your deviceID and token.
At the same time I am also trying to get this working only with ECMA scripts and the HTTPUtil, whereas I still did not figure out the correct syntax I think:
ECMA test code
//Initialize variables
var telegramAction = actions.get(“telegram”,“telegram:telegramBot:telegram”);
var Log = Java.type(“org.openhab.core.model.script.actions.Log”);
var ZonedDateTime = Java.type(“java.time.ZonedDateTime”);
var ScriptExecution = Java.type(“org.openhab.core.model.script.actions.ScriptExecution”);
var HttpUtil = Java.type(“org.openhab.core.io.net.http.HttpUtil”);
var timeofday = itemRegistry.getItem(“ntp_timeofday”).getState ();
var homestatus = itemRegistry.getItem(“presence_home”).getState ();
var presence = itemRegistry.getItem(“presence_bedroom_master”).getState ();
var scene = itemRegistry.getItem(“scene_bedroom_master”).getState ();
var headers = new testProperty();
headers.put(“Authorization”,“Bearer TOKEN”);
var headers = ;
headers[“Authorization”] = "Bearer " + token;
var url = “https://api.smartthings.com/v1/devices/ID/commands”;
var jsondata = ‘{“component”: “main”, “capability”: “switch”, “command”: “on”}’;
var result = HTTP.sendHttpPostRequest(url, “application/json”, jsondata, headers,15000);
ECMA test code Try 3 - **WORKING**
var token = “TOKEN”
var headers = ;
headers[“Authorization”] = "Bearer " + token;
var url = “https://api.smartthings.com/v1/devices/ID/commands”;
var jsondata = ‘[{“component”: “main”, “capability”: “switch”, “command”: “off”}]’;
var result = HTTP.sendHttpPostRequest(url, “application/json”, jsondata, headers,15000);
If anyone got some pointers there, appreciated.
Hope above helps a bit for standard setup, will try another one with command channel next.
EDIT2: Added ECMA test code 2 which gives me no log errors but also does not send the command
EDIT3: Got it working with simple POST code!! (see ECMA test code Try 3)
EDIT4: Now also implemented 2 rules to turn off/on the AC which works - still trouble getting it into one rule, but that’s more of an OH rule thing that I need to figure out (brain freeze )
EDIT 5: OK, made some progress, I got the following GETs running, in case you want to have the GET URLs, let me know:
Looking at your list of capabilities (which btw is exactly the same that I got) it seems that you cannot control the light via the REST API. Might be wrong here as in the app you got options like “Lighting” and “Sound Effects”, maybe they are not exposed via capabilities?
Thank you for taking the time to look into my question
And thank you for taking the time to describe your HTTP solution.
As I’m a noob in these matters, I would appreciate if you could share the GET/POST URL’s and POST commands you used.
Also, if there is no privacy issue, could you share your Things, Items, Rules and Sitemap settings?
I’m hoping that by having your initial definitions, I could just enter my Samsung AC’s Device ID’s and Token Bearer, thus making your definitions work for my devices
The GET codes I currently implemented, the POST code (on/off) is visible in above rule, I did not implement any more yet:
Stats - On/Off: https://api.smartthings.com/v1/devices/ID/components/main/capabilities/switch/status
Set Temp:
https://api.smartthings.com/v1/devices/ID/components/main/capabilities/thermostatCoolingSetpoint/status
AC Mode: https://api.smartthings.com/v1/devices/ID/components/main/capabilities/custom.airConditionerOptionalMode/status
AirQuality: https://api.smartthings.com/v1/devices/ID/components/main/capabilities/airQualitySensor/status
Humidity:
https://api.smartthings.com/v1/devices/ID/components/main/capabilities/relativeHumidityMeasurement/status
Room Temp:
https://api.smartthings.com/v1/devices/ID/components/main/capabilities/temperatureMeasurement/status
AC Fan Mode:
https://api.smartthings.com/v1/devices/ID/components/main/capabilities/airConditionerFanMode/status
A thing I saw though is that the values in OH did not auto update, because the SmartThings APP did not auto refresh somehow. I had to open the app and refresh so that the AC is shown as turned on. Did you experience something similar @Mario084? Or Is there a setting to turn on auto refresh? (seems it worked over night, maybe just a Wifi drop-out)