Mi Magic Cube

Yes. I know I need to listen for events. That is the same for all deconz switches.

But the issue is that there is nothing that separates rotation from other events and a rotation can have the same value as a move or turn of the cube.

I have noticed something new.

I deleted my Magic Cube and cleaned manually the core.device_registry and core.entity_registry.

And then restarted HA and repaired the cube.

It then gets recorded as switch_81 and switch_82. 81 is battery sensor also.

Then I restart Home Assistant and the switch_82 autorenames (I did nothing) to Mi Magic Cube. The battery sensor stays as switch_81.

Now turning the cube is switch_82. And rotation are events from mi_magic_cube.

Then I rename the device in Phoscon and all events become the new name and you cannot separate the events again. The problem is that the Cube gets registered as two different switches by default but the minute you rename the cube to something else they merge to one. If the event names has a suffix auto appended it would work.

ok bro

this is my Hassio system

have deConz running inside Hassio

and

hope this help

Yes exactly what I get and the event 1002 can mean flip from face 2 to 1 as well as meaning rotate clockwise 1002 steps. There is no information in the event JSON that says if it is one or the other

I wrote these automation

it just send the event to a MQTT with a bit of English was getting lost

base on

as i dont use appdaemon app

- alias: 'SquareBox Slide'
  initial_state: 'on'
  trigger:
    platform: event
    event_type: deconz_event
    event_data:
      id: magic_cube
  condition:
    condition: template
    value_template: >
      {% set valid_events = [1000, 2000, 3000, 4000, 5000, 6000] %}
      {{ trigger.event.data.event in valid_events }}
  action:   
  - data_template:
      payload: >
        Slide-{{ trigger.event.data.event }}
      topic: home/magic_cube
    service: mqtt.publish    

- alias: 'SquareBox double tap'
  initial_state: 'on'
  trigger:
    platform: event
    event_type: deconz_event
    event_data:
      id: magic_cube
  condition:
    condition: template
    value_template: >
      {% set valid_events = [1001, 2002, 3003, 4004, 5005, 6006] %}
      {{ trigger.event.data.event in valid_events }}
  action:   
  - data:
      payload: double tap
      topic: home/magic_cube
    service: mqtt.publish 

- alias: 'SquareBox flip180'
  initial_state: 'on'
  trigger:
    platform: event
    event_type: deconz_event
    event_data:
      id: magic_cube
  condition:
    condition: template
    value_template: >
      {% set valid_events = [1006, 2005, 3004, 4003, 5002, 6001] %}
      {{ trigger.event.data.event in valid_events }}
  action:   
  - data:
      payload: flip180
      topic: home/magic_cube
    service: mqtt.publish 

- alias: 'SquareBox flip90'
  initial_state: 'on'
  trigger:
    platform: event
    event_type: deconz_event
    event_data:
      id: magic_cube
  condition:
    condition: template
    value_template: >
      {% set valid_events = [1002, 1003, 1004, 1005, 2001, 2003, 2004, 2006, 3001, 3002, 3005, 3006, 4001, 4002, 4005, 4006, 5001, 5003, 5004, 5006, 6002, 6003, 6004, 6005] %}
      {{ trigger.event.data.event in valid_events }}
  action:   
  - data_template:
      topic: home/magic_cube
      payload: > 
        flip-{{ trigger.event.data.event }}
    service: mqtt.publish 

- alias: 'SquareBox shake'
  initial_state: 'on'
  trigger:
    platform: event
    event_type: deconz_event
    event_data:
      id: magic_cube
  condition:
    condition: template
    value_template: >
      {% set valid_events = [7007] %}
      {{ trigger.event.data.event in valid_events }}
  action:   
  - data_template:
      payload: shake
      topic: home/magic_cube
    service: mqtt.publish 

- alias: 'SquareBox fall'
  initial_state: 'on'
  trigger:
    platform: event
    event_type: deconz_event
    event_data:
      id: magic_cube
  condition:
    condition: template
    value_template: >
      {% set valid_events = [7008] %}
      {{ trigger.event.data.event in valid_events }}
  action:   
  - data_template:
      payload: fall
      topic: home/magic_cube
    service: mqtt.publish 

- alias: 'SquareBox wake'
  initial_state: 'on'
  trigger:
    platform: event
    event_type: deconz_event
    event_data:
      id: magic_cube
  condition:
    condition: template
    value_template: >
      {% set valid_events = [7000] %}
      {{ trigger.event.data.event in valid_events }}
  action:   
  - data_template:
      payload: wake
      topic: home/magic_cube
    service: mqtt.publish 

and its sensor

  - platform: mqtt
    name: "magic cube"
    state_topic: "home/magic_cube"
    qos: 1
1 Like

@myle

Your automation will trigger when you rotate the cube clockwise and the rotation happen to match one of your listed values. It may not happen often but it will happen. Normal rotation values easily fall in the 4 digit numbers. There is nothing you do that prevents that

Actually the link you referred to is a good one because further down that thread someone guides us to a work around involving hacking the single sensor into two and being able to set the names as well so rotation and other gestures are separate.

I will give that a try. Really deconz for HA should do either this or include the gesture value in the reported event data instead of only the event number

OK.

This is not perfect but here is a summary of the recipe to use the Mi / Aqara / Xiaomi Magic Cube in Home Assistant through Deconz. This is unique to Deconz!

First you pair the device in Phoscon. It seems to go best if you pair it while flipping and rotating it.

Deconz now actually created TWO devices. As they are created they will be called Switch## where ## is a number that depends on how many devices you have. Home assistant auto discover these.

It is a really good idea if you before restarting Home Assistant go to the Event viewer and listen for “deconz_event” and note which switch number is reported when to flip the cube or move it and which switch number is reported when you rotate the cube. If you rotate counter-clockwise you see negative event numbers. Note these two numbers and which does which.

The minute you reboot Home Assistant one of the two devices get renamed to the name that Phoscon/Deconz auto creates which is “Mi Magic Cube”. The other remains a Switch ##

What we need to do now is to rename these two devices in Deconz that that requires some hacking via the Rest API of Deconz. But don’t worry. It is geeky but easy when you follow this guide.

First we need to get an API key to be able to send rest commands to Deconz. You can create a new but the easiest is to use the one that Home Assistant has stored and uses. It is stored in …/.storage/core.config_entries

.storage is a hidden directory in the same directory where you have your configuration.yaml file.

Do a
cat core.config_entries | grep "api_key"

That should only return on line and that will be the api key for your Deconz

The easy way to send Rest API commands is to use curl from command line

The syntax is

curl -i -X PUT -H 'Content-Type: application/json' -d '{"parameter": "value"}' http://IP-address-of-Deconz:port/api/APIKEY/sensors/...

The port is 80 if Deconz is a standalone installation on bare metal and if you use the Home Assistant Addon the port is by default 40850

So the goal now is to change the names of the two sensors so that Home Assistant gets different names of events depending on rotation vs other gestures

So here is an example of changing the name of sensors 83 and 84 to Magic Cube Rotation and Magic Cube Gestures. The example IP is 192.168.1.15, Deconz as HA addon, and API key 0075B5E71E

curl -i -X PUT -H 'Content-Type: application/json' -d '{"name": "Magic Cube Rotation"}' http://192.168.1.15:40850/api/0075B5E71E/sensors/83

curl -i -X PUT -H 'Content-Type: application/json' -d '{"name": "Magic Cube Gestures"}' http://192.168.1.15:40850/api/0075B5E71E/sensors/84

If you did not record the numbers of the switches or do not want to re-pair, you can list all your sensors by simply looking up

http://192.168.1.15:40850/api/0075B5E71E/sensors

That returns all your sensors in a compressed JSON format which you can paste into a JSON beautifier (plenty of them on the net) and you can then find the two device numbers for your Cube

When you have renamed your two sensors you have to restart home assistant.

And then the gestures and rotation events should have the two names you chose.

It is a bit of a work around but at least it creates predictable names that you can use in many many automations. Should you ever need to re-pair the cube again, just repeat the renaming steps and all fits again even if the switch numbers changed.

This is how i (with help of the community) solved it to automate volume rotating the cube.

Use zigbee2mqtt and forget this diffculty.

1 Like

Could you explain any further?
I’ve got the Aqara cube and have it paired to zigbee2mqtt. Not sure where to go from now though.

Thanks

you solved?

You might visit zigbee2mqtt.io and lock for further information there.
Ive managed the mi cube with zigbee2mqtt and nodered for automation part :hugs:

In case someone find it usefull I made an internet radio remote control with the xiaomi cube and zigbee2mqtt, and placed channel logos on the faces of the cube:
image
That was a fun project! You can read more about it on my blog here
https://www.dingus.dk/xiaomi-cube-in-home-assistant-as-remote-control/
You can find my Home Assistant automation there if you want to do something similar. You could also have different playlists for each face of the cube.

9 Likes

10 out 10 bro

Thats thinking out side the BOX magic cube

Fellow Dane and what a nice idea!
Gotta get a cube more and adopt!!!

I’m wondering if my cube is broken. It’s connected via a Conbee2, which again is directly connected to my Rpi4 running HA supervised.

It seems, that I have a newer version of whatever since the events look different to what I see here.
I have a “Gesture” as well.

Here’s an example:
{
“event_type”: “deconz_event”,
“data”: {
“id”: “mi_magic_cube”,
“unique_id”: “00:15:8d:00:05:30:d6:33”,
“event”: 3001,
“gesture”: 3
},
“origin”: “LOCAL”,
“time_fired”: “2020-07-07T15:44:47.337803+00:00”,
“context”: {
“id”: “fff96a3c3b1e4133a36835598982af56”,
“parent_id”: null,
“user_id”: null
}
}

What doesn’t work for me, if I rotate the cube left/right, but staying on the same side, it doesn’t fire an event.
I see e.g. dingusdk is using it to change the volume.
Is mine broken, or does this not work in my combination?

this is mine spining left ant clock

clock wise

image

\

@dingusdk is using zigbee2mqtt not deconz

I just removed the cube from my home

It was fun to create the automations and play with. But in daily use tapping a cube on the table and rotating it which does not really work well for a volume control, it is adds little value. I used the 6 sides to choose between mode for my Harmony. Technically it works. But in practical use? Maybe if you have kids.
I also have the same feature implemented with two 4 button philips controls. With long press I can have 7 modes + off. And the other is used for Google chromecast volume and only TV on, Chromecast on, and off.
After many months I found that both my wife and I would use the Philips remotes and voice control. The Mi Cube is just collecting dust.

Just wanted to share this in case someone thinks about buying it. Real buttons and voice is just more natural than flipping a cube.

Maybe some have a fun game to use it for? Or you roll it and Alexa announce “doggy”, “missionary”, “rope”. :wink:

I agree.

The cube was fun for a wile, However the novelty has worn off and its more of a nuisance now.

I’m struggling to find a productive use for it.

Right now it’s setup to turn one lamp on and off with any action other then “shake” or “tap”, they are not mapped. A simple toggle switch more or less.