Filter out trigger values. Rotate xiaomi cube

Okey.
I have an xiaomi cube controlling my volume and other things.
Here is the automation.

 - alias: Spin the cube
   trigger:
     platform: event
     event_type: deconz_event
     event_data:
       id: rosa_kub_1


   action:
    -  service: media_player.volume_set
       entity_id: media_player.vaggen
       data_template:
         volume_level: >
           {% if trigger.event.data.event | float > 0 %}
             {{ states.media_player.vaggen.attributes.volume_level + 0.05 }}
           {% else %}
             {{ states.media_player.vaggen.attributes.volume_level - 0.05 }}
           {% endif %}

It is working great. The automation check if i get a positive value or a negative value (depending of right/left turn of the cube).
BUT! I also use the cube for play/pause
All other commands from the cube is positive values.
Here is a list:

These are the event ID’s available for each move you can do with the cube.

x000 for push, so 1000, 2000, 3000, 4000, 5000, 6000;
x00x for double tap, so 1001, 2002, 3003, 4004, 5005, 6006;
x00o for 180° flip (where o = 7 - x), so 1006, 2005, 3004, 4003, 5002, 6001;
x00y for 90° flip (where y ≠ x and y ≠ 7 - x), so 1002, 1003, 1004, 1005, 2001, 2003, 2004, 2006, 3001,
3002, 3005, 3006, 4001, 4002, 4005, 4006, 5001, 5003, 5004, 5006, 6002, 6003, 6004, 6005;
7007 for shake.
7000 for wakeup (the cube sends raw value 2 when I just tap it after a while).

All those commands is 00 in the middle.
Using wildcards in some conditions maybe should work? But HomeAssistant doesn’t support windcard in that way, right?
Is there any way to prevent my automation to trigger if i get any of these values from the cube?

This is some of the values i get when i rotate the cube:

2018-05-30 21:44:59 DEBUG (MainThread) [pydeconz.websocket] Websocket data: b'\x81t{"e":"changed","id":"5","r":"sensors","state":{"buttonevent":-3364,"lastupdated":"2018-05-30T19:44:59"},"t":"event"}'
2018-05-30 21:44:59 DEBUG (MainThread) [pydeconz.deconzdevice] Rosa kub 1: update lastupdated with 2018-05-30T19:44:59
2018-05-30 21:44:59 DEBUG (MainThread) [pydeconz.deconzdevice] Rosa kub 1: update buttonevent with -3364
2018-05-30 21:44:59 INFO (MainThread) [homeassistant.core] Bus:Handling <Event deconz_event[R]: id=rosa_kub_1, event=-3364>
2018-05-30 21:45:01 DEBUG (MainThread) [pydeconz.websocket] Websocket data: b'\x81t{"e":"changed","id":"5","r":"sensors","state":{"buttonevent":-2182,"lastupdated":"2018-05-30T19:45:01"},"t":"event"}'
2018-05-30 21:45:01 DEBUG (MainThread) [pydeconz.deconzdevice] Rosa kub 1: update lastupdated with 2018-05-30T19:45:01
2018-05-30 21:45:01 DEBUG (MainThread) [pydeconz.deconzdevice] Rosa kub 1: update buttonevent with -2182
2018-05-30 21:45:01 INFO (MainThread) [homeassistant.core] Bus:Handling <Event deconz_event[R]: id=rosa_kub_1, event=-2182>
2018-05-30 21:45:02 DEBUG (MainThread) [pydeconz.websocket] Websocket data: b'\x81t{"e":"changed","id":"5","r":"sensors","state":{"buttonevent":-3364,"lastupdated":"2018-05-30T19:45:02"},"t":"event"}'
2018-05-30 21:45:02 DEBUG (MainThread) [pydeconz.deconzdevice] Rosa kub 1: update lastupdated with 2018-05-30T19:45:02
2018-05-30 21:45:02 DEBUG (MainThread) [pydeconz.deconzdevice] Rosa kub 1: update buttonevent with -3364
2018-05-30 21:45:02 INFO (MainThread) [homeassistant.core] Bus:Handling <Event deconz_event[R]: id=rosa_kub_1, event=-3364>
2018-05-30 21:45:02 DEBUG (MainThread) [pydeconz.websocket] Websocket data: b'\x81t{"e":"changed","id":"5","r":"sensors","state":{"buttonevent":-2845,"lastupdated":"2018-05-30T19:45:02"},"t":"event"}'
2018-05-30 21:45:02 DEBUG (MainThread) [pydeconz.deconzdevice] Rosa kub 1: update lastupdated with 2018-05-30T19:45:02
2018-05-30 21:45:02 DEBUG (MainThread) [pydeconz.deconzdevice] Rosa kub 1: update buttonevent with -2845
2018-05-30 21:45:02 INFO (MainThread) [homeassistant.core] Bus:Handling <Event deconz_event[R]: id=rosa_kub_1, event=-2845>
2018-05-30 21:45:03 DEBUG (MainThread) [pydeconz.websocket] Websocket data: b'\x81t{"e":"changed","id":"5","r":"sensors","state":{"buttonevent":-8635,"lastupdated":"2018-05-30T19:45:03"},"t":"event"}'
2018-05-30 21:45:03 DEBUG (MainThread) [pydeconz.deconzdevice] Rosa kub 1: update lastupdated with 2018-05-30T19:45:03
2018-05-30 21:45:03 DEBUG (MainThread) [pydeconz.deconzdevice] Rosa kub 1: update buttonevent with -8635
2018-05-30 21:45:03 INFO (MainThread) [homeassistant.core] Bus:Handling <Event deconz_event[R]: id=rosa_kub_1, event=-8635>
2018-05-30 21:45:04 DEBUG (MainThread) [pydeconz.websocket] Websocket data: b'\x81s{"e":"changed","id":"5","r":"sensors","state":{"buttonevent":2510,"lastupdated":"2018-05-30T19:45:04"},"t":"event"}'
2018-05-30 21:45:04 DEBUG (MainThread) [pydeconz.deconzdevice] Rosa kub 1: update lastupdated with 2018-05-30T19:45:04
2018-05-30 21:45:04 DEBUG (MainThread) [pydeconz.deconzdevice] Rosa kub 1: update buttonevent with 2510
2018-05-30 21:45:04 INFO (MainThread) [homeassistant.core] Bus:Handling <Event deconz_event[R]: id=rosa_kub_1, event=2510>
2018-05-30 21:45:05 DEBUG (MainThread) [pydeconz.websocket] Websocket data: b'\x81s{"e":"changed","id":"5","r":"sensors","state":{"buttonevent":6831,"lastupdated":"2018-05-30T19:45:05"},"t":"event"}'
2018-05-30 21:45:05 DEBUG (MainThread) [pydeconz.deconzdevice] Rosa kub 1: update lastupdated with 2018-05-30T19:45:05
2018-05-30 21:45:05 DEBUG (MainThread) [pydeconz.deconzdevice] Rosa kub 1: update buttonevent with 6831
2018-05-30 21:45:05 INFO (MainThread) [homeassistant.core] Bus:Handling <Event deconz_event[R]: id=rosa_kub_1, event=6831>
2018-05-30 21:45:06 DEBUG (MainThread) [pydeconz.websocket] Websocket data: b'\x81s{"e":"changed","id":"5","r":"sensors","state":{"buttonevent":3104,"lastupdated":"2018-05-30T19:45:06"},"t":"event"}'
2018-05-30 21:45:06 DEBUG (MainThread) [pydeconz.deconzdevice] Rosa kub 1: update lastupdated with 2018-05-30T19:45:06
2018-05-30 21:45:06 DEBUG (MainThread) [pydeconz.deconzdevice] Rosa kub 1: update buttonevent with 3104
2018-05-30 21:45:06 INFO (MainThread) [homeassistant.core] Bus:Handling <Event deconz_event[R]: id=rosa_kub_1, event=3104>
2018-05-30 21:45:07 DEBUG (MainThread) [pydeconz.websocket] Websocket data: b'\x81s{"e":"changed","id":"5","r":"sensors","state":{"buttonevent":8275,"lastupdated":"2018-05-30T19:45:07"},"t":"event"}'
2018-05-30 21:45:07 DEBUG (MainThread) [pydeconz.deconzdevice] Rosa kub 1: update lastupdated with 2018-05-30T19:45:07
2018-05-30 21:45:07 DEBUG (MainThread) [pydeconz.deconzdevice] Rosa kub 1: update buttonevent with 8275
2018-05-30 21:45:07 INFO (MainThread) [homeassistant.core] Bus:Handling <Event deconz_event[R]: id=rosa_kub_1, event=8275>
2018-05-30 21:45:07 DEBUG (MainThread) [pydeconz.websocket] Websocket data: b'\x81s{"e":"changed","id":"5","r":"sensors","state":{"buttonevent":2931,"lastupdated":"2018-05-30T19:45:07"},"t":"event"}'
2018-05-30 21:45:07 DEBUG (MainThread) [pydeconz.deconzdevice] Rosa kub 1: update lastupdated with 2018-05-30T19:45:07
2018-05-30 21:45:07 DEBUG (MainThread) [pydeconz.deconzdevice] Rosa kub 1: update buttonevent with 2931
2018-05-30 21:45:07 INFO (MainThread) [homeassistant.core] Bus:Handling <Event deconz_event[R]: id=rosa_kub_1, event=2931>
2018-05-30 21:45:08 DEBUG (MainThread) [pydeconz.websocket] Websocket data: b'\x81s{"e":"changed","id":"5","r":"sensors","state":{"buttonevent":2286,"lastupdated":"2018-05-30T19:45:08"},"t":"event"}'
2018-05-30 21:45:08 DEBUG (MainThread) [pydeconz.deconzdevice] Rosa kub 1: update lastupdated with 2018-05-30T19:45:08
2018-05-30 21:45:08 DEBUG (MainThread) [pydeconz.deconzdevice] Rosa kub 1: update buttonevent with 2286
2018-05-30 21:45:08 INFO (MainThread) [homeassistant.core] Bus:Handling <Event deconz_event[R]: id=rosa_kub_1, event=2286>

I don’t know how many comibations there is.
Or is it possible to prevent the automation to trigger if some of my other automations trigger at the same time?
I tried to put a delay on this automation and in my other automation triggering the I play/pause use homeassistant.turn_off and turn this one off but it triggers once anyway before it get turned off.

It sure does. Check out the regex_match and regex_search filters documented here.

I checked github. It was added in 0.67? right? Or is it pssible to use for me? I am on 0.64.3. =/
And i searched the forum to find some examples how to use it. With my knowlege it is very hard to understand.

Then I’m afraid it’s time to update (well, it’s long past time to update).

Unfortunately you’ve got a dozen major releases between you and the current version (0.76 at the time I write this). There’s a summary of all the breaking changes here, but you’ll want to review each blog post.

Yeah, I know. But I am still on python 3.4.* and Ubuntu 16.04 and haven’t had the time to fix this. Thinking of moving over to docker but haven’t figured out how to use Let’s Encrypt…
Meanwhile, if someone want to suggest how to use regex in this case, I would be very happy.

Assuming the event ID is always x00y, then you could do something like:

{% set x = (trigger.event.data.event|string)[0] %}
{% set y = (trigger.event.data.event|string)[-1] %}

And then you can make decisions based on the extracted x & y values.

But I think you at least implied that the event code could also be negative, so those statements would probably need some further refinement, but maybe this is a direction you can go in.

It is the first and last number in the event ID I want to ignore and only check for the middle numbers, if they are 00 the automation shouldn’t do anything.

Should i replace {% if trigger.event.data.event | float > 0 %} with {% set x = (trigger.event.data.event|string)[0] %} ?

This doesn’t use regex, right? Is it doable with 0.64? Sorry, I really have problem understand this… :roll_eyes:

Ok, it wasn’t cleat at first what you were trying to do differently than your original automation. If I understand, what you have is fine, except you want to do nothing if the middle two digits are zero. Is that correct?

If so, add this condition to your existing automation.

  condition:
    condition: template
    value_template: >
      {% set x = trigger.event.data.event|string %}
      {{ x|length != 4 or x[1:3] != '00' }}

This will let the automation action run if either the event ID, as a string, is not exactly four digits, or if it is four digits but the two middle digits are not both zeros.

1 Like

Man, you are awesome.
Thank you.
Now i can use all other commands from the cube without this automation is triggered.
It only trigger when i turn the cube.
Thank you so much…

1 Like

I post the automation here if someone want to use the Xiaomi cube in combination with Deconz and control something rotating the cube.

 - alias: Rotate Xiaomi Cube
   trigger:
     platform: event
     event_type: deconz_event
     event_data:
       id: id_of_the_cube

   condition:
     condition: template
     value_template: >
       {% set x = trigger.event.data.event|string %}
       {{ x|length != 4 or x[1:3] != '00' }}


   action:
     service: media_player.volume_set
     entity_id: media_player.vaggen
     data_template:
       volume_level: >
         {% if trigger.event.data.event | float > 0 %}
           {{ states.media_player.vaggen.attributes.volume_level + 0.05 }}
         {% else %}
           {{ states.media_player.vaggen.attributes.volume_level - 0.05 }}
         {% endif %}
3 Likes

Hey @xydix, I used your template to do the same automation but it never triggers. I can get the event, I see all other events but this automation never triggers. Did you change anything or do you know a reason?

Hi.
No i didn’t change anything. I use that code posted.
Have you tried to remove the condition, just for test?
I am still on version 0.64.3. Don’t know if there have been any breaking changes.

Maybe you can add an event to it to check if it triggers, just for test.

Without the condition it fires everytime I use the cube except when rotating. Weird. With the condition it never triggers. But I can get the the event as a 4 digit number (and a - for negative rotation).I used 78 and now even 79b. But I don’t think this should change anything.

When you use debug, does it look like my log? If I remember right, the newer cubes is the aquara logo on. Don’t know if there is any difference between these.
I have the older one with the MI logo.

hi all
waking up this thread again…
i am getting all eventIDs … but i am not getting ANYthing for a spin… what action is this exactly?. and how does it show up int he logs_

Please share your automation.

first things first :)… i found out why the events for spinning werent showing up… so now i am going to make the automations

Just wanted to contribute my automation for volume up & down based on cube rotation. I added a few elif’s to adjust the volume based on how far the cube is rotated.
I have the new Aqara cube, running Deconz. I used Phil’s addition as well.

- id: cube volume adjust
  alias: cube_volume_adjust
  trigger:
    platform: event
    event_type: deconz_event
    event_data:
      id: cube
  condition:
  #this filters out the cube action if the middle two numbers are 0
    condition: template
    value_template: >
      {% set x = trigger.event.data.event|string %}
      {{ x|length != 4 or x[1:3] != '00' }}      
  action:
    - service: media_player.volume_set
      entity_id: media_player.red_room_speaker
      data_template:
        volume_level: >
          {% if (trigger.event.data.event > 0) and (trigger.event.data.event | int < 2500) %} 
            {{ states.media_player.red_room_speaker.attributes.volume_level | float + 0.1 }} 
          {% elif (trigger.event.data.event < 0) and (trigger.event.data.event | int > -2500) %} 
            {{ states.media_player.red_room_speaker.attributes.volume_level | float - 0.1 }}          
          {% elif (trigger.event.data.event > 2501) and (trigger.event.data.event < 4000) %} 
            {{ states.media_player.red_room_speaker.attributes.volume_level | float + 0.15 }} 
          {% elif (trigger.event.data.event < -2501) and (trigger.event.data.event | int > -4000) %} 
            {{ states.media_player.red_room_speaker.attributes.volume_level | float - 0.15 }}             
          {% elif (trigger.event.data.event > 4001) and (trigger.event.data.event < 7000) %} 
            {{ states.media_player.red_room_speaker.attributes.volume_level | float + 0.2 }} 
          {% elif (trigger.event.data.event < -4001) and (trigger.event.data.event | int > -7000) %} 
            {{ states.media_player.red_room_speaker.attributes.volume_level | float - 0.2 }}              
          {% elif (trigger.event.data.event > 7001) and (trigger.event.data.event < 110000) %} 
            {{ states.media_player.red_room_speaker.attributes.volume_level | float + 0.3 }} 
          {% elif (trigger.event.data.event < -7001) and (trigger.event.data.event | int > -110001) %} 
            {{ states.media_player.red_room_speaker.attributes.volume_level | float - 0.3 }}                    
          {% elif trigger.event.data.event > 110001 %} 
            {{ states.media_player.red_room_speaker.attributes.volume_level | float + 0.5 }}
          {% elif trigger.event.data.event < -110001 %} 
            {{ states.media_player.red_room_speaker.attributes.volume_level | float - 0.5 }}               
          {% else %}
            {{ states.media_player.red_room_speaker.attributes.volume_level | float - 0.01 }} 
          {% endif %}

just thought i’ld paste my cube config here in case anyone is interested; i use it to control 6 sets of different lights. each face maps to a specific light through the use of an input_number. double tapping (dropping) the cube will toggle the light, whilst rotating on a face will brighten or dim that specific light.

- alias: Cube01 Log State
  initial_state: 'on'
  trigger:
    platform: event
    event_type: deconz_event
    event_data:
      id: cube01
  condition:
    condition: template
    value_template: >
      {% set evt = trigger.event.data.event|string %}
      {{ evt[-3:-1] == "00" }}
  action:
  - service: input_number.set_value
    data_template:
      entity_id: input_number.cube01
      value: >
        {% set evt = trigger.event.data.event|string %}
        {{ evt[0] }}

- alias: Cube01 Dining Room Light Toggle
  initial_state: 'on'
  trigger:
    platform: event
    event_type: deconz_event
    event_data:
      id: cube01
  condition:
    condition: template
    value_template: >
      {{ states('input_number.cube01')|int == 6 }}
  action:
  - service: light.toggle
    entity_id: light.dining_room

- alias: Cube01 Dining Room Lights Rotate
  initial_state: 'on'
  trigger:
    platform: event
    event_type: deconz_event
    event_data:
      id: cube01
  condition:
    condition: and
    conditions:
    - condition: template
      value_template: >
        {% set evt = trigger.event.data.event|string %}
        {{ evt[-3:-1] != "00"  and  states('input_number.cube01')|int == 6 }}
  action:
  - service: light.turn_on
    data_template:
      entity_id: light.dining_room
      brightness: >
        {% set deg = trigger.event.data.event/100 %}
        {{ (state_attr('light.dining_room', 'brightness') + deg) | int }}

it does have the disadvantage where it might set the face incorrectly where rotations have have a ‘0.0’ degree in them (eg 170.02), but… shrug, unlikely to happen.

there is also a significant amount of not-Do-not-Repeat-Yourself for each face, but the combination of needing the entity_id in both the data_template for the entity_id and and the items current brightness makes it a bit awkward.

1 Like

Thanx, got some info of your automation that I’ve searched for all day.
Trying to do the same thing with the cube - 6 sides = 6 rgb lights(groups) but trying to include options for (not only brightness for rotation) hue and saturation as well hs_color.
Maybee a quick 90 deg. flipp right and back again to input_select brightness, flipp 90 deg. “away from you” to select hue and flipp left to select saturation.