I’m trying to get my Aqara Cube to increase and decrease the volume of my music based on turning it clockwise or counter-clockwise. My media_player is an Onkyo TX-NR717 amplifier which works fine with volume_level between 0 and 1.
The Aquara Cube is “too slow” to change the volume if I use volume_up and volume_down, because it uses 0.5 increments in the amplifier’s 0 to 80 range (40 being loud enough), and the cube takes one or two seconds before firing each event.
I tried to following to use volume_set based on the current volume_level, which I found in some other thread and seems like a nice solution, but it doesn’t work for me:
If I instead replace the entire “state_attr” lines with only absolute values such as 0.5 and 0.1, it works as expected, by changing to volume to the low value when turning one way, to the loud value when turning the other way.
When I go to see the state of my entity, it displays the expected value for volume_level such as can be seen here:
But when trying the +0.05% and -0.05% approach from above, it seems like the float it’s getting is 0 each time. I must be doing something stupid or wrong on the state_attr lines, but I’m unable to figure out what it is. Is someone able to point it out? Or tell me how to debug the output of the state_attr() on each trigger?
Thanks for the replies. I already have a condition in my automation about the media_player being on, so that’s covered. I don’t have any about the current volume_level, so I do see some errors in the logs about it needing to be above 0 when it’s already below 0.05 and trying to lower it, but those are expected.
The only other log entries I see are about “Cube Rotate: Already running” (the name of my automation), which probably happen when I rotate it multiple times in a row too fast. But even when slowly rotating the cube, and not seeing those logs, things still don’t work as expected.
I also tried adding |round(2) after the |float and that didn’t change anything.
Very weird. Then it hit me: I do have one “non standard” thing volume related:
media_player:
- platform: onkyo
host: tx-nr717.marmotte.ici
name: Onkyo TX-NR717
# In percent, so 60 vs. original 80
max_volume: 75
sources:
bd: 'Wii'
cbl: 'Pi3'
stb: 'Wii U'
game: 'S. Nintendo'
aux1: 'AUX'
In the above, I commented out the max_volume line, restarted HA… and now it works as expected! Weird and most probably a bug. Should I report this on GitHub, maybe?
Now I’m off to tweaking my data_template to increase/decrease volume based on the data.event value!
Even if max_volume is defined in the configuration, you said the template works when used like this:
The documentation indicates max_volume is used for scaling the volume level. I have trouble understanding how the presence of the max_volume option affects a template using state_attr().
you could always just use an equation. FWIW a new compensation integration is coming through that allows you to fit polynomials to datasets, you have a dataset. You can then use the coefficients to do the math for you.
Anyways, here’s the equation that would work for you for any rotation
service: media_player.volume_set
entity_id: media_player.onkyo_tx_nr717
data_template:
volume_level: >-
{% set e = trigger.event.data.event | int %}
{% set v = state_attr('media_player.onkyo_tx_nr717', 'volume_level')|round(2) %}
{{ v + 0.0000625 * e }}
when the new compensation integration is built, you can use it to make fits for you and give you the coefficients
service: media_player.volume_set
entity_id: media_player.onkyo_tx_nr717
data_template:
volume_level: >-
{% set e = trigger.event.data.event | int %}
{% set v = state_attr('media_player.onkyo_tx_nr717', 'volume_level')|round(2) %}
{% set c = state_attr('sensor.media_player_onkyo', 'coefficients')[0] %}
{{ v + 1 / c * e }}
Thanks a lot petro for these insightful details and examples! I have tested the equation approach, but reverted back to having 3 mapped intervals because of two issues:
The Cube is very bad at sending small values. When you don’t turn it much, no event happens. This could be worked around in the equation, I guess, but…
I don’t want to be able to turn the volume too high too fast, especially now that I’ve had to remove the max_volume to work around my initial problem
Still, thank you very much for all of these details!