🧯 ZHA - Xiaomi Cube Controller

(128 possible actions!!) Aqara works with T1 Pro and older cubes. This Blueprint uses a ZHA built sensor to sort out the 38(+54) commands available from the Xiaomi Magic Cube. This gives you the ability to trigger actions using the remote. NOTICE: Using this Blueprint and example scripts, this cube can be triggered over a hundred ways, but only 38(+54) of them are unique…

:bookmark_tabs: Changelog

  • 2024-06-04: Blueprint Input Sections for enhanced Descriptions.
  • 2023-10-20: Enhancement Add flip to side from any
  • 2023-08-07: Updates for Home Assistant 2023.8
    • Selector syntax change
    • Condition Selector addition (where applicable)
    • MQTT Discovery name changes (where applicable)
    • Clean-up code formatting
  • 2023-03-01: Add Author Tag. Bump HA required Version to 2023-3-0
  • 2023-01-19: Add instructions for new cube version Cube T1 Pro Action mode setup in Docs.
    • Add note that Drop is no longer available if using the Cube T1 Pro cube version.
  • 2022-12-12: Add Update Method Note, minor code change.
    • Name of Blueprint may have changed meaing you have to re-download with a new link.
    • If name changed, it is similar. Variables have not changed.
  • 2022-11-30: :tada::package::battery: First Version

:wrench: * Hardware Versions

Aqara / Lumi has released a new version of the cube. Box name listed as Cube T1 Pro. It is listed in your ZHA Device panel as lumi.remote.cagl02 by LUMI.

This device will work similar to the older versions if configured in the ACTION mode and NOT the SCENE mode. The exception being that the Drop function is no longer available.

If the only functions that you have available is rotate left/right and shake, your cube is in the wrong mode. To change modes, push the LINK button 5x swiftly and the mode should flip to the ACTION mode where the functions should work normally. You can test this by setting up the event sensor listed below and listening for ‘zha_cube_last_action’ events or by listening for ‘zha_events’ in the Developer event panel. Open your Home Assistant instance and show your event developer tools.

:crystal_ball: About this blueprint

Type of blueprint: AUTOMATION

What if I am having problems getting it going?

You can contact me for help, see the links below.

Why do I need this?

The Cube remote/switch device has a lot of ways to trigger it. This Blueprint makes it a bit easier to figure out what you are doing and remembering that in the future. It also makes sure the trigger is as clean and repeatable as possible, screening out false triggers and making the log clean and the experience good overall.

:wrench: Configuration

Requirements

  • To make the Blueprint work you will need a functional Magic Cube integrated to Home Assistant thru ZHA and find the sensor Device in the Home Assistant Device tab.
  • My ‘suggestion’ is that you do separate scripts for most, if not all of the actions you generate here. If you are using the UI editor for the simple things you are fine, but for more complicated things scripts may work better for you. This is my opinion and how I am using it, to each their own. See my example dimmer script below…
  • For this Blueprint to function properly, it must keep track of the last cube face that was triggered. This is done with an input_number helper. Open your Home Assistant instance and show your helper entities.

Create a number helper with the following parameters:

  • Minimum 0
  • Maximum 6
  • Step 1

Number Helper for this Blueprint

:card_index_dividers: Input fields

last_side_input/name: Last side up numeric helper
    Please select a Numeric Helper to be used to
    record the last side that saw action.

cube/name: Aqara Cube
    Select the Aqara Cube device

additional_conditions:
    Extra conditions you may want to add to this automation 
    (Example: Home occupied, TV on, etc)

shake:
  name: Shake the cube 🍕 Group 4
    This trigger only occurs once in the set-up.
    It can be combined in any group.'

drop:
  name: Drop the cube 🍕 Group 4
    **NOTE: NOT available with the ```Cube T1 Pro``` version of the cube!!**
    This trigger only occurs once in the set-up.
    It can be combined in any group.'

slide_any:
  name: Group 2 actions 🍒 Slide the cube with any side

doubletap_any:
  name: Group 2 actions 🍒 Double tap the cube with any side

flipped90_any:
  name: Group 2 actions 🍒 Flip the cube 90 degrees to any side

flipped180_any:
  name: Group 2 actions 🍒 Flip the cube 180 degrees any side

rotate_cw_any:
  name: Group 2 actions 🍒 Rotate cube clockwise with any side

rotate_ccw_any:
  name: Group 2 actions 🍒 Rotate cube counter clockwise with any side

**There is a set of these for all 6 sides. 36 inputs total..**

slide_face_0:
  name: Group 1 actions 🍎 Slide the cube with face 0 up

doubletap_face_0:
  name: Group 1 actions 🍎 Double tap the cube with face 0 up

flipped90_face_0:
  name: Group 1 actions 🍎 Flip the cube 90 degrees to face 0

flipped180_face_0:
  name: Group 1 actions 🍎 Flip the cube 180 degrees to face 0

flip_from_any_to_face_0:
  name: Group 1 actions 🍎 Flip the cube from any to face 0

rotate_cw_face_0:
  name: Group 1 actions 🍎 Rotate cube clockwise with face 0 up

rotate_ccw_face_0:
  name: Group 1 actions 🍎 Rotate cube counter clockwise with face 0 up

**There is a set of these for all 6 sides. 30 inputs total..**
The one action that flips 180 changes in every set.

0_from_1:
  name: Group 3 actions 🍐 Flip the cube to side 0 from side 1
    **!!Warning!!** The flip 90 automations 🍎 for side 0 and the ANY
    🍒 automations will also trigger.

0_from_2:
  name: Group 3 actions 🍐 Flip the cube to side 0 from side 2
    **!!Warning!!** The flip 90 automations 🍎 for side 0 and the ANY
    🍒 automations will also trigger.

0_from_3:
  name: Group 3 actions 🍐 Flip the cube to side 0 from side 3
    **!!Warning!!** The flip 180 automations 🍎 for side 0 and the
    ANY 🍒 automations will also trigger.

0_from_4:
  name: Group 3 actions 🍐 Flip the cube to side 0 from side 4
    **!!Warning!!** The flip 90 automations 🍎 for side 0 and the ANY
    🍒 automations will also trigger.

0_from_5:
  name: Group 3 actions 🍐 Flip the cube to side 0 from side 5
    **!!Warning!!** The flip 90 automations 🍎 for side 0 and the ANY
    🍒 automations will also trigger.

:eyes: :airplane: Extended Information

This Blueprint uses a ZHA Event Sensor to sort out the 38(+54) unique actions available from the Xiaomi Magic Cube Remote. (Some unique actions are available thru templating only. See the related document.)

The split out of functions gives you the ability to assign local scripts or functions to do the things you want the remote to do.

Functions that are left empty will simply do nothing.

:apple: There is a set of 36 event functions that will trigger on specific actions

on specific sides that are listed as Group 1 actions :apple:.

:cherries: There is a set of 6 event functions that will trigger on specific actions

on ANY side that are listed as Group 2 sctions :cherries:.

:pear: There is a set of 30 event functions that will trigger on cube flips to

& from a specific sides that are listed as Group 3 sctions :pear:.

:pizza: There are 2 actions (shake and drop) that only occur once and are OK to

be combined with any other group. Listed as Group 4 sections :pizza:.

Please be aware that ALL actions except the 2 listed above,

:pizza: will trigger an action in ALL 3 groups at the same time every time. Therefore I suggest if you just have a couple of things you want this remote to do that you choose the ANY / Group 2 / :cherries: events.

If you want more than a few events, you should select actions in Group 1 / :apple: OR Group 3 / :pear:.

With careful selection you can use mixed groups, but you run the risk of a single cube action triggering more than 1 Home Assistant action and making a mess of things :bento:.

NOTE: This blueprint references the sides from 0 to 5 like the ZHA integration does. Most ZHA blueprints reference the sides in the same order but use 1 thru 6. This was so I could re-use my Z2M code as-is. This does not affect the operation of the device.

:magic_wand: My Cube triggers on the Desk with a slight Bump. How do I fix that?

I have 3 of these cubes. If you aren’t careful they trigger and do all kinds of things just sitting on the desk. Awesome when you want it to do this, but a HUGE PITA if you don’t.

A friend of mine came up with this and it works awesome. He didn’t want people hassling him about remixes and such, so he let me put it on the Thingiverse. I printed one for each of my cubes and because it’s now parked at a diagonal it will not trigger, unless you knock it on the floor or something. I highly suggest you print yourself one of these or find someone to print one for you… It will make cube life much simpler… Magic Cube Holster / Parking Base / Stand / Sleep Station by SirGoodEnough - Thingiverse

NOTICE: This cube can be triggered over a hundred ways, but only 38 of them are unique

:wrench: How does this darn cube work?

There is sample code to make the template sensor in the help file on GitHib named the same as this one and in the community page related to this.

Seeing the cube commands for training the operator

Within this blueprint there is an event handler that will latch the last command that the blueprint finds and sends that to the event buss. From there a simple Template sensor can grab it and show you the last action sent.This will help when setting up new functions and to troubleshoot strange behaviors. Add an entity card in your dashboard for sensor.cube_last_action to see what actions occur as you move the cube.

Yaml file that contains the sample code here

Event Sensor in Action

Sample Script Generation Dashboard Entry

If you wish to ‘store’ these events you can add this sensor to recorder and it will save them for you.

:palm_down_hand: Getting Tap and Flip actions to work

I have had reports of the ‘tap’ action working. It was due to the lack of instructions provided by the manufacturer of the cube. Tap actions on the cube are initiated by sharply tapping the cube 2x on a hard surface like this:

Demo of Tap Action

In a similar manner, flips need to tap the surface at the end of the 90 or 180 flip. Setting up the template sensor above will help train you in the force needed for all the actions.

:speak_no_evil: Acknowledgments

This was ‘forked’ from

Aqara Cube ZHA - Simplified face-based device control Project authored by EdwardTFN (Edward Firmo) and he based on on several other giants that came up with most of the base code.

ZHA - Aqara Magic Cube (57 actions)

Aqara Magic Cube ZHA (51 actions)

ZHA - Aqara Magic Cube (24 actions)

I sincerely thank Them for their work. I wanted to support a version that was virtually the same as my Z2M version, and leverage all the documentation and code samples there but still port it to ZHA. I also had a better idea for troubleshooting info.

:sun_with_face: Dimmer Control

If you are looking for a dimmer control to change brightness based on rotation, here’s something I cobbled together from other community posts here and there. ( Credit Profile - yourigh - Home Assistant Community and others )

I did this with all the complicated stuff in a script that is called with data from the blueprint automation. Then the complicated part is all in 1 place and there is only 1 copy of it. The same script works for both increase and decrease of brightness because the angle in the cube goes positive when turning clockwise and negative when going counter clockwise.

Code Examples are found in the Yaml file here If you want to create the script file using a script BluePrint, I have that for you right here: Dimmer Control BluePrint

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

  • The script reduces the angle number to 40% of the rotation angle (you can change this, but 40% works well for my needs).
  • It then grabs the current brightness from the light entity (as a % of the full scale 255 number).
  • The new_brightness target is then calculated.
  • It then checks if the light is already off, and if so, leaves it off.
  • It makes sure the new_brightness is above, in my case, 10% so all the lights come on.
  • It then makes sure that if new_brightness >90%, it is set to 100% and not over that.
  • Finally it provides the calculated brightness %.

:vertical_traffic_light: Color Control

I also extended this to controlling the color one octet (color) at a time. Each color will use both rotations on one side of the cube. Colors are changed one at a time (Red or Green or Blue) and change the amount (positive or negative) based on how far you rotate the cube.

Code Examples are found in the Yaml file here If you want to create the color change script file using a script BluePrint, I have that for you right here: Color Change BluePrint

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

  • The script reduces the angle number to 60% of the rotation angle (you can change this, but 60% works well for my needs).
  • It then grabs the current colors from the light entity and puts them into a list.
  • The new color target is then calculated.
  • It makes sure the new color is not negative.
  • It then makes sure that the new color is not over 255.
  • Finally it provides the calculated color if not in the limits.

This can be used over and over for as many lights as you want to control. But you will need to repeat this and modify it for red and blue color octets.

:repeat: :red_circle: Using Rotate CW and CCW as a Short-Press / Long-Press Toggle

Not enough switch positions for you? How about a possible 24 more? I came up with some scripts you can add to Home Assistant and call for more actions. One is for CW rotation < 100 degrees, another for > 100 degrees. Also the same for CCW. These are can be called from the Group 1 :apple: rotate actions and the Group 2 :cherries: rotate actions. Match the CW call/recieve or the CCW call/receive pairs together or you are going to be sad. :weary:

Here is a sample of what you put into the script Blueprint UI. It will need to be a manual YAML edit and contain your specific variables. What you see here is one from my config.

Code Examples are found in the Yaml file here If you want to create the long / short rotation switch script file using a script BluePrint, I have that for you right here: Long Short Rotation Switch BluePrint

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

You can also do this buy going full gui and picking the matching template out of the below section and filling it in similar to this:

Full GUI Example

These are the standalone scripts that are ‘called’ from the Script calling yaml shown above. If you don’t need all of these only install the ones you will use.

This is a homeassistant.toggle action, so it can toggle anything that that service can handle. Changing it to homeassistant.turn_on or homeassistant.turn_off would change the behavior slightly if this fits your needs better. Using this integration, you can control lights, switches, locks, and lots of different things.

Code Examples are found in the Yaml file here If you want to create the long / short rotation switch script file using a script BluePrint, I have that for you right here: Long Short Rotation Switch BluePrint

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

:headphones: Volume Control

This script was invented to be used with my Magic Cube Blueprints. I released it as a standalone Script Blueprint because because of requests to extend the function of my BP’s with how to convert cube rotation to volume in other places. Research found me this Post from Petro which had a very elegant solution to the problem, It was very easy decision to adopt it here.

Code Examples are found in the Yaml file here If you want to create the script file using a script BluePrint, I have that for you right here: Volume Control BluePrint Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

  • The script scales the angle number of the rotation angle (you can change this, but 30 works well for my needs).
  • You send the script the media_player that you want to adjust.
  • You use a template (examples in the script BP description) to send the angle to the script.
  • Some math happens and it uses the amount you turn the cube to adjust the volume.

NOTICE when building action scripts…

It has been found that some set-ups use trigger.payload_json.action_angle here and others only accept trigger.payload_json.angle here. I have not been able to determine which attributes are available in which version of firmware and/or configurations, so it is up to you to determine the one you need here. Look in the Device listing for this cube and determine which version of angle is one of the listed sensors. That would be the one to use here.

Method to use Group 3 :pear: actions and not interfere with Group 1 :apple:

Not enough switch positions for you still? How about another posible 30 more?

This is another ‘action’ that I stumbled upon. I noticed if you turn the cube from side to side very gently, it will internally register as being on a new side, but the flip action doesn’t register. Then if you slide the cube, it will send out an action of slide on side 5 from side 2, or whatever side combo’s you choose. I used 5 from 2 in the example, but you can use any of them.

Showing slide 5 from 2 on the Cube Action Sensor

I’ll be honest, this is the trickiest thing to do yet and I’m not sure it is worth the trouble, but if you want an action in your pocket as a secret action that only you know, this is the trick. Using a soft surface like a towel helps to keep the flip from registering when you set it down.

NOTE: In this example the slide side 5 will also trigger. You may need to add a condition to prevent that from triggering. An example for this 5 from 2 example is to put this condition on the slide side 5 action {{ not last_side == 2 }} before the thing you want to do. Or you can just not have a slide 5 action.

It is as simple as adding a condition of let’s say ‘slide’ in one of the Group 3 :pear: slots.

Here is the GUI editor showing this.

Full GUI Example Conditional side to side

Code Examples are found in the Yaml file here

:sun_with_face: :snowflake: Troubleshooting tip

If you are troubleshooting and you want to see more traces back when doing so, here is a TIP I’ve found. Manually edit the automation created with the ui editor (or manually with a text editor) and add the following to have this automation contain 10 traces instead of the normal 5. Then if the automation is triggering often, you can see the last 10 traces to help you decide what the issue is. HA Docs on this here.

trace: stored_traces: 10

:envelope_with_arrow: Version Updates

Updates will be published on my GIT repository with the rest of my Home Assistant Blueprint collection.

:envelope_with_arrow: There is not an official version control system for Blueprints. However I have found something that comes pretty close. It is not perfect, but for MOST Blueprints, it does just fine. I encourage you to check this script out and use it to easily check if I have updated this blueprint. :link:koter84 Blueprint Update Script

Please Click the :orange_heart: at the end of this top Post if you find this Useful

:calling: Software to Download :floppy_disk:

HA link to download blueprint: Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

Direct link to download Blueprint: https://github.com/SirGoodenough/HA_Blueprints/blob/master/Automations/ZHA-Xiaomi_Cube_Controller.yaml

HA_Blueprints/Automations/ZHA-Xiaomi_Cube_Controller.yaml at master · SirGoodenough/HA_Blueprints · GitHub.

:globe_with_meridians: All My Blueprints

Link to ALL my Blueprints

https://github.com/SirGoodenough/HA_Blueprints/blob/master/README.md

:man_juggling:t5: Contact Links or see my other work

What are we Fixing Today Homepage / Website: https://www.WhatAreWeFixing.Today/

Channel Link URL: (WhatAreWeFixingToday) https://bit.ly/WhatAreWeFixingTodaysYT

Discord Guild: (Sir_Goodenough#9683) WhatAreWeFixingToday?

:cheese: If you want to support me

Buy me Coffee: https://www.buymeacoffee.com/SirGoodenough

PayPal one-off donation link: PayPal.Me

#WhatAreWeFixingToday

#SirGoodEnough

11 Likes

@Slaytanic
I did not want to hijack his topic with talk of my Blueprint…
Like it says in the message above, if the 0.4 is still too big of a bump for you and not smooth, change it so smaller, maybe 0.1 or something.
If you look above on the brightness I personally use (0.4) 40%, but on my color change one I use (0.6) 60%. Any value up to (1.0) 100% would work in there.

1 Like

Thank you for your fast and excellent suport!! @Sir_Goodenough .with 0.2 it’s smoother for me…almost perfect ;-). I"lll try otherr values too. But how can i port this to volume level of mefia players?

1 Like

I’m not much of a media player guy. However if there is a place where you tell it a range of number for the set volume level, the same logic in these templates should be easily adaptable for that.

1 Like

To anyone using the PRO T1 version of the cube…

The cube has a scene mode, a throw action, a hold action, and other features that are NOT being exported thru zigpy into ZHA.
I suggest that you go to

And ask for the new features to be installed.

I’m trying to generate some traction on the issue above. If you have the Pro version of the cube and are using ZHA, you are not seeing what this new cube can do…

  1. You are not able to switch between Action Mode and Scene mode without taking the cube apart. This works in the new one.
  2. You are missing the new ‘Throw’ action.
  3. The Drop action that is modeled in the old cube no longer exists and doesn’t work in the Pro cube.
  4. Temperature reading sensor from the new Cube has been deleted, so it reads a static 25C now in the data. The data output has become obsolete.

I strongly encourage you to pop into the link above and click the thumbs-up emoji to show your interest in getting this fixed. Add a comment if you have found other problems using the Pro cube on the old cube template.

This will help garner interest and maybe someone will step up and help us with a fix. The more people that are seen ‘touching’ the request by the developers the better.

Also if you are able to program devices in the Zigpy world, please help with the creation of a new profile for the Pro cube.

New Version:

:bookmark_tabs: Changelog

  • 2023-03-01: Add Author Tag. Bump HA required Version to 2023-3-0

@Sir_Goodenough when i try to use this link to add the blueprint to my HA i get the following error

Invalid blueprint: extra keys not allowed @ data[‘blueprint’][‘author’]. Got ‘SirGoodenough’

any ideas? thanks

This blueprint requires that you be using Home Assistant 2023.3.0 minimum. That is where this new feature has been added.

ahhh my mistake thank you.

I connected the magic cube through the Xiaomi gateway, but there is only one sensor.0x00000000_action. Why am I unable to add a device?

I’d suggest it didn’t add properly. That number is not a valid IEEE address.
Something went badly.

Troubleshooting steps…

@Sir_Goodenough Thank you for all your hard work on this. The blueprint looks fantastic. I have what I hope is a quick (albeit bone-headed) question. I read through everything here including your tidbit about the T1 Pro missing functionality in zigpy. In my case I only seem to be able to trigger rotation (right/left) and “shaken” events. I considered the cube’s mode is incorrect, but the scene mode doesn’t appear to support rotation, so I’m assuming I’m in action mode. Any tips?

Scene mode does rotate. You are in scene mode, switch it to action mode.
Here is a jump link to a list of what happens in action mode and scene mode with Z2M anyway…

Awesome. Thank you. Changing the mode seems to have done the trick. I do have one more question. I noticed that when you describe the helper entity, you indicate the range should be 0-6. I’m curious as to why it’s not 0-5 (assuming that 6 would actually be out of range).

Thanks again for all your help and hard work.

0 to 6 or 0 to 5 would be fine there. Either will work.
It was a carry-over from when I used to need to have people make an input helper for Z2M, which is the BP I built first.

I now build the input helper for you in the Z2M ones. I would do that here if I could…

I have added 3 new script blueprints to replace the yaml scripts I had offered as samples before. I have also cleaned the coding up on these and simplified them.
They may now be suitable for use in other people Blueprints and/or your bare automation to help with light dimming with the cube, color change with the cube, and simulating long / short button press with the cube.
I am now working on one to do volume control.

Cube Light Dimming: Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

Cube Color Change: Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

Cube Long/Short Press Simulation: Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

2 Likes

Hey @Sir_Goodenough, I’m using your BPs and they work flawlessly, thank you so much!

I was wondering about volume control - you mentioned back in April that you were working on one. I was curious to see how far you got. I was also wondering if I could adapt the light dimmer script yaml by changing domain from light to media_player (and a few other things). I did try instead using a basic binding of cw/ccw to media_player.volume_up/down, which does work, but the rotation fidelity isn’t smooth or consistent so I was looking for something nicer.

Try changing the multipliet so that you have to spin it way more times. This will smooth it out some. Otherwise we would have to make it do 5% bumps each time it detects a movement.
There is a helper BP I wrote in my library which does ‘short press’ / ‘long press’. You could move the volume like 5% on the short and 10% on the long?
Just thinking out of the box on how I would do this.

I am in the middle of an update of all my BP’s right now, so if you come up with something you think works well, drop it into an issue as a feature request, we can incorporate it in as a helper BP or something.

1 Like

I opened a feature request. It would be helpful if you could add any code you tried so far or details that would help me. I am not much of a media player person and want this to work for you (everyone)…

I have a beta version of the volume control BP I’m about to release if you want to test it.
It is your request after all.

(For future inquiries…) This link will not work forever and will eventually get merged into master upon release then deleted. Change ‘Nuggets’ to ‘master’ and you should be able to find it again.