Rotate to the left or right in Xiaomi Magic Cube?

Im trying to create an automation which increases the volume of my Chromecast Group when rotating to the right and decreases the volume when rotating to the left. Somehow this is not working for me:

- alias: Xiaomi Cube - Volume Rotate
  trigger:
    platform: event
    event_type: cube_action
    event_data:
      entity_id: binary_sensor.cube_158d000101bded
      action_type: rotate
  action:
    - service: media_player.volume_set
      data_template:
        entity_id: media_player.home_group          
        volume_level: >
          {%if trigger.event.data.action_value | float > 0 %} 
          {{  states.media_player.home_group.attributes.volume_level | float + 0.1 }} 
          {% else %}
          {{  states.media_player.home_group.attributes.volume_level | float - 0.1 }} 
          {% endif %}

Error:

2017-11-06 20:37:46 ERROR (MainThread) [homeassistant.core] Invalid service data for media_player.volume_set: expected float for dictionary value @ data['volume_level']. Got "'0.29999997317790983'"
2017-11-06 20:37:47 ERROR (MainThread) [homeassistant.core] Invalid service data for media_player.volume_set: expected float for dictionary value @ data['volume_level']. Got "'0.29999997317790983'"

Can anyone help me out, It is expecting a float and for as far as I can see it’s getting one right?

UPDATE: Removing the quotes did the trick :slight_smile:. I also removed them in the code above so that code is working.

Why doesn’t this work?

- alias: Xiaomi Cube - Brightness [Rotate]
  trigger:
    platform: event
    event_type: cube_action
    event_data:
      entity_id: binary_sensor.cube_xxxxxxxx
      action_type: rotate
  condition:
      - condition: state
        entity_id: input_select.cube_mode
        state: 'Light'   
  action:
    - service: light.turn_on
      data_template:
        entity_id: light.pc      
        brightness: >
          {%if trigger.event.data.action_value | float > 0 %} 
          {{  states.light.pc.attributes.brightness | int + 10 }}
          {% else %} 
          {{  states.light.pc.attributes.brightness | int - 10 }}
          {% endif %}

I just started attempting the same, there are a couple of problems.

The biggest is a light that is off has no brightness value, so states.light.pc.attributes.brightness throws an error. So that needs logic to cope with if the light is off. This is just an annoying thing about the light type IMO.

I noticed that if I turned the light on then rotated left, it did dim it, then remembered this thing.

Smaller issue is “+10” is a pretty small increment between 0 and 255, so that’s a lot of rotating, but that’s minor of course.

Subtler is I believe the rotate returns 90 or 180 (I know it’s an angle, are these the only values like flip - not sure). So you turn the cube twice (180 degrees) and still only increment 10. So that makes it even more rotation.

I also wonder what will happen when the value goes out of range 0 - 255. Maybe nothing but a log error, but that should get trapped too.

If I get a chance to make something work, I’ll post it.

1 Like

I looked at cuberotate values - they’re decimals, not sure what they correspond too, but maybe 25 is a quarter turn:

 7:05 PM
White cube rotate has been triggered
7:05 PM
cuberotate 31.5
7:05 PM
White cube rotate has been triggered
7:05 PM
cuberotate -13.37
7:05 PM
White cube rotate has been triggered
7:05 PM
cuberotate -29.5

Anyway, I just made it step the same value for any motion. it’s not going to feel like an analog dial in my opinion, so I just picked ‘40’ as a reasonable value between 0-255. Works for me:

- id: white_cube_rotate_on
  alias: White cube rotate on
  trigger:
    platform: event
    event_type: cube_action
    event_data:
      entity_id: binary_sensor.cube_XXXX
      action_type: rotate
  condition:
    - condition: state
      entity_id: light.s_lamp
      state: 'off'
  action:
    - service: light.turn_on
      data:
        entity_id: light.s_lamp
        brightness: 40

- id: white_cube_rotate
  alias: White cube rotate
  trigger:
    platform: event
    event_type: cube_action
    event_data:
      entity_id: binary_sensor.cube_XXXX
      action_type: rotate
  condition:
    - condition: state
      entity_id: light.s_lamp
      state: 'on'
  action:
    - service: light.turn_on
      data_template:
        entity_id: light.s_lamp
        brightness: >
          {%- set delta = 40 %}
          {%if trigger.event.data.action_value | int > 0 %}
            {%- set bright = states.light.s_lamp.attributes.brightness | int + delta %}
          {% else %}
            {%- set bright = states.light.s_lamp.attributes.brightness | int - delta %}
          {% endif %}
          {% if bright > 255 %}
            255
          {% elif bright < 0 %}
            0
          {% else %}
            {{bright}}
          {% endif %}
3 Likes
- condition: state
    entity_id: input_select.cube_mode
    state: 'Light'

Mine is the same but my condition is indented by 2 spaces yours seems indented by 4 spaces?

Thanks knss70. I have been messing around for days trying to do exactly this. I cut and pasted your code, updated the lamp name and it works perfectly.

Very sharp of you to notice (Y). But that can’t be it because this is working fine:

- alias: Xiaomi Cube - Volume [Rotate]
  trigger:
    platform: event
    event_type: cube_action
    event_data:
      entity_id: binary_sensor.cube_158d000101bded
      action_type: rotate
  condition:
      - condition: state
        entity_id: input_select.cube_mode
        state: 'Radio'   
  action:
    - service: media_player.volume_set
      data_template:
        entity_id: media_player.home_group          
        volume_level: >
          {%if trigger.event.data.action_value | float > 0 %} 
          {{  states.media_player.home_group.attributes.volume_level | float + 0.05 }}
          {% else %} 
          {{  states.media_player.home_group.attributes.volume_level | float - 0.05 }}
          {% endif %}

The only things I can think of is that the input_select doesn’t have a state of Light or the light.pc doesn’t have attributes.brightness.

Mine works fine…

 - alias: Xiaomi Cube - Brightness [Rotate]
   initial_state: 'on'
   trigger:
     - platform: event
       event_type: cube_action
       event_data:
         entity_id: binary_sensor.cube_158d000117d706
         action_type: rotate
   condition:
     - condition: state
       entity_id: input_select.cubemode
       state: 'Lights'   
   action:         
     - service: light.turn_on
       data_template:
         entity_id: light.zw_uplight_dimmer_level
         brightness: >-
           {%if trigger.event.data.action_value | float > 0 %}
           {{  states.light.zw_uplight_dimmer_level.attributes.brightness | int + 50 }}
           {% else %}
           {{  states.light.zw_uplight_dimmer_level.attributes.brightness | int - 50 }}
           {% endif %}
1 Like

I think I’m going crazy, after copying your automation and changing the lights, cube Id and input_select it works…

I doesn’t work flawless I must say, there is a pretty big delay sometimes and when controling multiple lights they don’t allways change brightness. I’m seeing the following in my log:

2017-12-03 21:47:12 ERROR (Thread-2) [homeassistant.components.light.yeelight] Error when calling <function YeelightLight.set_brightness at 0x69b95c00>: {'code': -1, 'message': 'client quota exceeded'}
2017-12-03 21:47:12 ERROR (Thread-15) [homeassistant.components.light.yeelight] Unable to update bulb status: {'code': -1, 'message': 'client quota exceeded'}

I’m using a combination of Yeelight and LimitlessLed.

- alias: Xiaomi Cube - Brightness [Rotate]
  initial_state: 'on'
  trigger:
    - platform: event
      event_type: cube_action
      event_data:
        entity_id: binary_sensor.cube_158d000101bded
        action_type: rotate
  condition:
    - condition: state
      entity_id: input_select.cube_mode
      state: 'Light'   
  action:         
    - service: light.turn_on
      data_template:
        entity_id: light.pc, light.hall_way, light.living_room, light.diner_table
        brightness: >-
          {%if trigger.event.data.action_value | float > 0 %}
          {{  states.light.living_room.attributes.brightness | int + 25 }}
          {% else %}
          {{  states.light.living_room.attributes.brightness | int - 25 }}
          {% endif %} 

hm… from the yeelight docs: “Per default the bulb limits the amount of requests per minute to 60, a limitation which can be bypassed by enabling the music mode.”

I’m not entirely sure how HA knows what light to dim/brighten when there are multiple candidates, so there may be some internal problems with doing it this way.

I think the better way to go would be to get the groups expressing the attributes for color and brightness given that groups already know that lights can have those attributes.

HI,
Thanks @knss70 for the code. However, I am getting an error thrown using 0.60.0 whenever I rotate. Any ideas anyone?

Sun Feb 11 2018 22:08:18 GMT+1000 (E. Australia Standard Time)

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/homeassistant/helpers/template.py", line 118, in async_render
    return self._compiled.render(kwargs).strip()
  File "/usr/lib/python3.6/site-packages/jinja2/asyncsupport.py", line 76, in render
    return original_render(self, *args, **kwargs)
  File "/usr/lib/python3.6/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 4, in top-level template code
  File "/usr/lib/python3.6/site-packages/jinja2/sandbox.py", line 385, in getattr
    value = getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'None' has no attribute 'attributes'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.6/asyncio/tasks.py", line 180, in _step
    result = coro.send(None)
  File "/usr/lib/python3.6/site-packages/homeassistant/components/automation/__init__.py", line 343, in async_trigger
    yield from self._async_action(self.entity_id, variables)
  File "/usr/lib/python3.6/site-packages/homeassistant/components/automation/__init__.py", line 433, in action
    yield from script_obj.async_run(variables)
  File "/usr/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 151, in async_run
    yield from self._async_call_service(action, variables)
  File "/usr/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 181, in _async_call_service
    self.hass, action, True, variables, validate_config=False)
  File "/usr/lib/python3.6/site-packages/homeassistant/helpers/service.py", line 76, in async_call_from_config
    config[CONF_SERVICE_DATA_TEMPLATE]))
  File "/usr/lib/python3.6/site-packages/homeassistant/helpers/service.py", line 72, in _data_template_creator
    for key, item in value.items()}
  File "/usr/lib/python3.6/site-packages/homeassistant/helpers/service.py", line 72, in <dictcomp>
    for key, item in value.items()}
  File "/usr/lib/python3.6/site-packages/homeassistant/helpers/service.py", line 74, in _data_template_creator
    return value.async_render(variables)
  File "/usr/lib/python3.6/site-packages/homeassistant/helpers/template.py", line 120, in async_render
    raise TemplateError(err)
homeassistant.exceptions.TemplateError: UndefinedError: 'None' has no attribute 'attributes'

Thanks!

Try to upgrade to 0.63

Thanks just did an hassio update from the command line and it updated to 0.62.1

Error has reduced to just:

Error rendering data template: UndefinedError: 'None' has no attribute 'attributes'
10:46 PM helpers/service.py (ERROR)

Plus I have some other errors I need to look at but they don’t seem associated…

Did anybody managed to get this really working?

Rotating the cube to change the brightness

See my post above it works fine, tho’ I’m only doing for a single light :slight_smile:

1 Like

Thanks for your code. I’m unable to test right now, but does it also turn off the light when rotating left after lowest dim-setting?

That depends on your light, mine does, ymmv :slight_smile:

Hi m8. Could i ask you to share the code for the mode-control for the cube as well?

Do you have any kind of public library? Your solutions looks fantastic, so i would love to be inspired - and copy/paste some of the solutions too :wink:

This from my input_select.yaml

cubemode:
  name: Cube Mode
  options:
    - Music
    - Lights
    - None
  initial: Lights 
  icon: mdi:cube-outline