Check if thing is Enabled in JS

Sorry, having trouble finding this in documentation.

I have this code that works

var tvUID = "xys";
var thingMgr = osgi.getService('org.openhab.core.thing.ThingManager');
var ThingUID = Java.type('org.openhab.core.thing.ThingUID');
var tvUIDObj = new ThingUID(tvUID);

thingMgr.setEnabled(tvUIDObj, true);

I just want to modify that last line, so that if the thing is disabled, attempt to enable it.

What’s the best way to go about it?

Cheers

Just use thingMgr.isEnabled to get the information (it’s boolean).
See

Thanks! @Udo_Hartmann

That wasn’t working for me, I will try it again in the morning.

Though I realised that’s not exactly what I want to do, I want to check if the status is “offline” and if so disable it, wait, then enable it. Those docs do explain enough I think.

Where can I actually view the console.log output though, it doesn’t show up in logs or anywhere visible in the script editor that I can see?

How do you look into the log file? Built-in log viewer? Frontail?
In case of frontail double check if the service is up and runing.

There is a problem you need to be aware of: The rule which triggers to disable/enable an offline thing most probably calls immediately the rule again.
Maybe this is could also be the reason why your console.log never gets executed.

Based on the question you have I could assume that you want to take action whenever ANY thing goes to offline status?

In this case there is a more elegant way, making use of ThingStatusInfoChangedEvent :

triggers:
  - id: "1"
    label: When ThingStatusInfoChangedEvent is raised
    configuration:
      topic: openhab/things/**
      types: ThingStatusInfoChangedEvent
      source: ""
      payload: ""
    type: core.GenericEventTrigger

You get the UID, old status and new status of the thing by

console.log(event.thingUID);
console.log(oldStatusInfo);
console.log(statusInfo);

Whenever ANY thing goes offline you can disable/enable it like this:

if (event.oldStatusInfo == "online") {
  things.getThing(event.thingUID).setEnabled(false);
}

Thanks. I was using the log viewer in the UI as I was coding from my iPad as it’s late and didn’t want to get out of bed to do actual programming. My problem was actually that the “Save” button on the script editor wasn’t working on touch input so my changes weren’t being saved. Fixed with a refresh.

I have solved the problem, so I will explain it now :stuck_out_tongue:

I have a TV that randomly goes offline from OpenHAB, even when it’s turned on, beats me why. Which is annoying because I have a lot of automations for that TV. However the TV is meant to go offline when I turn it off.

I wanted to set up a rule to run every minute to check if the TV is offline, and if so, disable it and re-enable it. If the TV is legitimately offline (turned off), then it will have no effect, but if it is erroneously offline, it will reload the binding.

The code I used to achieve it ended up being:

var logger = Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.automation.rules");
var thingMgr = osgi.getService('org.openhab.core.thing.ThingManager');
var tvUID = "samsungtv:tv:8c1abdf7-a43b-8617-cd30a1fae65a";
var ThingUID = Java.type('org.openhab.core.thing.ThingUID');
var tvUIDObj = new ThingUID(tvUID);

var thingStatusInfo = actions.Things.getThingStatusInfo(tvUID);
var tvStatus = thingStatusInfo.getStatus();

if(tvStatus == "OFFLINE"){
   logger.info("TV is offline, disabling.");
   thingMgr.setEnabled(tvUIDObj, false);

   setTimeout(function() {
        logger.info("Attempting to restart connection to TV");
        thingMgr.setEnabled(tvUIDObj, true);
    }, 1500);

}

Thanks for the advice!

There is an easier way.
If you create a new rule under Settings → Rules click on “Show All” when adding a trigger.
Then select "a thing status changes"
select your TV thing and select status “offline”

Now your rule will be triggered whenever your TV thing will become offline.
If you log into regular openhab logfile you do not need all the other stuff in your rule.

In the end your rule will become a 2-liner:

things.getThing(event.thingUID).setEnabled(false);
things.getThing(event.thingUID).setEnabled(true);

EDIT:
added code

Thanks, that was my original approach when I glanced at the problem a few days ago, and on further thought, I think that will very much work.

Again it’s very late here, but my line of thought was

  • the thing goes offline because I turned the TV off (99% of the time),
  • running code to disable and re-enable it then doesn’t do anything
  • it’s possible the binding is breaking while it’s offline, though I think that’s very unlikely.
  • But if it is the case, then I can’t detect that in a change of status, so cron made more sense to me.

Thanks for the second set of eyes - I will update that in the morning!

If you want to overcome potential binding problems (thing goes offline and TV is on) then change the trigger of your script to:

If TV-thing is offline AND TV-status is ON (I am sure there is an item which returns if your TV is on or off). Maybe this helps?