Event information deconz with Xiaomi Cube

Hello,
I use hass.io, a deconz USB stick and Xiaomi Cube. When i listen to the websocket from deconz rest api I get:

{"e":"changed","id":"15","r":"sensors","state":{"buttonevent":-12847,"lastupdated":"2019-01-12T13:01:49"},"t":"event","uniqueid":"00:15:8d:00:01:04:13:51-03-000c"}

But when I listen to event in AppDaemon I just get:

2019-01-12 13:01:49.490106 INFO xiaomi: {'id': 'mi_magic_cube', 'event': -12847}

The to be able to differentiate from rotate and switch sides the id (it changes between 14 and 15) from the websocket is important. How can I access that information?

Thanks!

how do you use the listen_event?

self.listen_event(self.cube_rotate)

Even if I force a warning, I cannot see the ID.

2019-01-12 22:13:07.466111 WARNING AppDaemon: Unexpected error in worker for App xiaomi: 2019-01-12 22:13:07.467371 WARNING AppDaemon: Worker Ags: {'name': 'xiaomi', 'id': UUID('a075c99f-e48a-49db-83cf-4b721e51cabe'), 'type': 'event', 'event': 'deconz_event', 'function': <bound method xiaomiCube.cube_rotate of <xiaomi.xiaomiCube object at 0x74573fb0>>, 'data': {'id': 'mi_magic_cube', 'event': 3070}, 'kwargs': {}}

i think you listen to the wrong event.
what you show from the restapi I think is a state change event.
and for state changes you need to use listen_state

what entities do you have that are related to that xiaomi cube?

[sensor.mi_magic_cube_battery_level] 100 event_id: mi_magic_cube unit_of_measurement: % friendly_name: Mi Magic Cube Battery Level device_class: battery

I just have the battery information.
I use listen_event() to catch the button presses from another Xiaomi button device.
Listening to states does not catch any changes (rotations, turns) but event does.

if there is only an event you need to listen to the right eventname.

{"e":"changed","id":"15","r":"sensors","state":{"buttonevent":-12847,"lastupdated":"2019-01-12T13:01:49"},"t":"event","uniqueid":"00:15:8d:00:01:04:13:51-03-000c"}```

this shows that you cant listen to magic cube.

i think the event you should listen to is called “changed” with id 15

so you need

  self.listen_event(self.callback, "changed"):
def callback(self, event_name, data, kwargs):
  self.log(data)

but i wonder if you are missing things, because all devices have entities
what xioami components did you add to HA configuration to get the cube in HA?

Thank you for helping out ReneTode.
I tried listen event with changed. But I did not get any hits.

This is from the Hass.IO log:

2019-01-13 12:49:45 DEBUG (MainThread) [pydeconz.deconzdevice] Mi Magic Cube: update buttonevent with -9135
2019-01-13 12:49:45 DEBUG (MainThread) [pydeconz.deconzdevice] Mi Magic Cube: update lastupdated with 2019-01-13T12:49:45
2019-01-13 12:49:45 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event deconz_event[R]: id=mi_magic_cube, event=-9135>
2019-01-13 12:49:45 DEBUG (MainThread) [pydeconz.websocket] Websocket data: {"e":"changed","id":"15","r":"sensors","state":{"buttonevent":-9135,"lastupdated":"2019-01-13T12:49:45"},"t":"event","uniqueid":"00:15:8d:00:01:04:13:51-03-000c"}
2019-01-13 12:49:45 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1862094320] Sending {'id': 12, 'type': 'event', 'event': {'event_type': 'deconz_event', 'data': {'id': 'mi_magic_cube', 'event': -9135}, 'origin': 'REMOTE', 'time_fired': datetime.datetime(2019, 1, 13, 12, 49, 45, 561815, tzinfo=<UTC>), 'context': {'id': '718de964e28d418eb6ccfa2beb89528d', 'user_id': None}}}

this makes me think that you have an mi magic cube entity.
or else there is not a lastupdated to change.

so you need to go to the dev tools and seach through your entities and look for that entity.
and my gut tells me that that entity has an attribute called buttonevent.
if thats true, you could use something like self.listen_state(self.cb,“component_type.mi_magic_cube”)
you say listening to states does not catch any changes, so you tried? what did you try?

still my question stand: what did you add to your home assistant configuration? is it a custom component? or what component are we talking about?

  def initialize(self):
    self.log(self.listen_state(self.cube_rotate, "changed")) 
  def cube_rotate(self, event_name, data, kwargs):
    self.log(data)
    self.log(event_name)
    self.log(kwargs)

This yielded:

2019-01-13 13:56:04.103949 INFO xiaomi: {'id': 'mi_magic_cube', 'event': -5539}
2019-01-13 13:56:04.107696 INFO xiaomi: deconz_event
2019-01-13 13:56:04.108289 INFO xiaomi: {}

I have searched the whole entities dev tool page and I only get the battery info. No buttonevent.
Entity:

[sensor.mi_magic_cube_battery_level] (xxxxx.xx.xx/dev-state#) 
95
event_id: mi_magic_cube
unit_of_measurement: % 
friendly_name: Mi Magic Cube Battery Level
device_class: battery

No custom components.
Deconz: https://github.com/marthoc/hassio-addons

And I am not using the Xiaomi component because I do not have the xiaomi/aqara gateway. I am using the usb-zigbee/phoscon to connect with the cube.

so yeah a custom component. (its a custom addon, which is created by a third party and what uses autodiscovery from home assistant)

i didnt mean to search for button event, but for anything what could be related to the magic cube.
does the usb-zigbee/phoscon create any entities?
maybe something called switch.15 or are there any other entities that you have no idea about where they come from?

do you know anything about how to write apps? because the code you provide shows that you dont know how to work with appdaemon. (and thats no shame, we all need to start learning at some point, when we start with something new)

this code:

  def initialize(self):
    self.log(self.listen_state(self.cube_rotate, "changed")) 
  def cube_rotate(self, event_name, data, kwargs):
    self.log(data)
    self.log(event_name)
    self.log(kwargs)

can never have given you this result.

2019-01-13 13:56:04.103949 INFO xiaomi: {'id': 'mi_magic_cube', 'event': -5539}
2019-01-13 13:56:04.107696 INFO xiaomi: deconz_event
2019-01-13 13:56:04.108289 INFO xiaomi: {}

because first of all you cant use
self.log(self.listen_state(self.cube_rotate, “changed”))
it will start a state listener if you have a decent callback, and log a handler (a number)
but it wont log a number at all because your callback is for an event listener, so it only generates an error in your errorlog.
above all it cant give any return because listen_state listens to entities and “changed” isnt an entity
so only that line alone has 4 different points where you go wrong.

the info you have in the log must be from something else you have done.
i think thats from self.listen_event(self.cube_rotate, “changed”)

i suggest you first try to learn how to create apps, and try to understand what listen_state and listen_event (and all other functions) do, before you try to something like this.
this could be helpfull to understand what you are doing:
my appdaemon for beginner:


how to write apps from the docs
https://appdaemon.readthedocs.io/en/latest/APPGUIDE.html
the list of available functions from AD
https://appdaemon.readthedocs.io/en/latest/AD_API_REFERENCE.html
the specific functions for home assistant
https://appdaemon.readthedocs.io/en/latest/HASS_API_REFERENCE.html

besides those info, it is always important to look at normal log and errorlog.

at the moment you know what you are doing, you could try to listen to the event with the name “deconz_event”

if there is anything that you dont understand from my tutorial or the docs i will gladly explain it some more for you.

Thanks for your response.
You are right, it was not the code that produced the response. But the listen_state didn’t catch anything. (I see now that I got lost in the states and changes and events…)
self.log(self.listen_state(self.cube_rotate, "changed"))

This didn’t catch anything. It just returned the handle. (Didn’t throw an error because it didn’t call cube_rotate)

But I found some more info that is just an event in https://www.home-assistant.io/components/deconz/

Remote controls (ZHASwitch category) will not be exposed as regular entities, but as events named deconz_event with a payload of id and event . Id will be the device name from deCONZ and Event will be the momentary state of the switch. However, a sensor entity will be created that shows the battery level of the switch as reported by deCONZ, named sensor.device_name_battery_level.

I guess I have to look into the component and change how it is handling the websocket info.

Thanks for the info!

your welcome, and indeed i already noticed that you need to listen to deconz_event
take it step by step. dont run into it. the more time you take in the beginning, the less you later on need to use to correct things :wink:

I also have a deconz setup. Try something like below:

def initialize(self) -> None:
    self.listen_event(self.button_pressed,
                      "deconz_event",
                      id='mi_magic_cube')

def button_pressed(self, event_name: str, data: dict,
                   kwargs: dict) -> None:
    button_event = data['event']

    if (button_event == 1002):
        do something

Hi, do you know which events are registered in HASS with various activity of the Cube (shake, fall, rotate and so on)?

I don’t find any reference

There is a section about this on the following site:

There is something like ‘shake_air’, ‘free_fall’ etc. in this section, is this what you are looking for?

You could also write a small appdaemon app that listens to all events and writes them to the log file. This way you can do things with the Cube and you will see the assosciated event name in the logs.

yes that works if you use a xiaomi gateway, since I am using a Conbee gateway I do need to know which event_id are sent from Conbee to HASS

Ah I understand. Did you try my suggestion for listening to all events? Try something like this below

def initialize(self) -> None:
    self.listen_event(self.action,
                      "deconz_event")

def action(self, event_name: str, data: dict, kwargs: dict) -> None:
    self.log(event_name)
    self.log(data)

This should put all the data for all deconz events in the log file.

I never did an appdaemon app (I do have hadashboard though).
I just create an app with that code under appdaemon/app and restart appdaemon?

Is it possible to put it in a separate log (my home assistant log is huge).

Tried but error
2019-02-02 16:17:00.842949 WARNING AppDaemon: No app description found for: /config/appdaemon/apps/deconz_event.py - ignoring

appdaemon has its own logging, which you set in appdaemon.yaml

every app you create also needs a yaml part to activate it.
if you want to learn the basics from creating apps then you can learn it here:

Ah I thought you want an appdaemon app because we are in the appdaemon section here :wink:

Check out the deconz component page, everything is explained there. Appdaemon or normal hass automation etc.

For the appdaemon app, I will not explain ypu how to create an app from scratch, please use the official documentation for this.