Set HA sensor value as value on rotary encoder

I’d like to use a rotary encoder as a volume knob.

I have the following sensor that gets the volume level from my sonos:

  - platform: homeassistant
    id: media_volume
    entity_id: media_player.symfonisk
    attribute: volume_level
    internal: true   

From the documentation I understood that I can set the certain value to a rotary encoder. However, If I add this:

    on_value:
      then:
      - sensor.the_encoder.publish:
        id: the_encoder
        value: !lambda '(id(media_volume).state*100);'

An error occurs:

Unable to find action with the name ‘sensor.the_encoder.publish’.

Even though I copied it literally from the documentation, so I am a bit confused what I’m doing wrong here. could anyone tell me what I am doing wrong and how I can fix it? Thanks!

Are you trying to push values to a esphome (internal) sensor called media_volume? That will not work to change the volume for a ha connected device :thinking:

The best solution might be just to de- or increment the volume with ha service calls directly with a code like this:

sensor:
  - platform: rotary_encoder
    ...
    on_clockwise:
      - homeassistant.service:
          service: media_player.volume_up
          data:
            entity_id: media_player.sonos_xyz
    on_anticlockwise:
      - homeassistant.service:
          service: media_player.volume_down
          data:
            entity_id: media_player.sonos_xyz

Actually I think he is trying to synchronise the volume in HA to the number in the rotary encoder. Otherwise when you, for example, reboot the esp, it resets the volume level.

That is exactly what I would like to achieve. The knob already works, but it stands on my desk. But if I change the volume from HA itself for example, then the value of the rotary encoder is not the same anymore as the volume level of my Sonos.

Where are you using this value?
Is it on a display?

Perhaps you could set up a number in esphome instead that is updated with the value from the speaker and use this instead of the encoder value?

I have a screen salvaged from an ender 3 printer, which also includes a rotary encoder. I am programming it to use it as an interface for Home-Assistant on my desk. So I can quickly change the volume and see which song is playing.

I’ve come to the conclusion that what I initially had in mind is not useful and necessary. In the future I would like to use the rotary encoder for other tasks, like select inputs, playlists, etc. That won’t work if the encoder has a certain value set or is continuously updated.

That’s why the best is to not even “try” to synchronize the two entities but just use the one “truth” (the ha volume) and only in-/decrement it with (anti)clockwise values from the rotary encoder like in the example I posted.

For that reason don’t try to have two truth’ but go with a solution that just works :tm: like presented earlier

Again this just raises complexity unnecessary. Because this just works :tm: with ha it’s no problem to show the volume level directly on the display and use the rotary encoder only to send service calls to de/increment volume. Winner winner chicken dinner :chicken:

Might be worth a shot to see what other people doing already - for example:

or: SONOS/SYMFONISK Display example - ControllerX

1 Like

Quite right. IIRC on_clockwise was not a thing when I first set mine up.