HABApp - Easy automation with openHAB

Hi @Spaceman_Spiff,

I gave this a quick read. If I understand this correctly, this library works by parsing the items constantly to monitor any changes, then trigger the rules defined?

Since this is REST based access to OH, any of the channel and thing based triggers won’t work, correct?

Impressive work btw.

Thanks,
Joe

Hi @goodfore:
Almost correct - it does not “parse” the items, but listens to the sse events from the rest api. Which means there is no polling, but OpenHAB “notifies” HABApp that an item has changed thus making it way quicker.
On receiving an event it triggers the corresponding rules. It is also possible to listen to different kinds of events and it is even possible to exchange data between rules with the event mechanism of HABApp without openhab knowing about it (this allows passing whole dicts, class instances, etc).

I think the thing events are available, but I have not yet implemented them. What are you listening to currently? The channel events are unfortunately not - you have to bind them to an item.

Thanks a lot :blush:

I lol’ed when I saw this in the code:

timeout=aiohttp.ClientTimeout(total=99999999999999999)

You sure that’s long enough? :smile:

Ok, back to business. I see that you are filtering on item events:

self.__get_openhab_url("rest/events?topics=smarthome/items/"),

I have something like these in my rules:

rule "Mijia & Aqara Wireless Switch"
when
    Channel "mihome:sensor_switch:<GwID>:<ID>:button" triggered
then
    var actionName = receivedEvent.getEvent()
    switch(actionName) {
        case "SHORT_PRESSED": {
            <ACTION>
        }
        case "DOUBLE_PRESSED": {
            <ACTION>
        }
        case "LONG_PRESSED": {
            <ACTION>
        }
        case "LONG_RELEASED": {
            <ACTION>
        }
    }
end

I guess that wouldn’t work? I can try binding that to a string item, to see if I can get the events that way.

Thanks,
Joe

I just hit the keyboard a couple of times so let’s hope it’s enough :wink:

Let’s try the most easy path. Can you link the channels (there should be four - check in paper ui) to a string item?

Sorry, when I said bind, I meant link. Just tried that. I think it might not work.

Here what I got from the event log in openhab.

Item:

String Xm_Button_Event { channel="mihome:sensor_switch:150000000000e4:button" }

Log:

2019-08-25 02:28:09.927 [.ItemChannelLinkAddedEvent] - Link 'Xm_Button_Event-mihome:sensor_switch:150000000000e4:button' has been added.
2019-08-25 02:28:46.500 [vent.ChannelTriggeredEvent] - mihome:sensor_switch:150000000000e4:button triggered SHORT_PRESSED
2019-08-25 02:32:46.747 [vent.ChannelTriggeredEvent] - mihome:sensor_switch:150000000000e4:button triggered SHORT_PRESSED
2019-08-25 02:32:47.659 [vent.ItemStateChangedEvent] - Dsc_Zone10_General_Status changed from CLOSED to OPEN
2019-08-25 02:32:49.890 [vent.ItemStateChangedEvent] - Xm_Motion3_Status changed from OFF to ON
2019-08-25 02:32:49.899 [vent.ItemStateChangedEvent] - Stair_Motion_Changed changed from 2019-08-25T02:28:49.630-0700 to 2019-08-25T02:32:49.894-0700
2019-08-25 02:32:49.900 [vent.ItemStateChangedEvent] - Motion_Changed changed from 2019-08-25T02:32:39.153-0700 to 2019-08-25T02:32:49.895-0700
2019-08-25 02:32:52.434 [vent.ItemStateChangedEvent] - Dsc_Zone10_General_Status changed from OPEN to CLOSED

Also tried switch:

Switch Xm_Button_Event { channel="mihome:sensor_switch:150000000000e4:button" }

Log:

2019-08-25 02:36:34.614 [ome.event.ItemUpdatedEvent] - Item 'Xm_Button_Event' has been updated.
2019-08-25 02:37:18.594 [vent.ChannelTriggeredEvent] - mihome:sensor_switch:158d00020161e4:button triggered SHORT_PRESSED

If it doesn’t trigger an item event, which would mean HABapp won’t pick it up?

Thanks,
Joe

Yea - that is the status quo. I am not sure how to subscribe to channel events, Maybe I’ll try removing the topic filter for the rest api in a couple of days and see how it turns out.

Unfortunately the only solution I can propose is to post an update to an item from your rule.
This way it’ll work but it is not a nice solution.

I tried using a channel filer on the events, it does seem to work:

{"topic":"smarthome/channels/mihome:sensor_switch:158d0000000000:button/triggered","payload":"{\"event\":\"SHORT_PRESSED\",\"channel\":\"mihome:sensor_switch:158d0001644783:button\"}","type":"ChannelTriggeredEvent"}
{"topic":"smarthome/channels/mihome:sensor_switch:158d0000000000:button/triggered","payload":"{\"event\":\"SHORT_PRESSED\",\"channel\":\"mihome:sensor_switch:158d0001644783:button\"}","type":"ChannelTriggeredEvent"}

The bad news is that there doesn’t seem to be a way to filter only items and channel with 1 stream.

However, it seems like 90% of the streams are items? Maybe remove the filter and get events on all? Just a thought.

I guess for now I can post update on item for channel trigger in rule.

Thanks,
Joe

Nice work. How did you filter? I’ll definitely try to add it to on of the next releases!

I found how to filter multiple topics!

rest/events?topics=smarthome/items/*,smarthome/channels/*

I think the * might be optional.

{"topic":"smarthome/items/FamilyRoomHallway_Light/state","payload":"{\"type\":\"OnOff\",\"value\":\"OFF\"}","type":"ItemStateEvent"}
{"topic":"smarthome/items/FamilyRoom_Light/state","payload":"{\"type\":\"OnOff\",\"value\":\"OFF\"}","type":"ItemStateEvent"}
{"topic":"smarthome/channels/mihome:sensor_switch:158d0000000000:button/triggered","payload":"{\"event\":\"SHORT_PRESSED\",\"channel\":\"mihome:sensor_switch:158d0001644783:button\"}","type":"ChannelTriggeredEvent"}
{"topic":"smarthome/items/StudyRoom_Light/command","payload":"{\"type\":\"OnOff\",\"value\":\"OFF\"}","type":"ItemCommandEvent"}
{"topic":"smarthome/items/StudyRoom_Light/state","payload":"{\"type\":\"OnOff\",\"value\":\"OFF\"}","type":"ItemStateEvent"}

It’s actually documented here :slight_smile: :

https://www.eclipse.org/smarthome/documentation/features/events.html#receive-events
https://www.eclipse.org/smarthome/documentation/features/rest.html

1 Like

Version 0.7.1

  • Datetimes from openhab are now timezone naive
  • Bugfix for creating parameter files
  • Support for ChannelTriggeredEvent
2 Likes

Quick question: if I don’t supply a host on the MQTT config, habapp won’t spin up a thread to connect to it, correct?

Thanks,
Joe

nvm, saw the log

[   HABApp.mqtt.connection]     INFO | MQTT disabled

Thanks,
Joe

Correct. If you use mqtt I found it easier to interact with it through HABApp.
If you don’t use it, it will be disabled. Same goes for openhab.

Need guidance on the channel events.

I have setup a rule class as follow.

#imports
class MyButtonRule(HABApp.Rule):
        def __init__(self):
                super().__init__()
                self.listen_event("mihome:sensor_switch:158d0000000000:button", self.button_pressed, ValueUpdateEvent)

        def button_pressed(self, event):
                log.warning(f"{event}")

MyButtonRule()

I am sure HABapp is getting the events, log from earlier:

[2019-08-26 19:24:03,160] [          HABApp.EventBus]     INFO | <ChannelTriggeredEvent name: mihome:sensor_switch:158d0000000000:button, event: SHORT_PRESSED>

However, in the habapp log, I am getting this:

[2019-08-27 00:43:14,069] [              HABApp.Rule]  WARNING | Item "mihome:sensor_switch:158d0000000000:button" does not exist (yet)! self.listen_event in "MyButtonRule" may not work as intended.

Did I do something incorrectly?

Thanks,
Joe

nvm, found it. Stupid mistake. The logger wasn’t right…

It’s late, I should go to bed.

Sorry for spamming the thread.

Thanks,
Joe

Hi @Spaceman_Spiff,

Quick question. How do we handle group item, what is also a switch? get_item(“item”).is_on() doesn’t seem to work.

Thanks,
Joe

What are you trying to achieve or what is your problem respectively?
is_on() returns true if the switch is on, which in the group case would be if the group state is ON

I have an item as defined:

Group:Switch:OR(ON, OFF) StudyRoom_Light "Study Room Light" <light> (gAutoOffLight)

When I do

get_item(“StudyRoom_Light”).is_on()

python returns error indicating there no such method for the object.

Thanks,
Joe

I have a feeling it’s due to the differences in Get Item on REST:

{
   "members":[

   ],
   "groupType":"Switch",
   "function":{
      "name":"OR",
      "params":[
         "ON",
         "OFF"
      ]
   },
   "link":"http://openhab/rest/items/StudyRoom_Light",
   "state":"OFF",
   "editable":false,
   "type":"Group",
   "name":"StudyRoom_Light",
   "label":"Study Room Light",
   "category":"light",
   "tags":[

   ],
   "groupNames":[
      "gAutoOffLight"
   ]
}

As opposed to normal switches:

{
   "link":"http://openhab/rest/items/NightStand_Lamp",
   "state":"OFF",
   "editable":false,
   "type":"Switch",
   "name":"NightStand_Lamp",
   "label":"Night Stand Lamp",
   "category":"light",
   "tags":[

   ],
   "groupNames":[
      "gExec",
      "gLight",
      "gLightSecondFloor",
      "gAutoOffLight"
   ]
}

Group state aggregation is currently not implemented yet. Could you please create an issue with the information from this thread on github?
Meanwhile do

if get_item(“StudyRoom_Light”) == 'ON':
    do sth