Automatic Volume Control in Home Assistant

Posted on December 29, 2023 • 4 min read • 752 words
Do you hate that too? When you switch on your Apple TV from YouTube to Music, or from Prime Video to Plex and then your amplifier’s sound is way too loud?…
Automatic Volume Control in Home Assistant

Do you hate that too? When you switch on your Apple TV from YouTube to Music, or from Prime Video to Plex and then your amplifier’s sound is way too loud?

Well, I do. That’s why I’ve created an automation in Home Assistant a while ago that ensures the sound levels from different sources are automatically adjusted by Home Assistant. How? I’ll explain.

How  

Apple TV integration

  • apps as sources
  • amplifier integration
  • input_number with the volume
  • automation that changes things based on the triggers

Before you start  

Before you start, make sure your basics are in order. That means: Your amplifier and Apple TV should be controllable by Home Assistant.

Amplifier  

In principle, you can do this with any amplifier that has the ability to adjust volume from Home Assistant. In my case, I use a relatively old Denon amplifier. Fortunately, this Denon amplifier does have a network connection. It can be controlled by Home Assistant using the standard Denon amplifier integration.

Apple TV  

For Apple TV, you’ll need a ‘relatively modern’ version. I’ve got an Apple TV HD that I use for this, so it doesn’t have to be that new.

Home Assistant  

You’ll have to use Home Assistant with the Apple TV integration. I don’t know from which version I’ve been using this, but I do know for sure that it has worked on versions 2023.06 and higher.

Since you’ve clicked on a fairly advanced topic here, I’m not going to explain to you how to configure Home Assistant , or how to include your Apple TV in Home Assistant. The same goes for how to create scripts , automations and input_number helpers. Click on these respective words for these topics, they take you to the topics on the Home Assistant site.

The steps  

Create the input_number helpers  

Nowadays, the Apple TV integration knows which apps are on the Apple TV and which ones are started. You can choose apps based on ‘source’, by giving the name of the app. So if you want to start Netflix, you choose ‘Netflix’ as the source, etc.

For this automation, the use of app_id for me is a bit more convenient. App_id is the bundle identifier. Every app on TVOS, but also on iOS, macOS, and even Android, uses it. It’s a way to uniquely identify an app in the ecosystem. It is common to use the ‘reverse domain name notation’ to avoid conflicts. For Netflix, the bundle ID looks like this: com.netflix.Netflix.

Why I use app_id, you’ll see later in the automation.

I haven’t found an easy way to read out a list of apps. Here is a list of apps that I’ve found and used so far, so you don’t have to search for them. Please note, I’ve already listed them in lower case.

App bundle ID
Netflix com.netflix.netflix
Disney+ com.disney.disneyplus
Apple Arcade com.apple.Arcade
Facetime com.apple.facetime
Apple Fitness com.apple.Fitness
Apple Movies com.apple.TVMovies
Apple Music com.apple.TVMusic
Apple Podcasts com.apple.podcasts
Amazone Prime Video com.amazon.aiv.aivapp
Airplay com.apple_tvairplay
Apple TV+ com.apple_tvwatchlist
Youtube com.google.ios.youtube
HBO Max com.hbo_hbonow
Ziggo Go com.libertyglobal.ziggogo_tv
Plex com.plexapp.plex
SkyShowtime com.skyshowtime.skyshowtime
Spotify com.spotify.client
NPO Start nl.omroep.uitzendinggemist

If you want to start a different app, you can use the ‘Developer tools’.

I created the helpers according to a certain naming: ‘std_vol_’ You have the choice to do this via the UI or via YAML files. I chose to do this in the UI.

Create the automation  

The automation itself is triggered by changing the app_id attribute. Then all the dots in the app_id are replaced by underscores, ‘_’. Then ‘std_vol_’ is added to the front. Also, the whole string is converted to lower case. This is ultimately the name of the input_number whose value is used to set the volume of the amplifier. If an app is not recognized by the automation, the amplifier automatically switches to a lower volume. That way, you’ll never be surprised again.

Complete code

alias: Automatische volume regeling
description: ""
trigger:
  - platform: state
    entity_id:
      - media_player.woonkamer
    attribute: app_id
condition: []
action:
  - service: media_player.volume_set
    target:
      entity_id: media_player.denon_x2100w
    data_template:
      volume_level: >
        {%if
        states("input_number.std_vol_"+(state_attr('media_player.woonkamer','app_id')|lower|replace('
        ','_')  | replace('.',"_"))) != "unknown" %} {{-
        states("input_number.std_vol_"+(state_attr('media_player.woonkamer','app_id')|lower|replace('
        ','_')  | replace('.',"_"))) }}  {%- else %} 0.1 {%- endif %}
mode: single

In Conclusion  

With this automation, you can finally enjoy your apps on your Apple TV, without being startled every time the volume turns out to be much louder than you thought. If you come across other app_ids, feel free to share them in the comments below.

See also

    Follow me