Random behaviour with Aqara cube sides

Hello guys,

recently I got an Aqara Cube T1 Pro - for some reason I thought it is a button but what the hell, I kept it and now I am trying to get the most of it. I created an automation which is dimming the lights (rotate), randomly changes their colour (shake) and restarting their initial state (hold). Fourth should do some kind of pre-set settings depending on the side of the cube facing up. I believe it is “flip_to_side” action but it seems it is more or less random behaviour.

Maybe it’s me who cannot use the cube properly but the automation when changing sides never do what it should, well in 1:6 it does :slight_smile:

Can you, please, check my automation if there is anything wrong? When checking traces it never goes to the second check which side is up but it still do something.

alias: svetlo_jidelni_stul
description: ""
triggers:
  - topic: zigbee2mqtt/tesseract
    trigger: mqtt
conditions: []
actions:
  - choose:
      - conditions:
          - condition: and
            conditions:
              - condition: state
                entity_id: light.svetlo_kuchyn_stul
                state: "on"
              - condition: state
                entity_id: light.svetlo_kuchyn_ledpasek_linka
                state: "on"
              - condition: template
                value_template: >-
                  {{ trigger.payload_json.action in ['rotate_right',
                  'rotate_left'] }}
        sequence:
          - if:
              - condition: template
                value_template: >-
                  {{ (states.light.svetlo_kuchyn_stul.attributes.brightness +
                  trigger.payload_json.action_angle | int) > 0 and
                  (states.light.svetlo_kuchyn_ledpasek_linka.attributes.brightness
                  + trigger.payload_json.action_angle | int) > 0}}
            then:
              - data:
                  brightness_step: "{{trigger.payload_json.action_angle | int}}"
                target:
                  entity_id:
                    - light.svetlo_kuchyn_stul
                    - light.svetlo_kuchyn_ledpasek_linka
                action: light.turn_on
            else:
              - data:
                  brightness_pct: 10
                target:
                  entity_id:
                    - light.svetlo_kuchyn_stul
                    - light.svetlo_kuchyn_ledpasek_linka
                action: light.turn_on
      - conditions:
          - condition: template
            value_template: "{{ trigger.payload_json.action == \"shake\" }}"
        sequence:
          - action: light.turn_on
            metadata: {}
            data:
              rgb_color:
                - "{{ range(0, 255) | random }}"
                - "{{ range(0, 255) | random }}"
                - "{{ range(0, 255) | random }}"
            target:
              entity_id:
                - light.svetlo_kuchyn_ledpasek_linka
                - light.svetlo_kuchyn_stul
        alias: nahodna_barva
      - conditions:
          - condition: template
            value_template: "{{ trigger.payload_json.action == \"hold\" }}"
            enabled: true
        sequence:
          - action: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id:
                - light.svetlo_kuchyn_ledpasek_linka
                - light.svetlo_kuchyn_stul
          - delay:
              hours: 0
              minutes: 0
              seconds: 1
          - action: light.turn_on
            metadata: {}
            data: {}
            target:
              entity_id:
                - light.svetlo_kuchyn_ledpasek_linka
                - light.svetlo_kuchyn_stul
        alias: restart
      - conditions:
          - condition: template
            value_template: "{{ trigger.payload_json.action == \"flip_to_side\" }}"
        sequence:
          - choose:
              - conditions:
                  - condition: template
                    value_template: "{{is_state('sensor.tesseract_side', '1')}}"
                sequence:
                  - action: light.turn_off
                    metadata: {}
                    data: {}
                    target:
                      entity_id:
                        - light.svetlo_kuchyn_stul
                        - light.svetlo_kuchyn_ledpasek_linka
              - conditions:
                  - condition: template
                    value_template: "{{is_state('sensor.tesseract_side', '2')}}"
                sequence:
                  - action: light.turn_on
                    metadata: {}
                    data: {}
                    target:
                      entity_id: light.svetlo_kuchyn_stul
              - conditions:
                  - condition: template
                    value_template: "{{is_state('sensor.tesseract_side', '3')}}"
                sequence:
                  - action: light.turn_on
                    metadata: {}
                    data: {}
                    target:
                      entity_id:
                        - light.svetlo_kuchyn_ledpasek_linka
                        - light.svetlo_kuchyn_stul
              - conditions:
                  - condition: template
                    value_template: "{{is_state('sensor.tesseract_side', '4')}}"
                sequence:
                  - action: light.turn_on
                    metadata: {}
                    data:
                      kelvin: 2000
                      brightness_pct: 1
                    target:
                      entity_id:
                        - light.svetlo_kuchyn_ledpasek_linka
                        - light.svetlo_kuchyn_stul
              - conditions:
                  - condition: template
                    value_template: "{{is_state('sensor.tesseract_side', '5')}}"
                sequence:
                  - action: light.turn_on
                    metadata: {}
                    data:
                      kelvin: 2000
                      brightness_pct: 100
                    target:
                      entity_id:
                        - light.svetlo_kuchyn_ledpasek_linka
                        - light.svetlo_kuchyn_stul
              - conditions:
                  - condition: template
                    value_template: "{{is_state('sensor.tesseract_side', '6')}}"
                sequence:
                  - action: light.turn_on
                    metadata: {}
                    data:
                      rgb_color:
                        - 210
                        - 32
                        - 223
                      brightness_pct: 50
                    target:
                      entity_id:
                        - light.svetlo_kuchyn_ledpasek_linka
                        - light.svetlo_kuchyn_stul
mode: single

Hi byt,

Looking good generally but I see one problem right away. Look at the text that you pasted. Look where it changes from (on mine) white text mixed with red text, then all of a sudden the rest of it is red text. Atthat point you have the first (There are a bunch) of instances of double quotes in double quotes. Maybe excaping them does what you want, but since I’ve necer done it that way I amd not going to recommend that method.
If I have to have double quotes inside the template string, I will single quote the outside of the string OR I will use the YAML literal or folded style to not have to use the outside quotes at all.

Aside from that, I have blueprints that you are weldome to use, or take control of or be insired by for that cube. GitHub - SirGoodenough/HA_Blueprints: 🧯 My Collection of Automation and Script Blueprints for Home Assistant 🧯,

@Sir_Goodenough hello,

I did not notice that here. In yaml mode inside HA it is looking fine. That “shake” and other commands I wrote via UI so it put double qoutes around the template itself.

I rewrote it a bit, with your reccomendation about style, but surprisingly it was not the issue. It was really me. I was too gentle on the cube. I tried both codes the original one and the new one and the result is same. Actually the original one seems a bit faster as there is {{is_state('sensor.tesseract_side', '6')}} and in the rewritten I put {{states('sensor.tesseract_side') == '1' }} - not sure if it just coincidence or it really has some “visible” impact. Anyway being a bit more harsh on the cube when changing sides is the solution :smiley:

Thank you for your reply anyway, I appreciate your time and advice.

Yes, no need to be gently with the cube, it to needs be clicked hard against surfaces. Have some help with that in the post (I linked to above) for the blueprints I publish.