How to display Sonos Volume on a LCD display 2004 IC2? Got screen working just need HA help?

esphome:
  name: sonos_display
  platform: ESP8266
  board: d1_mini_pro

wifi:
  ssid: "WHATEVER"
  password: "*********"

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: "*******"

ota:
  password: "********"


sensor:
  - platform: homeassistant
    id: office_temperature
    entity_id: media_player.tv_speakers
    internal: true

i2c:
  sda: D2
  scl: D1

display:
  - platform: lcd_pcf8574
    dimensions: 16x2
    address: 0x27
    lambda: |-
     // Print inside temperature (from homeassistant sensor)
      if (id(office_temperature).state == id(office_temperature).state) {
        it.printf(0, 0, id(office_temperature).state);
      } else {
        it.print(0, 0, "waiting");
      }

How do i change this code to display my current volume on my Sonos, at present on the LCD display (2004) I get waiting so I know the config for that works.

To confirm I have gone into integration and added the sonos_display in there.

esphome:
  name: sonos_display
  platform: ESP8266
  board: d1_mini_pro

wifi:
  ssid: "WHATEVER"
  password: "WHATEVER"

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: "XXXXXXXXX"

ota:
  password: "XXXXXXXXX"

text_sensor:
  - platform: homeassistant
    id: sonos_volume
    entity_id: sensor.media_volume
    internal: true


i2c:
  sda: D2
  scl: D1

display:
  - platform: lcd_pcf8574
    dimensions: 16x2
    address: 0x27
    lambda: |-
      it.printf(0, 0, id(sonos_volume).state.c_str());
          
sensor:
  - platform: template
    sensors:
      media_volume:
        friendly_name: "media_volume"
        value_template: "{{ state_attr('media_player.tv_speakers', 'volume_level') }}"

Why does this work

it.printf(id(sonos_volume).state.c_str());

Where as this fails?

it.printf("Volume", id(sonos_volume).state.c_str());

error when doing the compile

src/main.cpp: In lambda function:
src/main.cpp:126:51: warning: too many arguments for format [-Wformat-extra-args]
it.printf(“Vol”, sonos_volume->state.c_str());

one more question for the experts.

how do I display sonos_volume which come out like this 0.42 but have it displayed as this instead 42%

I know its some formatting command in it.printf but what???

This was the answer

it.printf(“volume: %s%%”, id(sonos_volume).state.c_str());

the %s added the volume from HA and the %% added a percentage sign after the volume to make it look tidy.

one more question for the experts.

how do I display sonos_volume which come out like this 0.42 but have it displayed as this instead 42%

I know its some formatting command in it.printf but what???

did it in home assistant was easier

This is exactly what I want to do… but I can’t get the sensor part to work. I am assuming this is for the config.yaml in HA. The ESPHome Editor says all it well… but the compiler craps out on me. see errors below

esphome:
  name: widget-1

esp32:
  board: esp32dev
  framework:
    type: arduino


# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: "XXX"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Widget-1 Fallback Hotspot"
    password: "XXX"

captive_portal:

text_sensor:
  - platform: homeassistant
    id: sonos_volume
    entity_id: sensor.media_volume
    internal: true

font:
  - file: 'ArchivoBlack-Regular.ttf'
    id: font1
    size: 20
    
    # Example configuration entry
i2c:
  sda: 4
  scl: 15

display:
  - platform: ssd1306_i2c
    model: "SSD1306 128x64"
    reset_pin: 16
    address: 0x3C
    lambda: |-
      it.printf(0, 0, id(sonos_volume).state.c_str());
      

and the line from configuration.yaml:

template:
   - sensor:
      - name: "media_volume"
        value_template: "{{ state_attr('media_player.tv_speakers', 'volume_level') }}"

Annoying Errors…

INFO Reading configuration /config/esphome/widget-1.yaml...
WARNING GPIO4 is a Strapping PIN and should be avoided.
Attaching external pullup/down resistors to strapping pins can cause unexpected failures.
See https://esphome.io/guides/faq.html#why-am-i-getting-a-warning-about-strapping-pins
WARNING GPIO4 is a Strapping PIN and should be avoided.
Attaching external pullup/down resistors to strapping pins can cause unexpected failures.
See https://esphome.io/guides/faq.html#why-am-i-getting-a-warning-about-strapping-pins
WARNING GPIO15 is a Strapping PIN and should be avoided.
Attaching external pullup/down resistors to strapping pins can cause unexpected failures.
See https://esphome.io/guides/faq.html#why-am-i-getting-a-warning-about-strapping-pins
WARNING GPIO15 is a Strapping PIN and should be avoided.
Attaching external pullup/down resistors to strapping pins can cause unexpected failures.
See https://esphome.io/guides/faq.html#why-am-i-getting-a-warning-about-strapping-pins
INFO Generating C++ source...
INFO Compiling app...
Processing widget-1 (board: esp32dev; framework: arduino; platform: platformio/espressif32 @ 3.5.0)
--------------------------------------------------------------------------------
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
Dependency Graph
|-- <AsyncTCP-esphome> 1.2.2
|-- <WiFi> 1.0
|-- <FS> 1.0
|-- <Update> 1.0
|-- <ESPAsyncWebServer-esphome> 2.1.0
|   |-- <AsyncTCP-esphome> 1.2.2
|-- <DNSServer> 1.1.0
|-- <ESPmDNS> 1.0
|-- <Wire> 1.0.1
Compiling /data/widget-1/.pioenvs/widget-1/src/main.cpp.o
Compiling /data/widget-1/.pioenvs/widget-1/FrameworkArduino/esp32-hal-misc.c.o
Compiling /data/widget-1/.pioenvs/widget-1/FrameworkArduino/esp32-hal-psram.c.o
Compiling /data/widget-1/.pioenvs/widget-1/FrameworkArduino/esp32-hal-rmt.c.o
/config/esphome/widget-1.yaml: In lambda function:
/config/esphome/widget-1.yaml:52:50: error: no matching function for call to 'esphome::display::DisplayBuffer::printf(int, int, const char*)'
       it.printf(0, 0, id(sonos_volume).state.c_str());
                                                  ^
In file included from src/esphome.h:17:0,
                 from src/main.cpp:3:
src/esphome/components/display/display_buffer.h:195:8: note: candidate: void esphome::display::DisplayBuffer::printf(int, int, esphome::display::Font*, esphome::Color, esphome::display::TextAlign, const char*, ...)
   void printf(int x, int y, Font *font, Color color, TextAlign align, const char *format, ...)
        ^
src/esphome/components/display/display_buffer.h:195:8: note:   candidate expects 6 arguments, 3 provided
src/esphome/components/display/display_buffer.h:207:8: note: candidate: void esphome::display::DisplayBuffer::printf(int, int, esphome::display::Font*, esphome::Color, const char*, ...)
   void printf(int x, int y, Font *font, Color color, const char *format, ...) __attribute__((format(printf, 6, 7)));
        ^
src/esphome/components/display/display_buffer.h:207:8: note:   candidate expects 5 arguments, 3 provided
src/esphome/components/display/display_buffer.h:218:8: note: candidate: void esphome::display::DisplayBuffer::printf(int, int, esphome::display::Font*, esphome::display::TextAlign, const char*, ...)
   void printf(int x, int y, Font *font, TextAlign align, const char *format, ...) __attribute__((format(printf, 6, 7)));
        ^
src/esphome/components/display/display_buffer.h:218:8: note:   candidate expects 5 arguments, 3 provided
src/esphome/components/display/display_buffer.h:228:8: note: candidate: void esphome::display::DisplayBuffer::printf(int, int, esphome::display::Font*, const char*, ...)
   void printf(int x, int y, Font *font, const char *format, ...) __attribute__((format(printf, 5, 6)));
        ^
src/esphome/components/display/display_buffer.h:228:8: note:   candidate expects 4 arguments, 3 provided
Compiling /data/widget-1/.pioenvs/widget-1/FrameworkArduino/esp32-hal-sigmadelta.c.o
Compiling /data/widget-1/.pioenvs/widget-1/FrameworkArduino/esp32-hal-spi.c.o
/data/cache/platformio/packages/framework-arduinoespressif32/cores/esp32/esp32-hal-spi.c: In function 'spiTransferBytesNL':
/data/cache/platformio/packages/framework-arduinoespressif32/cores/esp32/esp32-hal-spi.c:922:39: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
                 uint8_t * last_out8 = &result[c_longs-1];
                                       ^
/data/cache/platformio/packages/framework-arduinoespressif32/cores/esp32/esp32-hal-spi.c:923:40: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
                 uint8_t * last_data8 = &last_data;
                                        ^
Compiling /data/widget-1/.pioenvs/widget-1/FrameworkArduino/esp32-hal-time.c.o
*** [/data/widget-1/.pioenvs/widget-1/src/main.cpp.o] Error 1
========================== [FAILED] Took 2.64 seconds ==========================