Set volume from input number to mopidy player

I would like to have an automation with input_number component as a slider, to control volume of mopidy media_player. so when I move the slider, the volume of the media_player should change accordingly. This is my setup in configuration.yaml:
input_number:
glasnost:
name: glasnost
initial: 0.5
min: 0
max: 1
step: 0.1

media_player:
  - platform: mpd
    name: mpd
    alias: mpd dnevna
    host: 192.168.1.9

and this is automations.yaml:

- alias: slider_mpd
  trigger:
    platform: state
    entity_id: input_number.glasnost
  action:
    - service: media_player.volume_set
      data:
        entity_id: media_player.mpd
        volume_level: "{{ states('input_number.glasnost') }}" 
       # volume_level: 0.60

However, nothing happens when I move the slider. On the other hand, if I uncomment the line
volume_level: 0.60
then the volume level of media_player.mpd changes to 0.60, so the autromation works. also, the media_player.mpd accepts values between 0.0 and 1.0. Any thoughts why this is not working?

1 Like

using code like this "{{ .... }}" means you are using templating. Templating is only allowed in certain fields. In the action section, its typically only allowed in value_template sections or data_template sections. Your code is only using a data section, so you need to change that from data to data_template:

- alias: slider_mpd
  trigger:
    platform: state
    entity_id: input_number.glasnost
  action:
    - service: media_player.volume_set
      data_template: #<--------------Changed to data_template from data
        entity_id: media_player.mpd
        volume_level: "{{ states('input_number.glasnost') }}" 
       # volume_level: 0.60

(optional) You can also stream line this to use more than just one slider to control your volume_level. Also, this will make your code more manageable. Basically, instead of using a hard coded entity_id, you can utilize the trigger:

- alias: slider_mpd
  trigger:
    - platform: state #<--- put a dash here so that more triggers can be added to this list in the future.
      entity_id: input_number.glasnost # this entity causes the automation to 'trigger'
  action:
    - service: media_player.volume_set
      data_template: #<--------------Changed to data_template from data
        entity_id: media_player.mpd
        volume_level: "{{ trigger.to_state.state }}" #<---- uses what caused the automation to 'trigger'
1 Like

thank you, it works now.

Hi,

I am trying to configure above so Volume from my Sony TV controls my Yamacha volume:

- alias: Volume_OPT_Control
  trigger:
    - platform: numeric_state
      entity_id: media_player.sony_bravia_tv # this entity causes the automation to 'trigger'
  action:
    - service: media_player.volume_set
      data_template:
        entity_id: media_player.salon_2
        volume_level: "{{ trigger.to_state.state }}"

But all I get is a load off errors