Aqara Cube in zigbee2mqtt addon

Hi,

I wanted to get rid of the Aqara Control gateway and so I bought a cc2531 zigbee sniffer.

My doors sensors work fine already.

But I have some problems with the Cube.

First it is not autodetected like the other senors, so I had to implement this:

- sensor:
  - platform: "mqtt"
    name: Cube state
    state_topic: "zigbee2mqtt/0x00XXXXXXXX"
    availability_topic: "zigbee2mqtt/bridge/state"
    icon: "mdi:gesture-double-tap"
    value_template: "{{ value_json.action }}"
    json_attributes: 
      - "battery"
      - "voltage"
      - "angle"
      - "side"
      - "from_side"
      - "to_side"
      - "brightness"
      - "angle_x_absolute"
      - "angle_y_absolute"
      - "angle_z"
      - "angle_y"
      - "angle_x"
      - "unknown_data"
    force_update: true

This is an example from here https://github.com/Koenkk/zigbee2mqtt/wiki/Integrating-with-Home-Assistant

Before I had a working automation like this:

- alias: Cube - flip90
  initial_state: 'on'
  trigger:
    platform: event
    event_type: cube_action
    event_data:
      entity_id: binary_sensor.cube_XXXXXXX
      action_type: flip90
  action:
    - service: homeassistant.toggle
      entity_id: light.flaschen

With the new mqtt sensor I tried this:

- alias: Cube - flip90
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: sensor.cube_state
    to: 'flip90'
  action:
    - service: homeassistant.toggle
      entity_id: light.flaschen

The problem is that toggling the light will not work, because the state does not change. So if flip90 changes to flip90 again the automation will not do anything.

How can I make this work?

The next thing that does not work is using the rotation as a dimmer function
this one worked before, but I have no clue how to change it to make it work with the mqtt sensor.

- alias: Cube - rotate
  initial_state: 'on'
  trigger:
    - platform: event
      event_type: cube_action
      event_data:
        entity_id: binary_sensor.cube_XXXXXXX
        action_type: rotate
  action:         
    - service: light.turn_on
      data:
        entity_id: light.kugellicht
        color_name: darkturquoise
    - service: light.turn_on
      data_template:
        entity_id: light.kugellicht
        brightness: >-
          {% if trigger.event.data.action_value | float > 0 %}
          {{  states.light.kugellicht.attributes.brightness | int + 30 }}
          {% else %}
          {{  states.light.kugellicht.attributes.brightness | int - 30 }}
          {% endif %}
1 Like

Have a look at this (https://koenkk.github.io/zigbee2mqtt/integration/home_assistant.html)

You have to change the automation:

automation:
  - alias: Respond to Cube action
    trigger:
      platform: mqtt
      topic: 'zigbee2mqtt/<FRIENDLY_NAME'
    condition:
      condition: template
      value_template: '{{ "tap" == trigger.payload_json.action }}'
    action:
      entity_id: light.bedroom
      service: light.toggle

Tap can also be flip/slide/etc.

2 Likes

Thanks, already found the solution here and also added my examples at the end.

This is my first post opver here and I am playing with automations.
I made some tests and the best automation for this was following:

First version:
This automation is doing the job, but it does not prevent that the values for brightnes are set below 1 and above 255

  • alias: Cube - rotacion
    initial_state: ‘on’
    trigger:
    platform: mqtt
    topic: ‘zigbee2mqtt/Magic_Cube_Xiaomi’
    condition:
    condition: template
    value_template: “{{ trigger.payload_json.action in [‘rotate_right’, ‘rotate_left’] }}”
    action:
    • service: light.turn_on
      data:
      entity_id: light.luz_habitacion_pequena
    • service: light.turn_on
      data_template:
      entity_id: light.luz_habitacion_pequena
      brightness: “{{ (states.light.luz_habitacion_pequena.attributes.brightness | int) + (trigger.payload_json.angle | int) }}”

Second Version:
I added extra conditions to make sure that min value is 1 and maximum value is 255:

  • alias: Cube - rotacion
    initial_state: ‘on’
    trigger:
    platform: mqtt
    topic: ‘zigbee2mqtt/Magic_Cube_Xiaomi’
    condition:
    condition: template
    value_template: “{{ trigger.payload_json.action in [‘rotate_right’, ‘rotate_left’] }}”
    action:
    • service: light.turn_on
      data:
      entity_id: light.luz_habitacion_pequena
    • service: light.turn_on
      data_template:
      entity_id: light.luz_habitacion_pequena
      brightness: >-
      {% if (states.light.luz_habitacion_pequena.attributes.brightness | int) + (trigger.payload_json.angle | int) < 0 %}
      1
      {% elif (states.light.luz_habitacion_pequena.attributes.brightness | int) + (trigger.payload_json.angle | int) > 255 %}
      255
      {% else %}
      {{ states.light.luz_habitacion_pequena.attributes.brightness | int + (trigger.payload_json.angle | int) }}
      {% endif %}

Sorry I could not indent the lines, rooky mistakes.

hope this is usefull for you, like all the posts over here helped me a lot.

Cheers

Sergio

Are there any easy tutorial how i can add some working actions with cc2531 and the cube? I have succesfully added to HA but how can i add actions? There is a blueprints for decons but not for cc2531.