HABApp: Disable Thing?

Hi all,

If this is possible, I haven’t figured out how to do it or seen it done… looked at the docs, GitHub and here…

Is it possible to programmatically disable a Thing as part of a rule?

I have some things that are occasionally powered off and would like to suppress errors in the logs (without filtering them).

You can use rest api to enable / disabled a thing and issue the rest call within a rule

Thanks @Matze0211 - I did check out the API to make sure it was exposed and possible, and know I could write some code to do it, but just wondered if there was a more graceful way to do in HABApp before doing so…

It’s 2 line of code in a dsl rule…

var headers = newHashMap("Authorization" -> "Bearer <YOUR-API-KEY>", "WWW-Authenticate"-> "Basic")

sendHttpPutRequest("http://<YOUR-OH-INSTANCE>/rest/things/<THING-ID>/enable", "text/plain","false",headers,1000);

replace the false within the request with true to enable the thing again

1 Like

Thanks - Appreciate the example.

I already have a thing monitoring rule using HABApp so it seems like I should be able to change the state against the already referenced Thing. Agreed, not a big deal to do it with ‘requests’ as part of the code, but want to keep as simple as possible, especially as it is not that annoying of an issue.

I’ll take a look at it next week and map it to the thing.
Probably
self.set_enabled(False)
or something

2 Likes

Working with HABApp is inspiring me to write cleaner, more re-usable code, where I am typically just a ‘make it work’ coder (it is not my profession). Maybe some day I’ll be able to contribute some code to such a project…

Disabeling the thing is available.

@alfista2600
You can always contribute to the documentation!
It’s the most important part of a project but it’s often overlooked because it’s cumbersome to create and hard keeping it up to date.

1 Like

Hey @Spaceman_Spiff

This is great, and earlier in the week I decided I’d try to document the feature but you beat me to it.
I’ve never been a technical writer but if there are areas I can flesh out, I’ll do my best.

Meanwhile, I’m happy to offer this sample code for review and possible inclusion as an example. I’m sure you can whittle it down to 7 or 8 lines…lol

I just finished it and can see dozens of improvement opportunities, but it solves a nice little problem for me as is (I think). Also never sure of which code fences to use…

disableable_things.yml:

SwitchProxy_BonsaiLEDs:
  definition: { 'things': ['wled:segment:WLED_Bonsai:dc1d5b413d','wled:json:WLED_Bonsai'],
                'enabled': True,
                'startup_time': 10,
  }
OutletProxy_BarnMain_Alexa:
  definition: { 'things': ['amazonechocontrol:echoshow:account1:G0911asdasd0A9J'],
                'enabled': True,
                'startup_time': 60,
  }

ThingDisabler.py

# Some other stuff
#
# HABApp:
#   reloads on:
#    - params/disableable_things.yml

import HABApp
from HABApp.openhab.items import Thing
from HABApp.core.events import ValueChangeEventFilter
import logging


class ThingDisabler(HABApp.Rule):
    def __init__(self, item, parameters):
        super().__init__()
        self.item = item
        self.parameters = parameters
        self.things = self.parameters['definition']['things']
        self.listen_event(self.item, self.item_toggle, ValueChangeEventFilter())
        self.light_timer = self.run.countdown(parameters['definition']['startup_time'], self.things_online)

    def item_toggle(self, event: ValueChangeEventFilter):
        if event.value == 'ON':
            # give device time to start up before enabling thing
            log.warning('Setting Startup Timer for Things Controlled By ' + self.item)
            self.light_timer.reset()
        else:
            for thing in self.things:
                Thing.get_item(thing).set_enabled(enable=False)
                log.warning('Disabling: ' + thing)

    def things_online(self):
        for thing in self.things:
            Thing.get_item(thing).set_enabled(enable=True)
            log.warning('Enabling: ' + thing)


log = logging.getLogger('MyRules')
cfg = HABApp.DictParameter('disableable_things')  # this will get the file content
for d, p in cfg.items():
    enabled = p['definition'].get('enabled', False)
    if enabled:
        log.warning('Creating listener to enable/disable things based on ' + d)
        ThingDisabler(d, p)

Why would you do this? This would return the json representation of the value as a string.
The check in the next line is always true since you are checking the bool value of the string …
The json part has to removed.

my bad - as I was cleaning it up, that area had a json.loads and json.dumps that worked but was a remnant of me being a bad coder (still am)… I started trying to simplify but forgot to finish/test. Editing original post now…