Aqara Cube ZHA - Simplified face-based device control

Ive reported an issue for the face reporting on knock events.

1 Like

Hi @EdwardTFN

I can confirm the bug in the Aqara Cube ZHA integration and just now submitted diagnostic info to the code author on the issue reported by @mouth4war.

For the meantime, I’ve updated your blueprint to use only flip event which reports the correct side to store the input_helper.

activated_face: '{{ iif((command == ''flip''), trigger.event.data.args.activated_face, states(temp_last_face_input)) | int(0) }}'

This seems to temporarily resolve the issue as it can still use the active face number from the last flip event.

An advanced code may be written to invert the code for knock/slide reported values to increase the update frequency of the input_helper.

Even before I try using this blueprint, I have to appreciate the effort you made in writing a great description! It has links to similar blueprints that inspired yours (and that might be better suited for some use-cases), it has a clear example on how you use your device (and thus what to expect from the blueprint), and it has clear instructions with screenshots on how to setup the helper entity, it even has a changelog! It’s only missing a screenshot of the blueprint itself, but I’m not complaining, because you did beyond what most people do. And I recognize this effort, as you made it easier for new users to understand the required steps. Congratulations!

2 Likes

Thanks for your kind feedback, @denilsonsa.

I did one modification to your blueprint: I’ve added a “neutral” rotation action. This can be very powerful together with {{ relative_degrees }}. In my case, I set one face to change the brightness, and the other to change the color temperature. Like this:

- id: '1234567890123'
  alias: Remote - Aqara Cube - Living room
  description: ''
  use_blueprint:
    path: EdwardTFN/aqara-cube-zha-simplified-face-based-device-control-remix.yaml
    input:
      last_face_input: input_number.aqara_cube_1_last_face
      cube: 6ce360c004dda1bf7f25e54e543aa1c5
      face_1_rotate:
      - service: light.turn_on
        data:
          brightness: '{{ (state_attr(''light.living_room'', ''brightness'') or 0)
            + (relative_degrees * 255 / 360 / 2) }}'
        target:
          entity_id: light.living_room
      face_2_rotate:
      - service: light.turn_on
        data:
          color_temp: "{{ min(\n     max(\n       (\n         (state_attr('light.living_room',
            'color_temp') or 0)\n         + (relative_degrees * 200 / 360)\n       ),\n
            \      state_attr('light.living_room', 'min_mireds') or 0\n     ),\n     state_attr('light.living_room',
            'max_mireds') or 1000\n   )\n}}"
        target:
          entity_id: light.living_room

This means subtle rotations will change less than longer rotations. And since {{ relative_degrees }} can be positive or negative, I don’t need separate actions for left/right rotations.

As my next step, I should move all that logic (to increase/decrease brightness or color_temp) into a script, to make it easier to call. Maybe even a blueprint of a script.


Here are my changes, feel free to incorporate into your blueprint.

EDIT! These changes are incorrect! By applying these changes, the “neutral” rotation is called but the specific direction actions are never called. That’s my mistake because I didn’t realize the whole block was inside a choose. Oops.

--- aqara-cube-zha-simplified-face-based-device-control.yaml
+++ aqara-cube-zha-simplified-face-based-device-control-remix.yaml
@@ -1,6 +1,13 @@
 blueprint:
-  name: Aqara Cube face-based
-  description: Control devices based on the cube's face.
+  name: Aqara Cube face-based (remix)
+  description: |-
+    Control devices based on the cube's face.
+
+    The following variables are available in the templates:
+    * `{{ relative_degrees }}`: Negative number for `rotate_left` and positive number for `rotate_right`.
+    * `{{ flip_degrees }}`: Either `90` or `180` for `flip` commands, or `0` for other commands.
+    * `{{ command }}`: The action being performed with the cube. You probably don't need this in your template.
+    * `{{ activated_face }}`: The current cube face, or the last known face. You probably don't need this in your
 template.
   domain: automation
   input:
     last_face_input:
@@ -32,6 +39,13 @@
       default: []
       selector:
         action: {}
+    face_1_rotate:
+      name: Rotate (any direction) face 1
+      description: Actions to execute when the cube rotates in any direction with
+        face 1 in the top.
+      default: []
+      selector:
+        action: {}
     face_1_rotate_counter_clockwise:
       name: Rotate counter-clockwise face 1
       description: Actions to execute when the cube rotates counter-clockwise with
@@ -65,6 +79,13 @@
       default: []
       selector:
         action: {}
+    face_2_rotate:
+      name: Rotate (any direction) face 2
+      description: Actions to execute when the cube rotates in any direction with
+        face 2 in the top.
+      default: []
+      selector:
+        action: {}
     face_2_rotate_counter_clockwise:
       name: Rotate counter-clockwise face 2
       description: Actions to execute when the cube rotates counter-clockwise with
@@ -98,6 +119,13 @@
       default: []
       selector:
         action: {}
+    face_3_rotate:
+      name: Rotate (any direction) face 3
+      description: Actions to execute when the cube rotates in any direction with
+        face 3 in the top.
+      default: []
+      selector:
+        action: {}
     face_3_rotate_counter_clockwise:
       name: Rotate counter-clockwise face 3
       description: Actions to execute when the cube rotates counter-clockwise with
@@ -131,6 +159,13 @@
       default: []
       selector:
         action: {}
+    face_4_rotate:
+      name: Rotate (any direction) face 4
+      description: Actions to execute when the cube rotates in any direction with
+        face 4 in the top.
+      default: []
+      selector:
+        action: {}
     face_4_rotate_counter_clockwise:
       name: Rotate counter-clockwise face 4
       description: Actions to execute when the cube rotates counter-clockwise with
@@ -164,6 +199,13 @@
       default: []
       selector:
         action: {}
+    face_5_rotate:
+      name: Rotate (any direction) face 5
+      description: Actions to execute when the cube rotates in any direction with
+        face 5 in the top.
+      default: []
+      selector:
+        action: {}
     face_5_rotate_counter_clockwise:
       name: Rotate counter-clockwise face 5
       description: Actions to execute when the cube rotates counter-clockwise with
@@ -197,6 +239,13 @@
       default: []
       selector:
         action: {}
+    face_6_rotate:
+      name: Rotate (any direction) face 6
+      description: Actions to execute when the cube rotates in any direction with
+        face 6 in the top.
+      default: []
+      selector:
+        action: {}
     face_6_rotate_counter_clockwise:
       name: Rotate counter-clockwise face 6
       description: Actions to execute when the cube rotates counter-clockwise with
@@ -252,6 +301,9 @@
         - '{{ command == ''knock'' }}'
         sequence: !input face_1_knock
       - conditions:
+        - '{{ command == ''rotate_left'' or command == ''rotate_right'' }}'
+        sequence: !input face_1_rotate
+      - conditions:
         - '{{ command == ''rotate_left'' }}'
         sequence: !input face_1_rotate_counter_clockwise
       - conditions:
@@ -272,6 +324,9 @@
         - '{{ command == ''knock'' }}'
         sequence: !input face_2_knock
       - conditions:
+        - '{{ command == ''rotate_left'' or command == ''rotate_right'' }}'
+        sequence: !input face_2_rotate
+      - conditions:
         - '{{ command == ''rotate_left'' }}'
         sequence: !input face_2_rotate_counter_clockwise
       - conditions:
@@ -292,6 +347,9 @@
         - '{{ command == ''knock'' }}'
         sequence: !input face_3_knock
       - conditions:
+        - '{{ command == ''rotate_left'' or command == ''rotate_right'' }}'
+        sequence: !input face_3_rotate
+      - conditions:
         - '{{ command == ''rotate_left'' }}'
         sequence: !input face_3_rotate_counter_clockwise
       - conditions:
@@ -312,6 +370,9 @@
         - '{{ command == ''knock'' }}'
         sequence: !input face_4_knock
       - conditions:
+        - '{{ command == ''rotate_left'' or command == ''rotate_right'' }}'
+        sequence: !input face_4_rotate
+      - conditions:
         - '{{ command == ''rotate_left'' }}'
         sequence: !input face_4_rotate_counter_clockwise
       - conditions:
@@ -332,6 +393,9 @@
         - '{{ command == ''knock'' }}'
         sequence: !input face_5_knock
       - conditions:
+        - '{{ command == ''rotate_left'' or command == ''rotate_right'' }}'
+        sequence: !input face_5_rotate
+      - conditions:
         - '{{ command == ''rotate_left'' }}'
         sequence: !input face_5_rotate_counter_clockwise
       - conditions:
@@ -351,6 +415,9 @@
       - conditions:
         - '{{ command == ''knock'' }}'
         sequence: !input face_6_knock
+      - conditions:
+        - '{{ command == ''rotate_left'' or command == ''rotate_right'' }}'
+        sequence: !input face_6_rotate
       - conditions:
         - '{{ command == ''rotate_left'' }}'
         sequence: !input face_6_rotate_counter_clockwise

Or you could adapt this Dimmer to ZHA…

or this color change…

And use those scripts.

Just have to get the triggered angle out to the script.

Thank you very much for this awesome blue-print. So far Ive managed to connect 3 lightgroups to 3 different sides and controlling scenes on them by sliding. Managing saturation by turning clockwise/counterclockwise and toggling on and off.

Unfortunately Ive been unable to add my favorite radio station towards one side.

What I would like is on side X tapping it would toggle play/stop my favorite radio channel. While turning would adjust volume and perhaps if its even possible let slide swap between a set of like 3 music channels.

Anyone know how to realise this? Thank you in advance!

What kind of element you use to control your radio? Which integration?

Im a big rookie to HA. Im not sure what exactly you are asking. Ive used 3 scenes to toggle between radio channels within HA itself. And I use " radio browser" and a lovelace card to control.

If there is an easier way to get it connected to the cube im all ears. I havent figured it out yet.

You have answered my question… Your integration is Radio Browser.

I will take a look and come back to you…

Radio Browser will be playing in a Media Player entity, so you can use services media_player.toggle, media_player.volume_up and media_player.volume_down in other to control it.

Something like this:

thnx for your assistance. I managed to turn on/off power by tapping the cube.
volume regulation by rotating clock/counter
swapping between channels by toggling between scripts of each radio channel with the swipe action.

Yet im not able to toggle the power AND resume the last channel it was playing.

EDIT I have managed to let it start with a radio channel allready playing. Yet the toggling between the different channels isnt going as smoothly. it either just randomly selects another channel or even the same one. I tried to create a helper/scene where it should run the 3 different scripts for the channels but it fails to start the script on activating the scene.

Don’t you wanna share some of your code so we can take a look and try to get ideas?

Can anyone confirm whether or not this blueprint will also fully work with the new Aqara Cube T1 Pro?

1 Like

I don’t have the new cube, so I would appreciate if someone try the Blueprint with the new cube and come back with some.feedback.
It will be a bit hard for me to change the Blueprint wit out having the cube with me, but maybe with a couple of info from your side I can put it to work.

Thanks for your reply and willing to look into it if someone provides some data/feedback.

Can’t easily order it on Amazon in my country yet, but as soon as it comes in there I might order it and give it a try with your blueprint. For now I’ve ordered the old cube and give that a try first.

I have the new cube, the T1 PRO is what you mean right?
I have tried connected the cube to HA using Aqara Hub and also through ZHA.

Through aqara hub only face changes are recognised
And through ZHA I am only getting events for rotate and shake

Therefore you would not yet be able to utilize the full functionality of the blueprint with the new cube

See the following issue:
Zigbee Home Automation (ZHA) - Aqara T1 Pro Cube - Only Shake and Rotate Events Triggering · Issue #84301 · home-assistant/core (github.com)

1 Like

Thanks for sharing, @althan.

I’ve managed to get it working now with this fix.

1 Like

Hi, I’m using your blueprint and it is fantastic! I’m trying to trigger an automation with a condition tapping the cube, but it doesn’t work. I’m quite sure it is my fault, but I can’t find a solution. Thanks in advance for any help

alias: Cinema ON
description: Predispone la stanza per guardare il proiettore
trigger: []
condition:
  - condition: device
    device_id: ccacf2caf9f620ad7d9a3188184ae33d
    domain: media_player
    entity_id: media_player.denon_avr_x3100w
    type: is_off
action:
  - service: media_player.turn_on
    data: {}
    target:
      entity_id: media_player.telecomando_jvc
  - service: media_player.turn_on
    data: {}
    target:
      device_id: aeb8570f8bd33d8d211583bfcb230583
  - service: light.turn_on
    data:
      transition: 0
      rgb_color:
        - 0
        - 255
        - 255
      brightness_pct: 25
    target:
      device_id: 8dc8effc7091a9739d018cc870eb4845
  - service: media_player.turn_on
    data: {}
    target:
      device_id: ccacf2caf9f620ad7d9a3188184ae33d
  - service: switch.turn_off
    data: {}
    target:
      device_id: 4cadbb0719870b5e4915cf64fbad075f
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - service: media_player.select_source
    data:
      source: Media Player
    target:
      device_id: ccacf2caf9f620ad7d9a3188184ae33d
  - delay:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    data:
      transition: 0
    target:
      area_id: soggiorno
mode: single