Automation trigger volume change

Hello everyone!
I’ve been scraching my head big time on how to do this automation.
I have a Sony Android TV which HA see’s as media_player.sony_bravia_tv which has a volume control (volume_up, volume_down, volume_set).
I’m trying to set u a trigger when volume goes up to action a script called script.volumeup and when volume goes down to action a script called script.volumedown.
The volume scripts send mqtt messages to my nodemcu which has IR sender on it controlling my sound sistem. The script is all set. All I need is the automation, so when I change volume on the TV it will chage volume on my sound system as I want to use optical out on TV.

Any help is much appreciated!

3 Likes

I don’t think bravia can tell you the volume level its on or that volume is changing so I don’t think HA can know that volume is being turned up. You might try to use lirc component to read the IR from the remote.

1 Like

I can click in HA on volume up/down so the must be a way to do it. I had a look at what other people were doing in regards to volume control, but I couldn’t find the answer to what I am trying to acheive.

Just because you can adjust a hardware device does not mean you can monitor the state of it. Check the states of your bravia tv. My guess is that it would be named media_player.braviatv or something like that. If that state object does not contain a volume level, then you are out of luck.

I so wanted to prove you wrong petro.
thanks to mistercormell I got an idea on how to do it, but I’m a bit stuck.

so far I made a sensor

sensor:

  • platform: template
    sensors:
    sony_volume:
    value_template: ‘{{ states.media_player.sony_bravia_tv.attributes.volume_level }}’
    entity_id: media_player.sony_bravia_tv

and the automation so far looks like this:

  • action:
    • alias: ‘’
      data:
      entity_id: script.volumeup
      service: script.volumeup
      condition: []
      trigger:
    • above: ‘0.01’
      below: ‘0.05’
      entity_id: sensor.sony_volume
      platform: numeric_state
    • above: ‘0.06’
      below: ‘0.10’
      entity_id: sensor.sony_volume
      platform: numeric_state

So I can do volume up, but I don’t know how to do volume down. I need some sort of tempate.
Can someone help please?

Lol, its all good. The good news is that from the looks of it, you can use the volume_level attribute to perform your automations. It should update when you use your remote as well.

This is an example of what I have done in the past using a slider bar and the volume_level attribute for my media_players.

- alias: Set Zone 1 Slider
  trigger:
    - platform: state
      entity_id: media_player.yamaha_receiver
  condition:
    - condition: template
      value_template: >
        {% if is_state('media_player.yamaha_receiver','off') %}
          true
        {% else %}
          {% if is_state_attr('media_player.yamaha_receiver', 'volume_level', states('input_number.yamaha_receiver') | int / 100) %}
            false
          {% else %}
            true
          {% endif %}
        {% endif %}
  action:
    - service: input_number.set_value
      entity_id: input_number.yamaha_receiver
      data_template:
        value: >
          {% if trigger.to_state.state == 'on' %}
            {% set n = trigger.to_state.attributes.volume_level | float %}
            {{ (n - 1.0)*100.0 | int }}
          {% else %}
            -80
          {% endif %}

- alias: Set Zone 1 Volume
  trigger:
    - platform: state
      entity_id: input_number.yamaha_receiver
  condition:
    - condition: template
      value_template: >
        {% if is_state('media_player.yamaha_receiver', 'on') %}
          {% if not is_state_attr('media_player.yamaha_receiver', 'volume_level', states('input_number.yamaha_receiver') | int / 100) %}
            true
          {% else %}
            false
          {% endif %}
        {% else %}
          false
        {% endif %}
  action:
    - service: media_player.volume_set
      entity_id: media_player.yamaha_receiver
      data_template:
        volume_level: >
          {% set n = states('input_number.yamaha_receiver') | float %}
          {{ n / 100 + 1 }}
          
- alias: Set Zone 2 Slider
  trigger:
    - platform: state
      entity_id: media_player.yamaha_receiver_zone_2
  condition:
    - condition: template
      value_template: >
        {% if is_state('media_player.yamaha_receiver_zone_2','off') %}
          true
        {% else %}
          {% if is_state_attr('media_player.yamaha_receiver_zone_2', 'volume_level', states('input_number.yamaha_receiver_zone_2') | int / 100) %}
            false
          {% else %}
            true
          {% endif %}
        {% endif %}
  action:
    - service: input_number.set_value
      entity_id: input_number.yamaha_receiver_zone_2
      data_template:
        value: >
          {% if trigger.to_state.state == 'on' %}
            {% set n = trigger.to_state.attributes.volume_level | float %}
            {{ (n - 1.0)*100.0 | int }}
          {% else %}
            -80
          {% endif %}

- alias: Set Zone 2 Volume
  trigger:
    - platform: state
      entity_id: input_number.yamaha_receiver_zone_2
  condition:
    - condition: template
      value_template: >
        {% if is_state('media_player.yamaha_receiver_zone_2', 'on') %}
          {% if not is_state_attr('media_player.yamaha_receiver_zone_2', 'volume_level', states('input_number.yamaha_receiver_zone_2') | int / 100) %}
            true
          {% else %}
            false
          {% endif %}
        {% else %}
          false
        {% endif %}
  action:
    - service: media_player.volume_set
      entity_id: media_player.yamaha_receiver_zone_2
      data_template:
        volume_level: >
          {% set n = states('input_number.yamaha_receiver_zone_2') | float %}
          {{ (n / 100.0) + 1.0 }}

The math performed on my slider bars is because my receiver displays decibels for some stupid reason. So i had to convert it.

So you could just remove the n / 100 + 1 crap you see everywhere and get it working with sliders.

I have a few more minutes to answer your question alittle more directly… This is what a volume up or volume down action for an automation:

action:
  - service: media_player.volume_set
    entity_id: media_player.sony_bravia_tv
    data_template: 
      volume_level: >
        {% set volume = states('media_player.sony_bravia_tv') | float %}
        {% if volume + 0.01 > 1.0 %}
          1.0
        {% else %}
          {{ volume + 0.01 }}
        {% endif %}

volume down’s action would be similar:

action:
  - service: media_player.volume_set
    entity_id: media_player.sony_bravia_tv
    data_template: 
      volume_level: >
        {% set volume = states('media_player.sony_bravia_tv') | float %}
        {% if volume - 0.01 < 0.0 %}
          0.0
        {% else %}
          {{ volume - 0.01 }}
        {% endif %}

I appreciate you reply but this doesn’t actually work for me.
I have the action in form of a script
what I’m missing is the trigger
when volume decreases I want to call script volume down and when volume increases I want to call script volume down.
Someone suggested Binary sensor trend
I’ve set it up but it doesn’t go on when I change volume. The sensor for the volume works fine, it does change volume.
binary_sensor:

  • platform: trend
    sensors:
    volume:
    entity_id: sensor.sony_volume

sensor:

  • platform: yr

  • platform: template
    sensors:
    sony_volume:
    value_template: ‘{{ states.media_player.sony_bravia_tv.attributes.volume_level * 100}}’
    entity_id: media_player.sony_bravia_tv

    • action:
  • alias: ‘’
    data:
    entity_id: script.volumeup
    service: script.volumeup
    alias: Volume Sony to Sound System 2
    condition: []
    id: ‘1518382654591’
    trigger:

  • above: ‘1’
    below: ‘5’
    entity_id: sensor.sony_volume
    platform: numeric_state

  • above: ‘6’
    below: ‘10’
    entity_id: sensor.sony_volume
    platform: numeric_state

Volume up:

  trigger:
    - platform: state
      entity_id: media_player.sony_bravia_tv
  condition:
    condition: template
    value_template: >
      {% set from = trigger.from_state.attributes.volume_level | float %}
      {% set to = trigger.to_state.attributes.volume_level | float %}
      {{ to > from }}

Volume down:

  trigger:
    - platform: state
      entity_id: media_player.sony_bravia_tv
  condition:
    condition: template
    value_template: >
      {% set from = trigger.from_state.attributes.volume_level | float %}
      {% set to = trigger.to_state.attributes.volume_level | float %}
      {{ to < from }}
5 Likes

Just an afterthought… What are you trying to do with the script?

I’ve spent load of hours trying to find a soluton. I can’t thank you enough!
Cheers!
It does work!
So, don’t know if I was clear but I have a sony android tv on which I wanted to use the original remote to control volume but not for the tv but for my surround sound system because the optical link between them.
I’ve setup a nodemcu with IR sender on it next ot the IR receiver for the surround sound system and I’m now pressing the volume on the remote which triggers the automation which calls script volume up or down. The script contains the IR code which is sent via MQTT to nodemcu. Does it make sense?
It’s like scratching your left ear with you right hand above your head. BUT it works.

The only thing that I can’t do at the moment is if I keep volume up/down pressed on the TV remote it won’t actually turn the volume up/down as much as I would like and I understand why. You can’t have it all…
Small thing to point out. there is a 5-10 seconds delay from the time that I press volume button on tv remote till sound system changes volume.

Once again! Thanks Petro! I hope this post will help other people with their automations!

Lol, thats a pretty cool work around. Glad it works.

You can remove some of the delay by switching over to appdaemon. I’m not sure how much delay will get removed for you. It ended up removing about ~1 second for me with the automations I posted above. At the time, they were taking ~2 seconds to fire. It might chop off ~5 seconds for you. But that would require you to learn python if you don’t already know it.

Dear,
i tried many ways to get it working but unable to do so
i have android tv box ( mini m8sii)
and i wanted volume bar for it,
can you please guide me,
thanks in adv

what do your automations look like?

I have deleted all files related to slider,
I wanted to play media files in morning with volume to 25%,
thats what i want to do
I have Mini M8sii ( Andorid TV Box) witn onkyo receriver.
and i have installed HA on my oprange Pi +2e.
Please

does HA see the media player?

yes it does
i am using broadlink rm mini3 to control all my media devices

my media configuration

  • platform: broadlink
    host: 192.168.11.19
    mac: ‘34:EA:34:58:BD:36’
    name: SamsungTv
    ircodes_ini: ‘broadlink_media_codes/samsung.ini’
    ping_host: 192.168.11.37
    and i am using vassils vpnmaster

Then all you need to do is build your automation using service: media_player.volume_set.

The documentation for this service is located here:

example automations are here:

each breakout of the automations are here:

dear,
i think this will be too hard for me to go around,
If you can help me that would make me my automation work,
else i think i have to stop automation for my media player

Well I can’t help you if I don’t know what you want the automation to do.

doesn’t give enough information.

do you know how to write automations in general?