(128 possible actions!!) This Blueprint uses a Zigbee2MQTT 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 98 ways, but only 38(+54) of them are uniqueā¦
Changelog
- 2022-05-05: Updated for 2022.5.0 HA. Added Markdown to !input Descriptions plus shortcut or.
-
2022-04-26 update-B UPDATE: No code changes. Added examples to provide another 30 more ways to trigger something using conditionals in Group 3
,
- 2022-04-26 update-A UPDATE: No code changes. Added examples to provide 24 more ways to trigger something using the rotate sensor as a device toggle, both long and short for each rotate sensor.
-
2022-04-26: Re-configure to add 30 Action Methods !!NOTICE!! If you are upgrading the Blueprint, upgrade the template sensor as well. The variables are differentā¦
- Add 30 flip actions for any side to any side addressing
- Add ālast_sideā variable to display sensor and code
- Change variable named āeventā into āactionā fo clarity
- Change variable named āsub-eventā into āsideā for clarity
- Added Group 1
, Group 2
, & Group 3
to help users decide which sensors to populate
- Added Emojis to help people visualize what action is in what Group
-
2022-04-11: No Code Change. Added guidance to solve missing Action Sensor condition in this document.
- Example code bug fix from Michael Fischer
-
2022-03-17: Added 6 functions that do not care about side. Makes it simple if you only want a couple of functions.
- Added some aliases on some choose statements to improve Trace Diagrams and Troubleshooting.
- 2022-03-12: Changed de-bounce logic from not repeating the last action to single mode and added a 1 second delay at the end. Was hard to do the same action twice (IE: Rotation) as the logic would prevent it.
- 2022-02-15.1: Later that same day realized that if you have more than 1 cube, the event will be lacking so added ID.
-
2022-02-15: Forked from Z2M - Xiaomi Cube Controller Version 1.2
- Updated Documentation.
- Added Latched event sensor.
Get Started
Updates will be published on my GIT repository with the rest of my Home Assistant Blueprint collection.
I have 2 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ā¦
Option 1: My Home Assistant
Click the badge to import this Blueprint
Please Click the
at the end of the Top Post if you find this Useful
Option 2: Direct Link
Copy this link if you want to import the blueprint in your installation.
https://github.com/SirGoodenough/HA_Blueprints/blob/master/Automations/Zigbee2MQTT%20-%20Xiaomi%20Cube%20Controller.yaml
Description
This Blueprint uses a Zigbee2MQTT built sensor to sort out the 38 commands available from the Xiaomi Magic Cube Remote. 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.
There is a set of 36 event functions that will trigger on specific actions on specific sides that are listed as Group 1 actions
There is a set of 6 event functions that will trigger on specific actions on ANY side that are listed as Group 2 sctions
There is a set of 30 event functions that will trigger on cube flips to & froma specific sides that are listed as Group 3 sctions
There are 2 actions (shake and drop) that only occur once and are OK to be combined with any other group
Please be aware that ALL actions except the 2 listed above / 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 /
events. If you want more than a few events, you should select actions in Group 1 /
OR Group 3 /
. 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
.
NOTICE: This cube can be triggered 74 ways, but only 38 of them are unique
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.
template:
- trigger:
- platform: event
event_type: cube_last_action
sensor:
- name: "Cube Last Action"
unique_id: Any-unique-string-here-MUST-be-unique
icon: mdi:eye-refresh-outline
attributes:
friendly_name: "Cube Action"
state: >
{{ trigger.event.data.friendly_name }} -
{{ trigger.event.data.action }} -
{{ trigger.event.data.side }} frm
{{ trigger.event.data.last_side }}
Event Sensor in Action
If you wish to āstoreā these events you can add this sensor to recorder and it will save them for you.
Programming actions
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ā¦
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 manfacturer of the cube. Tap acrions on the cube are initiated by sharply tapping the cube 2x on a hard surface like this:
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.
This was āforkedā from āZ2M - Xiaomi Cube Controllerā
V1.2 project authored by luckypoppy and the friends he pulled together to create the base.I sincerely thank Him (Them) for their work. I felt there needed to be more documentation for rookie users to properly set this up. I had quite a few questions and when I saw a few questions in that chat from people struggling, I wanted to help. I also had a better idea for troubleshooting info.
First, letās go over Blueprints and what they are. Blueprints are a way to share automations and is built into Home Assistant. Simple as that. You can import my template code and a copy of it will reside in your configuration. Once there, you can can edit it (if you need changes only) or you can call up that Blueprint to build an automation. It will collect the information needed based on your entities and your personal adjustments, and provide a working automation. You will have to have or add the required hardware and entities that the Blueprint needs to function.
Usage
Installation
- Open Home Assistant with administrator privileges and on a Lovelace screen, click anywhere in the main entity area and type the letter ācā. A selection box should pop up. Type blue and select the button to navigate to blueprints. You can also find blueprints by selecting configuration from the left menu and then blueprints from the center menu.
- Once there, click on the āImport Blueprintā button in the lower right side of the main screen.
- In the āURL of the blueprintā line type or paste in the URL of my Blueprint. I have the blueprint stored on my Public GitHub:
To make the blueprint work it will need
To make the Blueprint work you will need a functional Magic Cube integrated to Home Assistant thri Zigbee2MQTT and find the sensor entity in the Home Assistant Device tab that Z2M imported which is named similar this:
- āāāsensor.xxDevice_Namexx_actionāāā
If you do not see that sensor, āLegacyAPIā might not be selected in the Zigbee2MQTT settings - settings - advanced menu. Please find and check/select that setting like so:
ā SCROLL DOWN ā
ā Another menu item ā
The other 4 imported sensors in this Device can be disabled as they will not be used.
Once you have found the entity_id you can build the Automation. To build the automation:
Code Samples for the items in the next few sections can be found in my Home-Assistant Config here:
- Home-Assistant-Config/Z2M-Xiaomi_Cube_A2.yaml at master Ā· SirGoodenough/Home-Assistant-Config Ā· GitHub
- Home-Assistant-Config/cube_script.yaml at master Ā· SirGoodenough/Home-Assistant-Config Ā· GitHub
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.
In the blueprint automation:
rotate_cw_face_0:
- service: script.cube_dimmer_control
data:
angle: "{{ trigger.to_state.attributes.action_angle }}"
light: light.bulb1
rotate_ccw_face_0:
- service: script.cube_dimmer_control
data:
angle: "{{ trigger.to_state.attributes.action_angle }}"
light: light.bulb1
Then this is the script thatās called to do the heavy lifting. It works for both CW and CCW cube rotations.
In the script intrgration:
cube_dimmer_control:
description: Template Dimmer Control
variables:
angle:
light:
sequence:
- service: light.turn_on
data_template:
entity_id: "{{ light }}"
brightness_pct: >
{% set step_size = angle * 0.4 %}
{# Get brightness as a percent. #}
{% set cb = (state_attr( light, 'brightness') | float(10) / 255.0) * 100.0 %}
{% set new_brightness = cb | int(10) + step_size %}
{% if new_brightness < 5 %}
{# If it gets really low set to 0. Adjust if needed for your lights. #}
0
{% elif new_brightness <= 10 %}
{# If it's not quite 0, set to a minimum working brightness. #}
10
{% elif 91 <= new_brightness < (90 + step_size) %}
{# If it's almost full brightness, set to 100%. #}
100
{% else %}
{# Send actual calculated value. #}
{{ new_brightness }}
{% endif %}
- 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 %.
Color Control
I also extended this to controlling the color one octet (color) at a time. Each color will use both rotations on one sude of the cube. Colors are changed one at a time (Red or Green or Blue) and change the amount (positive ot negative) based on how far you rotate the cube.
In the blueprint automation:
## Side 5 green
rotate_cw_face_5:
- service: script.cube_green_color_control
data:
angle: "{{ trigger.to_state.attributes.action_angle }}"
light: light.grp_studio
rotate_ccw_face_5:
- service: script.cube_green_color_control
data:
angle: "{{ trigger.to_state.attributes.action_angle }}"
light: light.grp_studio
Then this is the script thatās called to do the heavy lifting. It works for both CW and CCW cube rotations.
In the Script Intrgration:
cube_green_color_control:
description: Control the green part of the color
variables:
light:
angle:
sequence:
- service: light.turn_on
data_template:
entity_id: "{{ light }}"
rgb_color: >
{% set step_size = angle * 0.6 %}
{% set color = state_attr(light, 'rgb_color') %}
{% set R = color[0] %}
{% set G = color[1] %}
{% set B = color[2] %}
{% set new_green = G + step_size | int(0) %}
{% if new_green < 0 %}
{# Make sure it doesn't go negative #}
{{ R, 0, B }}
{% elif new_green > 255 %}
{# Make sure it doesn't go over 255 #}
{{ R, 255, B }}
{% else %}
{# Send the value calculated #}
{{ R, new_green, B }}
{% endif %}
- 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.
Using Rotate CW and CCW as a Short-Press / Long-Press Toggle
Not enough switch positions for you? How about a posible 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 rotate actions and the Group 2
rotate actions. Match the CW call/recieve or the CCW call/receive pairs together or you are going to be sad.
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.
service: script.cube_long_cw_toggle
data:
angle: '{{ trigger.to_state.attributes.action_angle }}'
entity: light.livingroomlight
And if you are editing it manually in an editor inside the Script calling yaml, this is the way it should look for rotate ce face 3, as an example.
rotate_cw_face_3:
- service: cube_long_cw_toggle
data:
angle: "{{ trigger.to_state.attributes.action_angle }}"
entity: light.livingroomlight
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:
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.
script:
cube_short_cw_toggle:
description: CW Short Press Toggle
variables:
entity:
angle:
sequence:
- condition: template
value_template: '{{ angle > 0 <= 100 }}'
- service: homeassistant.toggle
target:
entity_id: '{{ entity }}'
cube_long_cw_toggle:
description: CW Long Press Toggle
variables:
entity:
angle:
sequence:
- condition: template
value_template: '{{ angle > 100 }}'
- service: homeassistant.toggle
target:
entity_id: '{{ entity }}'
cube_short_ccw_toggle:
description: CCW Short Press Toggle
variables:
entity:
angle:
sequence:
- condition: template
value_template: '{{ angle < 0 >= -100 }}'
- service: homeassistant.toggle
target:
entity_id: '{{ entity }}'
cube_long_ccw_toggle:
description: CCW Long Press Toggle
variables:
entity:
angle:
sequence:
- condition: template
value_template: '{{ angle < -100 }}'
- service: homeassistant.toggle
target:
entity_id: '{{ entity }}'
Method to use Group 3
actions and not interfere with Group 1
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.
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 slots.
Here is the GUI editor showing this.
Here is the YAML editor showing this:
alias: Aqara Magic Cube test
description: stuff happens here
use_blueprint:
path: SirGoodenough/Zigbee2MQTT - Xiaomi Cube Controller.yaml
input:
remote: sensor.yertle_action
5_from_2:
- condition: template
value_template: '{{ action == "slide" }}'
- service: light.toggle
data: {}
target:
entity_id: light.kitchensink
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.
alias: aaaaaaa Test
description: 'See how to increase the number of Traces available''
trace:
stored_traces: 10
use_blueprint:
.....
All My Blueprints
Here is a list of each of my blueprints, a quick description and jump links to the Blueprints Exchange postā¦
Scripts
Broadlink on Script Blueprint
This is a SCRIPT Blueprint that uses my Broadlink RM3 to turn my TV on and get it into the correct mode, Pushes remote buttons in sequence.
Tasmota EZ Button Blueprint
This Script Blueprint generates 3 Buttons to help you manage your Tasmota installed base. Restart All, Update a few, and Update all.
Play Media File Script Blueprint Blueprint
This is a SCRIPT Blueprint. This provides a way to play canned media files with the big long list of YAML entries but keep the main script or automation clean.
TTS All Message Blueprint
This script can use any of the 11 integrated TTS Platforms in Home Assistant to send a message to a media player.
Automations
Auto Fan Control Blueprint
This Blueprint is for controlling a 3 speed fan based on a temperature sensor. Both fan% control & MQTT fan control versions.
Door Open TTS Cloud-Say Message Blueprint
This Blueprint is a TTS.cloud-say version of another Door Announcer I found in the HA Blueprint Exchange.
Keypad Lock or puzzle Box Tool Blueprint
This Blueprint accepts 5 actions & when done in the right order, flips an input_boolean.
Zigbee2MQTT - Xiaomi Cube Controller Blueprint
This Blueprint uses a Zigbee2MQTT built sensor to sort out the 38(+54) commands from the Xiaomi Magic Cube Remote.
Zigbee2MQTT - ZemiSmart ZM-RM02 Controller Blueprint
This Blueprint uses the Z2M (Zigbee2MQTT) imported Action sensor to sort out the 18 commands from the ZemiSmart ZM-RM02 Controller.
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
What are we Fixing Today Facebook page (Sir GoodEnough): WhatAreWeFixingToday
What are we Fixing Today Twitter Account (Sir GoodEnough): https://bit.ly/WhatAreWeFixingTodayTW
Discord Guild: (Sir_Goodenough#9683) WhatAreWeFixingToday?
If you want to support me
Buy me Coffee: Sir GoodEnough
PayPal one-off donation link: PayPal.Me
#WhatAreWeFixingToday
#SirGoodEnough