OH3 Execute Command within a Rule behaving strangely

Hi All,

Need some help, please. Running a rule that executes a shell script. Shell script returns a JSON string that I wish to assign to different items. Tried running this with ‘executeCommandLine’ and ‘transform’ which I have used before. Both methods work but the returned string has a bunch of other data in it I do not want or recognise. When I execute the shell script from the command line it works perfectly and returns just the JSON data.

The shell script returns:

[{“id”:“01EW1DZSCT20KY4PQWCZ7DXE53”,“nmi”:“6407294703”,“channels”:[{“identifier”:“E1”,“type”:“general”}]}]

The string in the rule (AmberAPI_JSON) returns:

  • % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:–:-- --:–:-- --:–:-- 0 100 106 100 106 0 0 185 0 --:–:-- --:–:-- --:–:-- 185 [{“id”:“01EW1DZSCT20KY4PQWCZ7DXE53”,“nmi”:“6407294703”,“channels”:[{“identifier”:“E1”,“type”:“general”}]}]

The data that I do not recognise is the prepended data in the rule output. Specifically

  • % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:–:-- --:–:-- --:–:-- 0 100 106 100 106 0 0 185 0 --:–:-- --:–:-- --:–:-- 185

No idea what this is.

My rule is as follows:

rule "AmberAPI"
when
    // Every 15 Seconds
    Time cron "*/15 * * * * *"

then
    logInfo("AmberAPI", "Rule started: ")
    var String AmberAPI_JSON = executeCommandLine(Duration.ofSeconds(5), "/usr/bin/sh", "/home/openhabian/AmberAPI.sh")
    logInfo("AmberAPI", "Script returned: {}", AmberAPI_JSON)
   	val String id = transform("JSONPATH", "$.id", AmberAPI_JSON)
   	val String nmi = transform("JSONPATH", "$.nmi", AmberAPI_JSON)
   	val String channels = transform("JSONPATH", "$.channels", AmberAPI_JSON)
    logInfo("AmberAPI", "JSON to Var Output: {}, {}, {}", id, nmi, channels)
    sendCommand(AmberAPI_JSON2, AmberAPI_JSON)
    logInfo("AmberAPI", "Rule ending...")
end

Searched for hours trying to find a solution but so far found nothing that helps. Any help would be greatly appreciated.

Probably console output from the middle of your script. There is no console available here (no shell).

Strip it out with string manipulation - you’ve got a [ marking the start of the JSON.

Thanks Rossko57,

Weird! Not sure what process would be sending this sort of output to the console. I have just checked a few things and nothing jumps out as a root cause.

My shell script is very simple:

#!/bin/bash
curl -X 'GET' \
  'https://api.amber.com.au/v1/sites' \
  -H 'accept: application/json' \
  -H 'Authorization: Bearer psk_72fxxxxxxxxxxxxxxxxxxxxx3674dc'

Not sure if this help at all but I like your idea about stripping out the rubbish until the start of the JSON data. I will give this a try. Not sure how to do this within a rule as typically I would use a shell script. Any pointers would be much appreciated.

Well, it’s only string manipulation.

  var rubbish = = executeCommandLine( ... )
  var interim = rubbish.split("[{")  // should be fairly unique to JSON part
  var AmberAPI_JSON = "[{" + interim.get(1) + "[{" + interim.get(2)  // but there are multiple parts

Undoubtedly more elegant ways to do it

However, I think what you really want to do is use curl with -s option to stop it blabbering about what it is doing.

You know there is an HTTP binding?

that is the curl progress bar. There is an option “-s” I think for silent to turn it off.

Thank you @rossko57, The -s option did the trick. Thank you also for the rule suggestion for string manipulation. Much appreciated.

Thank you @Wolfgang_S, The -s option did the trick. Very much appreciated

1 Like

Hi Ian,

I take it you’ve been working to get a feed from Amber Electric’s API into openHAB - have you had success based on the above? I can’t find much discussion of it here, and the API discussion linked from Amber’s dev page seems to be all about HomeAssistant.

I’d really like to know what you’ve been able to do and how, if you don’t mind sharing!

Thanks,
Robin

Hi Robin,

Happy to help.

I have a curl script doing the API feed and a couple of rules triggering the script and reading the data being returned. Working really well albeit a first pass.

I assume you are local to Melbourne. Happy to discuss this further over the phone. Do you have a number I could call you on?

Cheers

Ian