Event information deconz with Xiaomi Cube

Have you also defined the correct values for the variables in the apps.yaml file? If not than that is missing.

Don’t you have any error messages in your logfile?
This is what I have in my apps.yaml:

cube_control:
  module: cube_control
  class: CubeControl
  event: deconz_event
  id: aqara_cube

The new release includes a configuration option through the phoscon app. See here, the actions then off course are set through deCONZ/Phoscon instead of Home Assistant so only actions on devices connected to deCONZ can be set.

1 Like

This is nice, but as you said you can only trigger things connected to deCONZ. Personally I don’t configure any actions in Phoscon to avoid duplicate automations by having all automations in one place.

I still got problems with the rotate events. I can’t get these into Hassio. The other events are coming through without a problem.

I have a Mi Magic Cube by the way.

Any idea why the rotate events doesn’t work? In Deconz I can see the rotation.

Likely the second sensor resource isn’t registered in the REST-API yet (this could happen with deCONZ versions prior to 2.05.69). To fix this with version 2.05.69 please delete and re-pair the Xiaomi Cube under Phoscon App > Menu > Switches.

deCONZ version is 2.05.69 and I’ve deleted and repaired the Mi Magic Cube. But still no rotation events in Hassio (Appdeamon- or deCONZ add-in).

Hmm strange, can you see the REST-API output for /sensors?
There should be two sensors for the Xiaomi Cube, one handles the rotation.

I finally could connect through the REST API. Looks like I only have one sensor, cause this is what I get when I go through the sensors:

"42": {
        "config": {
            "battery": 100,
            "on": true,
            "reachable": true,
            "temperature": 2100
        },
        "ep": 2,
        "etag": "dfd40db1a5ac4e47f785fee37d988f4b",
        "manufacturername": "LUMI",
        "mode": 1,
        "modelid": "lumi.sensor_cube",
        "name": "living_room_cube",
        "state": {
            "buttonevent": 7000,
            "gesture": 0,
            "lastupdated": "2019-09-12T21:53:46"
        },
        "swversion": "20160407",
        "type": "ZHASwitch",
        "uniqueid": "00:15:8d:00:01:04:1d:e0-02-0012"
    },

I’ve managed to get my Mi Magic Cube in the Switch Editor and see two sensors in the REST api.

This is what I did. I searched for the sensor for a second time: add new switch in the Phoscon App and push the reset button on the cube for two to five seconds (blue led on). In the mean time move and rotate the cube. In the REST api I now see two sensors: sensors/42 (flip, etc.) and sensors/46 (rotate).

1 Like

I am not using Appdaemon but use these automations to trigger the events, this works just fine with Deconz.

  trigger:
    platform: event
    event_type: deconz_event
    event_data:
      id: cube
  condition:
    condition: template
    value_template: >
      {% set valid_events = [1000, 2000, 3000, 4000, 5000, 6000] %}
      {{ trigger.event.data.event in valid_events }}
  action:
    service: light.turn_on
.........

The only problem is that I am not able to get the define the rotate event,

I am running the latest Deconz fw but how can I use this event in my automation?

@bramschats I now have two sensors in the REST Api:

Sensor 42:

{
    "config": {
        "battery": 88,
        "on": true,
        "reachable": true,
        "temperature": 2200
    },
    "ep": 2,
    "etag": "db00e7dbfd724436dc2771a907944141",
    "manufacturername": "LUMI",
    "mode": 1,
    "modelid": "lumi.sensor_cube",
    "name": "living_room_cube",
    "state": {
        "buttonevent": 7007,
        "gesture": 1,
        "lastupdated": "2019-09-14T18:56:58"
    },
    "swversion": "20160407",
    "type": "ZHASwitch",
    "uniqueid": "00:15:8d:00:01:04:1d:e0-02-0012"
}

and

Sensor 46:

{
    "config": {
        "battery": 88,
        "on": true,
        "reachable": true,
        "temperature": 2200
    },
    "ep": 3,
    "etag": "25f7594e13ab043a86d9ae745b80ebc4",
    "manufacturername": "LUMI",
    "mode": 1,
    "modelid": "lumi.sensor_cube",
    "name": "living_room_cube",
    "state": {
        "buttonevent": -5641,
        "gesture": 8,
        "lastupdated": "2019-09-14T18:56:44"
    },
    "type": "ZHASwitch",
    "uniqueid": "00:15:8d:00:01:04:1d:e0-03-000c"
}

With this I use these automations:

Rotate clockwise:

---
# Turn the volume up in the living room with the Xiaomi Cube
#
alias: living_room_cube_turn_volume_up
initial_state: 'on'
trigger:
  platform: event
  event_type: deconz_event
  event_data:
    id: living_room_cube
condition:
  condition: template
  value_template: >
    {{ trigger.event.data.event | int > 0 }}
action:
  - service: media_player.volume_set
    data_template:
      entity_id: media_player.woonkamer
      volume_level: >
        {% set state = states.media_player.woonkamer.attributes.volume_level + 0.05 -%}{{ state }}

and

Rotate counter clockwise:

---
# Turn the volume down in the living room with the Xiaomi Cube
#
alias: living_room_cube_turn_volume_down
initial_state: 'on'
trigger:
  platform: event
  event_type: deconz_event
  event_data:
    id: living_room_cube
condition:
  condition: template
  value_template: >
    {{ trigger.event.data.event | int < 0 }}
action:
  - service: media_player.volume_set
    data_template:
      entity_id: media_player.woonkamer
      volume_level: >
        {% set state = states.media_player.woonkamer.attributes.volume_level - 0.05 %}
        {% if state <= 0 %}
          {% set state = 0 %}{{ state }}
        {% else %}
          {{ state }}
        {% endif %}

When I am listening to Deconz_event under developer tools i can see the rotation event from the cube:

{
    "event_type": "deconz_event",
    "data": {
        "id": "cube",
        "event": 6005
    },
    "origin": "REMOTE",
    "time_fired": "2019-09-15T08:05:57.517384+00:00",
    "context": {
        "id": "748e2ecd4f3d491fac36a513e47eddd7",
        "parent_id": null,
        "user_id": null
    }
}
Event 25 fired 10:05 AM:
{
    "event_type": "deconz_event",
    "data": {
        "id": "cube",
        "event": -7492
    },
    "origin": "REMOTE",
    "time_fired": "2019-09-15T08:05:55.705658+00:00",
    "context": {
        "id": "fd2e3feba7ad48ea8cddb2e33d2d611b",
        "parent_id": null,
        "user_id": null
    }
}

The one with a negative id is rotate counter clockwise, the other is flip 90. I can use the counter clockwise event like you described but how to do this for the clockwise event?

Oké, but you’re already there. The event will be > 0 when you rotate the cube clockwise. So you can use both automations as I mentioned above.

I guess that won’t do the trick since all other events like flip 90, move etc all have integer numbers >0 for the same sensor id!

You could rename the ID in the REST API to cube_rotation. That way you separate the two sensors.

PUT http://ip-address:40850/api/restapi-key/sensors/46 and in the body:

{
  "name": "living_room_cube_rotation"
}

That would make these two sensors:

    "42": {
        "config": {
            "battery": 88,
            "on": true,
            "reachable": true,
            "temperature": 2200
        },
        "ep": 2,
        "etag": "bac52d206b56327d29293ebeb8f38fad",
        "manufacturername": "LUMI",
        "mode": 1,
        "modelid": "lumi.sensor_cube",
        "name": "living_room_cube",
        "state": {
            "buttonevent": 5000,
            "gesture": 5,
            "lastupdated": "2019-09-15T17:59:47"
        },
        "swversion": "20160407",
        "type": "ZHASwitch",
        "uniqueid": "00:15:8d:00:01:04:1d:e0-02-0012"
    },
    "46": {
        "config": {
            "battery": 88,
            "on": true,
            "reachable": true,
            "temperature": 2200
        },
        "ep": 3,
        "etag": "df9d650b630492cb4b077aaa6a7c6ca1",
        "manufacturername": "LUMI",
        "mode": 1,
        "modelid": "lumi.sensor_cube",
        "name": "living_room_cube_rotation",
        "state": {
            "buttonevent": 1416,
            "gesture": 7,
            "lastupdated": "2019-09-15T17:59:57"
        },
        "type": "ZHASwitch",
        "uniqueid": "00:15:8d:00:01:04:1d:e0-03-000c"
    }

And the volume automations:

---
# Turn the volume up in the living room with the Xiaomi Cube
#
alias: living_room_cube_turn_volume_up
initial_state: 'on'
trigger:
  platform: event
  event_type: deconz_event
  event_data:
    id: living_room_cube_rotation
condition:
  condition: template
  value_template: >
    {{ trigger.event.data.event | int > 0 }}
action:
  service: media_player.volume_set
  data_template:
    entity_id: media_player.woonkamer
    volume_level: >
      {% set state = states.media_player.woonkamer.attributes.volume_level + 0.02 -%}{{ state }}


---
# Turn the volume down in the living room with the Xiaomi Cube
#
alias: living_room_cube_turn_volume_down
initial_state: 'on'
trigger:
  platform: event
  event_type: deconz_event
  event_data:
    id: living_room_cube_rotation
condition:
  condition: template
  value_template: >
    {{ trigger.event.data.event | int < 0 }}
action:
  service: media_player.volume_set
  data_template:
    entity_id: media_player.woonkamer
    volume_level: >
      {% set state = states.media_player.woonkamer.attributes.volume_level - 0.02 %}
      {% if state <= 0 %}
        {% set state = 0 %}{{ state }}
      {% else %}
        {{ state }}
      {% endif %}
1 Like

Hmmm, seems like a straight forward thing to do but I am stuck with the URL!

http://192.168.25.25:40850/api/restapi-key/sensors/46

gives outpu:

[{"error":{"address":"/sensors/46","description":"unauthorized user","type":1}}]

within my browser! How can I get to the body of the code?

You must first obtain an api key:

Click in the Phoscon app => Gateway => Advanced => Authenticate app.

And next use the following REST Api command:

POST http://192.168.25.25:40850/api

With in the body:

{ "devicetype": "my application" }

As a result you get your restapi-key. So your command will be:

GET http://192.168.24.24:40850/api/restapi-key/sensors

With this you can get all your sensors from within deCONZ. This will include the Cube sensor(s).

You can read more about this at: https://dresden-elektronik.github.io/deconz-rest-doc/getting_started/

I now understand how this works and can see my sensors within the RESTapi:

  "43": {
    "config": {
      "battery": 98,
      "on": true,
      "reachable": true,
      "temperature": 2100
    },
    "ep": 2,
    "etag": "1b1ddf292e7faa9d796cbb21608c1b7b",
    "manufacturername": "LUMI",
    "mode": 1,
    "modelid": "lumi.sensor_cube.aqgl01",
    "name": "Cube",
    "state": {
      "buttonevent": 7000,
      "gesture": 0,
      "lastupdated": "2019-09-21T10:42:18"
    },
    "swversion": "20160704",
    "type": "ZHASwitch",
    "uniqueid": "00:15:8d:00:02:8f:72:ca-02-0012"
  },
  "44": {
    "config": {
      "battery": 98,
      "on": true,
      "reachable": true,
      "temperature": 2100
    },
    "ep": 3,
    "etag": "1b1ddf292e7faa9d796cbb21608c1b7b",
    "manufacturername": "LUMI",
    "mode": 1,
    "modelid": "lumi.sensor_cube.aqgl01",
    "name": "Cube",
    "state": {
      "buttonevent": -7492,
      "gesture": 8,
      "lastupdated": "2019-09-15T08:05:55"
    },
    "swversion": "20160704",
    "type": "ZHASwitch",
    "uniqueid": "00:15:8d:00:02:8f:72:ca-03-000c"
  },

Now I have to rename sensor 43 to cube_rotation but how do I save this to the API?

Thnx for your help I’am getting there… :wink:

Never mind, found it already…

PUT http://192.168.25.25:40850/api/...../sensors/43

{
  "name": "cube_rotation"
}

now it works… and I am going to test in right away!

Thnx! :+1:

Glad to hear you’ve got it working! I went the same route as you! :smiley::+1:t3: