Volume slider for media_player causes loop

Hi everyone

I have a bluesound speaker configured in my HA system as a media_player. The thing is that I would like to create a volume slider for the speaker in one of my groups.

What I have done is to create the following:

  • A input_slider
  • Automation for slider- > speaker
  • Automation for speaker -> slider

The problem is that when I am using the automation below, it ends up in a loop where the slider updates the speaker, the speaker updates the slider with the old value, and the volume goes up and down in a never ending loop.

- alias: Volume stue (Slider -> Device)
  trigger:
    platform: state
    entity_id: input_slider.volume_stue
  action:
    - service: media_player.volume_set
      data_template:
        entity_id: media_player.bluesound_stue
        volume_level: '{{ trigger.to_state.state }}'

- alias: Volume stue (Device -> Slider)
  trigger:
    platform: state
    entity_id: media_player.bluesound_stue
  action:
    - service: input_slider.select_value
      data_template:
        entity_id: input_slider.volume_stue
        value: '{{ states.media_player.bluesound_stue.attributes.volume_level }}'

I did another attempt by adding the following to both automations:

- wait_template: "{{ states.media_player.bluesound_stue.attributes.volume_level == input_slider.volume_stue }}"
  timeout: 00:00:01

This stops the loop behavior, but instead it throws an error in the log. (Also, the slider is not 100% reliable with this wait_tempalate present, but that might be something else.)

2017-09-25 20:22:31 ERROR (MainThread) [homeassistant.helpers.condition] Error during template condition: UndefinedError: 'input_slider' is undefined
2017-09-25 20:22:31 ERROR (MainThread) [homeassistant.helpers.condition] Error during template condition: UndefinedError: 'input_slider' is undefined

Do any you have any suggestion on how I can fix this issue? My goal is to get a standalone slider for the speaker, so I can place it in any of the groups I want.

Thanks in advance. Any help is much appriciated.
Henrik

1 Like

Hey, a little late, but I am with the same issue at the moment and found this:

Maybe it is usefull for you too.

Hi,
I want to do the exact same thing, did you find a good solution or would you like to work one out?

Hi, and sorry for the late reply.

I didn’t really manage to get it working properly using input sliders and automations. The closest I got was a solution that worked most of the times, but occasionally ended up in a loop where it was updating it self, making it more or less useless.

What I ended up with at last was to just have a one-way sync from HA -> media player. It solves my use case, but if i change the volume directly on the media_player, it is not reflected in HA. In my case that is not a big problem, so I stopped investigating further.

I do remember to have seen someone using appdaemon and a python-script to do this kind of bidirectional functionality. (Bidirectional input slider fo media player).
Looks like the link to the github repo is broken, but maybe @tinmice could share the code again?

Regards
Henrik

Hi absolutely no worries!

I’ve actually solved this problem, while it’s not the cleanest solution it does indeed work.
I have duplicates of every state regarding the speaker I want to control.
So I have volume_state and volume_slider.

When the speaker updates it’s state via the control or buttons, the volume_state and the volume_slider is changed. Now this will trigger state change for the volume_slider, but no change request will be sent to the speaker since in the state_change automation I have if volume_state != volume_slider as a condition.

Logitech Speakers -> Home assistant

  - id: logitech_z906_state_change
      alias: Logitech z906 state change (Logitech > HA)
      trigger:
        platform: mqtt
        topic: speaker/logitech_z906/state/json
      action:
     - service: script.change_logitech_volume_state
       data_template:
         value: '{{ trigger.payload_json["settings"]["soundlevel"] | int }}'

The script

change_logitech_volume_state:
  alias: Change Logitech volume state (HA)
  sequence:
    - service: input_number.set_value
      data_template:
        entity_id: input_number.logitech_z906_volume
        value: '{{ value }}'
    - service: input_number.set_value
      data_template:
        entity_id: input_number.logitech_z906_volume_slider
        value: '{{ value }}'

Home assistant -> Logitech Speakers

- id: logitech_z906_volume_slider_change
  alias: Logitech z906 volume state change (HA > Logitech)
  trigger:
    platform: state
    entity_id: input_number.logitech_z906_volume_slider
  condition:
  - condition: template
    value_template: '{{ trigger.to_state.state != states.input_number.logitech_z906_volume.state
      }}'
  action:
  - service: input_number.set_value
    data_template:
      entity_id: input_number.logitech_z906_volume
      value: '{{ trigger.to_state.state | int }}'
  - service: mqtt.publish
    data_template:
      topic: speaker/logitech_z906/cmnd/json
      payload_template: '{"method":"setSettings", "soundlevel":{{ trigger.to_state.state
        | int }}}'

I hope this helps someone!
Ps, Otroligt bra namn, Bananprinsen.

@bananprinsen thanks for the poke, I moved everything to Gitlab a while back - I’ve put the new link in that thread, sorry!

How did you integrate Z906 into HA? Hardware mod?

Yes mainly hardware mod bot a lot of home assistant as well. I havent written a thorough guide yet but if your interested you could check out this reddit thread: https://www.reddit.com/r/hardwarehacking/comments/99eh5u/hacking_the_logitech_z906_speaker_system/?utm_source=reddit-android

Do you have more info now? That old reddit thread isn’t very helpful for a noob like me.

4 posts up has the solution that can still be used

Sorry, I meant on how to hack the Logitech z906 witn an esp, a guide or something that tells specifically which parts to use and how to connect them.

I have in my pipeline to write an extensive guide on how to hack the Logitech Z906 system with an ESP. I’m planning to write it this fall or winter so hopefully you can get more answers then!

That would be pretty great indeed, and very much appreciated.

Any updates on this project of yours?

Still working on the guide I’m afraid, it’s hard to find time. Currently I’m sorting out some bugs that got introduced when the software libraries used in the project got updated.

The plan is still to write it sometime this fall or winter. I’ll let you know in this threat the second it’s public.

Here’s hoping everything is well. This project of yours is highly anticipated. Good luck.