Event information deconz with Xiaomi Cube

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:

Hello, I did the same thing and the new name cube_rotation isn’t exposed in the deconz_event. Is there a way to refresh device metadata?

{
    "event_type": "deconz_event",
    "data": {
        "id": "mi_magic_cube",
        "unique_id": "00:15:8d:00:02:a3:76:9c",
        "event": 6000
    },
    "origin": "LOCAL",
    "time_fired": "2019-10-06T14:32:58.710379+00:00",
    "context": {
        "id": "6145043fdfe64d8595bfa15fc3c40394",
        "parent_id": null,
        "user_id": null
    }
}

Hi

Great forum thread.

For those needing the renaming of the two cube devices in deconz I have summarized the workaround in another forum thread.

Hi @fantomnotabene
How do you get the Eventdata (this 4numbers) with templating?
I try to make a template sensor, but ehitout luck.

sensor:

  • platform: template
    sensors:
    aqara_cube_01:
    friendly_name: ‘aqara_cube_01’
    value_template: “{{states (‘sensor.cube_01’)}}”

I created the name cube_01 when i had conneting the aqara cube in the deconz web interface. The battery-State is shown as entity: sensor.cube_01_battery_level So i just guess the name in the template sensor script. The result: the state of the template sensor are always ‘unknown’.
Can sombody help?

Inside of an automation, triggered by “platform: event”, you can use
{{ trigger.event.data }}
Event should be filtered by deconz_event and your cube’s device id.

thanks - but that aren’t i’m looking for. i like to have the sensor data…

You can use input_text instead of sensor (or create template sensor and sync their states) and set its value via automation. Don’t know if there’s a better way.

Thanks - sounds good. Would you give me an example, to bring me on the right track?

Create an input_text entity.
Create a template sensor entity, which value_template points to an input_text's state (something like {{ states('input_text.cube') }}).
Then create an automation like this one:

- alias: ...
  trigger:
    platform: event
    event_type: deconz_event
    event_data:
      id: cube
  action:
    service: input_text.set_value
    entity_id: input_text.cube
    data_template:
      value: "{{ trigger.event.data.event }}"

Or you can even set text value to an input_text accordingly to event number if you know how to use template conditions.

2 Likes

Works perfect! Thanks a lot

I’ve been tried code under, I have two cubes, but it doesn’t working. When I’ve been trying deconz_event, it shows me events with event and gesture elements, but all sensors has only value 0 and nothing has been changed.

input_text:
  mi_magic_cube_1_event:
    name: Mi Magic Cube 1 Event
    initial: 0000
  mi_magic_cube_1_gesture:
    name: Mi Magic Cube 1 Gesture
    initial: 0
  mi_magic_cube_2_event:
    name: Mi Magic Cube 2 Event
    initial: 0000
  mi_magic_cube_2_gesture:
    name: Mi Magic Cube 2 Gesture
    initial: 0

sensor:
  - platform: template
    sensors:
      mi_magic_cube_1_event:
        value_template: "{{ states('input_text.mi_magic_cube_1_event') }}"
      mi_magic_cube_1_gesture:
        value_template: "{{ states('input_text.mi_magic_cube_1_gesture') }}"
      mi_magic_cube_2_event:
        value_template: "{{ states('input_text.mi_magic_cube_2_event') }}"
      mi_magic_cube_2_gesture:
        value_template: "{{ states('input_text.mi_magic_cube_2_gesture') }}"

automation mi_magic_cube_1_event:
- alias: Mi Magic Cube 1 event
  trigger:
    platform: event
    event_type: deconz_event
    event_data:
      id: mi_magic_cube_1
  action:
    service: input_text.set_value
    entity_id: input_text.mi_magic_1_event
    data_template:
      value: "{{ trigger.event.data.event }}"

automation mi_magic_cube_1_gesture:
- alias: Mi Magic Cube 1 gesture
  trigger:
    platform: event
    event_type: deconz_event
    event_data:
      id: mi_magic_cube_1
  action:
    service: input_text.set_value
    entity_id: input_text.mi_magic_1_gesture
    data_template:
      value: "{{ trigger.event.data.gesture }}"

automation mi_magic_cube_2_event:
- alias: Mi Magic Cube 2 event
  trigger:
    platform: event
    event_type: deconz_event
    event_data:
      id: mi_magic_cube_2
  action:
    service: input_text.set_value
    entity_id: input_text.mi_magic_2_event
    data_template:
      value: "{{ trigger.event.data.event }}"

automation mi_magic_cube_2_gesture:
- alias: Mi Magic Cube 2 gesture
  trigger:
    platform: event
    event_type: deconz_event
    event_data:
      id: mi_magic_cube_2
  action:
    service: input_text.set_value
    entity_id: input_text.mi_magic_2_gesture
    data_template:
      value: "{{ trigger.event.data.gesture }}"


The second. How to create code which changes numbers to human text ie. 1 - Shaking etc.? Thank you

input_texts has a _cube_ in their entity ids, but inside of an automations they are not.

Something like:

value: >-
  {% if trigger.event.data.gesture == X %}
  Sliding
  {% elif trigger.event.data.gesture == Y %}
  Shaking
  {% elif trigger.event.data.gesture == Z %}
  Dropping
  {% else %}
  etcetera...
  {% endif %}