Set volume level on media player (Sony Bravia TV)

In the thread Universal media_player volume_set entity takes value I contributed to a solution in order to set the media volume. When I tried to use this solution, It did not work for me.

What I want to do is to prevent the kids from playing media on a (to) high volume on weekend mornings.

Here’s my automation:

  - alias: "Media: Tillåt maxvolym 4 % på TV morgnen helger"
    trigger:
      - platform: numeric_state
        entity_id: media_player.sonytv
        value_template: '{{ states.media_player.sonytv.attributes.volume_level }}'
        above: 0.04
    condition:
      - condition: time
        before: '09:00:00'
        weekday:
        - sat
        - sun
    action:
      - service: media_player.volume_set
        data_template:
          entity_id: media_player.sonytv
          volume_level: '{{ 0.04 }}'

Any suggestions are welcome.

I can spot one thing anyway, I’d change the order in the below

to

   action:
      service: media_player.volume_set
      entity_id: media_player.sonytv
      data_template:
        volume_level: '{{ 0.04 }}'

Bumbing up this thread since I never solved the problem.

Thanks @Bit-River. Now when I am a bit more experienced in HASS, here is the current version of the part of the script that does not work.

   action:
      service: media_player.volume_set
      entity_id: media_player.sonytv
      data_template:
        volume_level: '{{ 0.03 }}'

unfortunately. , this does not work

Your trigger was accessing the data in an odd way, so i adjusted it to pull correctly.

Also, you don’t need a template for volume_level

  - alias: "Media: Tillåt maxvolym 4 % på TV morgnen helger"
    trigger:
      - platform: numeric_state
        entity_id: media_player.sonytv
        value_template: '{{ state.attributes.volume_level }}'
        above: 0.04
    condition:
      - condition: time
        before: '09:00:00'
        weekday:
        - sat
        - sun
    action:
      - service: media_player.volume_set
        data_template:
          entity_id: media_player.sonytv
          volume_level: 0.04

Thanks @petro for your assistance in this issue.
Your concern is always appreciated.

The trigger is working as expected, but the action does not.
I guess that culprit could be either the HA-component or perhaps my TV-set.
I can turn on/off the TV, se all the attributes and so on.

Interesting, have you tried just doing a basic call without the automation in the service call tester?

Your js would be:

{'volume_level':0.04}

When sending

{
	"volume_level": "0.04"
}

Nothing is changed.

When accessing the device from the frontend, I cannot change the volume using the slider, but it works when i am using the vol+ / vol- icons.

This tells me that I have to find som kind of custom component that provides this feature.

I’m guessing your TV does not give a range of volume. Some media devices don’t have the ability to set volume, only increment it.

I guess you are right. To bad for such an expensive TV. (Sony 65XD8505)

A kind of work around is to loop 100 times and decrease the volume by 1. Then loop 4 times and increase the volume by 1.

I’ll try that and se if it works.

I have the same issue. Won’t work by volume_set or volume_mute so I have to send multiple commands as below. Very untidy.

- service: media_player.volume_down
  entity_id: media_player.sony_43

etc etc x 40!!!
Wish there was a for next loop!
It is frustrating that mute works via the icon in the media player panel.

1 Like

This does not work

mute_sony43_using_volumeset:
  sequence:
  - service: notify.notify_lounge
    data:
      message: "Mute Sony 43 using VolumeSet start"  

  - service: media_player.volume_set
    data_template:
      entity_id: media_player.sony_43
      volume_level: '{{ 0.0 }}'
  
  - service: notify.notify_lounge
    data:
      message: "Mute Sony 43 using VolumeSet END"

Well, actually there’s a way to use loops but then you have to use Jinja2-templates.

I’ll try to write some code later this eavning. It’s not that hard to do when you’ve understood how templating works.

1 Like

Thanks ibennani
:slight_smile:

Count me in on this; my kids have a habbit of turning their room speakers too loud. When that happens, I get my phone out and manually play volume wars with them; I turn it down they turn it up, rinse and repeat. I would get a kick out of it if I could hand this task off to HA; kid turns volume past 60% or more, HA triggers automation to lower it to 50%, kid tries again, HA does it’s thing… and I just chuckle to myself.

Haha, figured it out, inspired by damocles-git’s post. Tested the below and it works brilliantly, que evil dad laugh.

  - alias: Volume Too High in the Kids Room
    initial_state: true
    hide_entity: false
    trigger:
      - platform: template
        value_template: '{{ states.media_player.kids_bedroom.attributes.volume_level > 0.7 }}'
    action:
      - service: media_player.volume_set
        entity_id: media_player.kids_bedroom
        data_template:
          volume_level: '0.6'

Note: The speaker I am controlling is a Bose SoundTouch

EDIT: Improved on the original template so it no longer throws errors in the logs for misconfiguration.