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.
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
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
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);
}
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.