Quantcast
Channel: Configuration - Home Assistant Community
Viewing all articles
Browse latest Browse all 102800

Remember volume levels of each source for a media player, to automate restoring

$
0
0

Hello!

I have spent the morning trying to find a way to monitor the volume_level+source of a media player (a universal media player, but I think that is immaterial), so that I might set the volume_level to the last used level for that source.

My AVR (Denon) for whatever reason plays HEOS/Spotify/streams at SUCH a loud volume compared to anything else. I have to have it at ~40% volume, but everything else at 65%.

I had this idea to use an input_text to store a JSON blob mapping source name to last-known volume level and combine it with an automation which triggered on the source of the media player changing.

Unfortunately, I’m having a hell of a time trying to construct JSON in a template. I suspect I could use Node-RED for this but I don’t yet have any experience with it and really would prefer to keep the definition for this automation in my media_centre.yaml package if possible.

What I’ve tried:

{% set volumes = iif(is_state('input_text.cached_volumes', 'unknown'), "{}", states('input_text.cached_volumes')) | from_json %}
{% set source = state_attr('media_player.living_room', 'source') %}
{% set level = state_attr('media_player.living_room', 'volume_level') %}

{# This raises a SecurityError #}
{% set _ = volumes.update({source: level}) %}

{# This allows me to _set_ keys but I can't turn it into JSON and I can't use dynamic key names #}
{% set volumes = namespace(volumes) %}
{% set volumes.foo = level %}

The closest I can get (which tbh may be workable) is to store a list of dictionaries as json, instead of single dictionary, but then the update logic will be quite more difficult to avoid duplicating entries:

{% set volumes = iif(is_state('input_text.cached_volumes', 'unknown'), "[]", states('input_text.cached_volumes')) | from_json %}
{% set source = state_attr('media_player.living_room', 'source') %}
{% set level = state_attr('media_player.living_room', 'volume_level') %}
{% set volumes = volumes + [{"source": "e.g. source", "volume_level": 0.3}] %}
{% set volumes = volumes + [{"source": source, "volume_level": level}] %}
{{ volumes | to_json }}

which outputs:

[{"source": "e.g. source", "volume_level": 0.3}, 
 {"source": "HEOS Music", "volume_level": 0.5}]

Is this my best bet? Am I missing an obvious way to do this more easily (besides Node-RED, which I will definitely look into if I am not left with a superior option).

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 102800

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>