Unexpected result - OH3 Rule

I’ve been running OH2 for the past few years and have refined my rules over this time. Now seems to be a good time to migrate onto OH3 and use some of the new bindings and functionally on offer. My plan is to migrate in small steps and with the least amount of impact.

As I take my first steps I’ve come across a problem that I need help to resolve.

I live in the UK and use the Satellite TV provider Sky, they don’t offer any form of API to work with their set-top box. Named Sky Q. However thanks to sky-remote-cli - npm and GitHub - dalhundal/sky-q: A Node module for interacting with Sky Q boxes it’s possible to interact with the Sky Q box to a degree.

I the idea of the script below is to check the status of the Sky Q box when the TV is switched on and to update a virtual/proxy Switch. However I get this error:

Script execution of rule with UID 'Living_Room-1' failed: An error occurred during the script e
xecution: array element type mismatch in Living_Room

The output of vSkyQ_Status is: null. Where, as you can see from the script below it should either be ON or OFF. The rule works fine using OH2, so clearly something needs to be adjusted to make it now work in the same way with OH3 and therefore I’m looking for some assistance.

rule "Check SkyQ Status" 
when
    Item Livingroom_TV_vSwitch changed
then
    logInfo(logName, "System Message: Updating SkyQ Status.")
    var vSkyQ_Status executeCommandLine("/etc/openhab/scripts/SkyQ.sh")
    Livingroom_SkyQ_Switch.sendCommand(vSkyQ_Status)
end

Below is the contains of SkyQ.sh

const SkyQ = require('/etc/openhab/scripts/node_modules/sky-q');

const box = new SkyQ({ip:'192.168.10.59'})
// Where 10.10.10.10 is the address of the SkyQ box you want to interact with

box.getPowerState().then(isOn=>{
  if (isOn) {
    console.log("ON")
//    $('.btn').click(function()) {
//    $.post(Url,"ON");
  } else {
    console.log("OFF")
  }
//    $('.btn').click(function()) {
//    $.post(Url,"OFF");
})

I hate to say it but this rule as posted could never have ever worked in any version of OH.

  1. You are missing the equals sign between the variable declaration and the call to executeCommandLine. I’m pretty sure that’s either a syntax error or it’s treating the var vSkyQ_Status and the call to executeCommandLine as two separate lines of code.

  2. If you do not provide a timeout as an argument to executeCommandLine, as documented, then executeCommandLine runs the script in the background and never returns the result of the command. It just immediately returns void.

So if it works fine on OH 2, it is not identical to what is posted here.

As shown in the docs, if you want what ever output was generated by the command, you have to supply a timeout argument. This changed slightly between OH 2 and OH 3. In OH 2 the last argument would be the maximum number of milliseconds to wait for the command to complete. In OH 3 the first argument needs to be a java.time.Duration representing the maximum amount of time to wait for the command to complete. Then you need to actually assign what executeCommandLine returns to the variable using =.

Thanks @rlkoshak ,

After reading the documentation once again I’ve now updated my rule with the following, which works. The missing = sign was me playing around with the rule prior to posting. so another example of making sure I validate my posts before hitting the submit button.

var vSkyQ_Status = executeCommandLine(Duration.ofSeconds(1), "/etc/openhab/scripts/SkyQ.sh");