[SOLVED] Openhab2 executeCommand fetch JSON string - Tado

Hello

I am trying to get some data from the Tado unofficial API to get the operation, insideTemp and setPointTemp.

I have found I can use the URL:

https://my.tado.com/mobile/1.9/getCurrentState?password=XXXXX&username=XXXXX@XXXXX.XXX

However using sendHttpGetRequest() every other response is unauthorized - so I found I have to use curl:

var response = executeCommandLine("curl@@https://my.tado.com/mobile/1.9/getCurrentState?password=XXXXX&username=XXXXX@XXXXX.XXX@@-H@@Referer: https://my.tado.com/", 5000)

This fetches the string every time without a fault. However the response also contains the execution times so I can’t use the JSONPATH transformation:

var Mode = transform("JSONPATH",  "$.operation", response)
var Temp = transform("JSONPATH",  "$.insideTemp", response)
var SetTemp = transform("JSONPATH",  "$.setPointTemp", response)

Here is a typical response:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   635  100   635    0     0   3602      0 --:--:-- --:--:-- --:--:--  3587

{"success":true,"operation":"HOME","preheating":false,"preheatingTargetOperation":null,"autoOperation":"HOME","operationTrigger":"SYSTEM","insideTemp":20.29,"setPointTemp":20,"controlPhase":"STEADY","currentUserPrivacyEnabled":false,"currentUserGeoStale":null,"deviceUpdating":false,"homeId":XXXXX,"heatingOn":false,"internetGatewayType":"GW02","internetGatewayConnected":true,"heatingMuscleType":"BU01","heatingMuscleConnected":true,"insideTemperatureSensorType":"RU01","insideTemperatureSensorConnected":true,"settingsEnabled":true,"fallbackOperation":false,"helpUrl":"https://support.tado.com/hc/","pendingDeviceInstallation":false}

Maybe you need to URL encode your username and/or password, in case they contain illegal characters?

Also, have you tried to use the HTTP Binding instead of a rule? Your openhab.cfg would include

http:tado.url=https://my.tado.com/mobile/1.9/getCurrentState?password=XXXXX&username=XXXXX@XXXXX.XXX{Referer=https://my.tado.com/}
http:tado.refreshInterval=60000

And your items would contain:

String Mode "Mode [%s]" { http="<[tado:60000:JSONPATH($.operation)]" }
Number Temp "Temp [%.1f °C]" { http="<[tado:60000:JSONPATH($.insideTemp)]" }
Number SetTemp "SetTemp [%.1f °C]" { http="<[tado:60000:JSONPATH($.setPointTemp)]" }
1 Like

I will give it a go the way you have suggested and let you know how it goes!

However at a guess I think I will have the same issue.

The way I mentioned with sendHttpGetRequest worked 4 times out of 10 - so I know the URL wasn’t badly coded. It was the API server at the other end which didn’t like the request - I should have mentioned the unauthorized request was a JSON string which the API returned.

Adding the Referer information into the cURL request seems to keep the API server happy and every request for the last few hours has returned successfully.

I was thinking I could try using REGEX to pull out the JSON from the string, then use that as normal?

Something like:

var JSON = transform("REGEX", '\{(?:[^{}]|(?R))*\}/xg', response)

However I don’t know the correct syntax to get the JSON object from the string or if that is even the correct way to try and do such a thing? It does however throw an error so I know it isn’t correct!

 Illegal repetition near index 0
^{(?:[^{}]|(?R))*}$

I’ve updated my example setup above to add the Referer: header that you suspect makes a difference. I recommend trying this approach with the HTTP binding because I think it’s a little simpler than the workaround approaches. Let us know how it goes!

1 Like

Perfect - thats a much neater way of doing things! So far so good - no errors returned yet!

I decided to use this route as I already use Tado to know when we are home - no point running two geo-location apps to let openHab know we are home. I had used IFTTT to update a switch when Tado changed modes but found it to be unreliable and had some horrendous delays so thought I could cut out the middle man!

1 Like

hello,

reading data from the api with the http-binding works well for right now. now i want to set values with with openhab and curl to the api. but using curl always gives me the mentioned error back.

root@HomeServer:~# curl "https://my.tado.com/api/v2/me" -d username=email -d password=password | jq ''
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   158  100    98  100    60    326    200 --:--:-- --:--:-- --:--:--   327
{
  "errors": [
    {
      "code": "accessDenied",
      "title": "current user is not allowed to access this resource"
    }
  ]
}

can some help me to find the rights curl-commands so that i can use the following guide to post data?

thanks, alex