I think the one idea I came up with is not yet mentioned here. If you add lines like the ones below, you will be able to turn your light off (see complete code in the link above).
The two automations works slightly different from each other. On volume you are increasing the value a fixed amount at each cube event (+0.10 or -0.10). While on brightness control you are increasing it by a variable amount based on how much you did rotate the cube. you could use the same idea from brightness to the volume, but you will have to do some math as the range is not the same (brightness 1-255 and volume 0 to 1.0).
You may try something like this:
volume_level: >
{% set state = (state_attr('media_player.vu',volume_level') + trigger.event.data.action_value|float / 36-%}
{%- if state > 1.0 -%}
{%- set state = 1.0 -%}
{%- elif state < 0 -%}
{%- set state = 0 -%}
{%- endif %}
{{ state }}
I’m not home and don’t remember well what is the range of values the cube reports, but from a automation that i have, something like 18 is like half turn, so you will have to play with the “36” value to adjust the “sensitivity”. Based on these values a half turn should increase/decrease the volume by 0.50 (too much?).
Thanks for the code, it has two small typos, but I figured out:
- id: '121'
alias: "Xiaomi Cube Volume Adjust"
initial_state: 'on'
trigger:
- platform: event
event_type: xiaomi_aqara.cube_action
event_data:
entity_id: binary_sensor.cube_158d00027
action_type: rotate
action:
- service: media_player.volume_set
data_template:
entity_id: media_player.vu
volume_level: >
{% set state = (state_attr('media_player.vu','volume_level') + trigger.event.data.action_value|float / 72) -%}
{%- if state > 1.0 -%}
{%- set state = 1.0 -%}
{%- elif state < 0 -%}
{%- set state = 0 -%}
{%- endif %}
{{ state }}
It is better than previously, but it still has problems, sometimes it doesn’t register movements, sometimes it skips up-down, sometimes I turn right half cm and it goes form 20% to 100% etc . It is not like a good classic analog volume knob.
I shamelessly stole this from someone else and it works well for me, but I’m not sure it is possible to get a true equivalent to a smooth analogue dial with the cube:
trigger:
platform: event
event_type: xiaomi_aqara.cube_action
event_data:
entity_id: binary_sensor.cube_xxxxxxxxxxx
action_type: rotate
action:
- service: media_player.volume_set
data_template:
entity_id: media_player.kitchen
volume_level: >
{% set state = states.media_player.kitchen.attributes.volume_level + (trigger.event.data.action_value|float)/90 -%}
{%- if state > 1 -%}
{%- set state = 1 -%}
{%- elif state < 0 -%}
{%- set state = 0 -%}
{%- endif %}
{{ state }}
Nice. I should have warned you that have to have typos :-). A long time ago when I was testing my cube I used to “tail -f” the homeassistant log and see what HA reported when I moved the cube. I was expecting something like the angle you moved the cube (90 = quarter turn) but it wasnt, so I had to test/try which values make senses. As you say, it’s not like a good analog volume knob and it will never will, first because the cube does not report values as you are moving it but only when you finish moving and second it’s not that precise with the values.
I use my cube to control the volume of my tv but since I can’t set absolute values (I’m using IR) what I do is increment/decrement a single “unit” when I do a small rotation and increment/decrement 4 units when I do a bigger rotation. Not ideal but acceptable :-).
I still have that idea of using a cheap midi controller with sliders to do that kind of thing…
“you are moving it but only when you finish moving and second it’s not that precise with the values.”
And this is the downside of the whole cube and should be mentioned everywhere. I thought that it reports continuously. Now I see why it is working like this.
Hi everyone,
I’m a beginner and I need your help:
I tried this automation to adjust the light of the gateway, but it does not work,
can you tell me where I’m wrong?