Afficher le state d'une entity sur ecran oled esphome

Afficher le state d’une entity sur ecran oled esphome

Bonjour,

Je cherche a afficher l’état d’une entité sur un ecran ESPHOME.

j’ai créé dans mon configuration.yml de HA ça :

template:
      - name: "Spotify Song"
        state: "{{ state_attr('media_player.spotify_nicolasdevouge', 'media_title') }}"
        unique_id: spotify_song

      - name: "Spotify Artist"
        state: "{{ state_attr('media_player.spotify_nicolasdevouge', 'media_artist') }}"
        unique_id: spotify_artist

Et dans l’outil de dév HA c’est bien reconnu, le titre de la chanson est dans la colonne Etat :

Ensuite dans ESPHOME j’ai ce code :

esphome:
  name: esphome-spotify-info
  friendly_name: Spoti-infos

esp8266:
  board: nodemcuv2

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "pouetpouet"

ota:

font:

  # gfonts://family[@weight]
  - file: "gfonts://Roboto"
    id: roboto
    size: 10

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esphome-Web-23Bc6D"
    password: "4eluVHoH4x80"

captive_portal:
web_server:

time:
  - platform: homeassistant
    id: homeassistant_time
    timezone: Europe/Paris

text_sensor:
  - platform: homeassistant
    id: spotify_song
    entity_id: sensor.spotify_song
    internal: true
    name: "spotify_song"
 
  - platform: homeassistant
    id: spotify_artist
    entity_id: sensor.spotify_artist
    internal: true

i2c:
  sda: D2
  scl: D1
  scan: true
  id: bus_a

display:
  - platform: ssd1306_i2c
    model: "SSD1306 64x48"
    #reset_pin: D0
    address: 0x3C
    lambda: |-
      if (id(spotify_song).has_state()) {
          it.printf(0, 0, id(roboto), "%s", id(spotify_song).state.c_str());
      } else {
          it.printf(0, 0, id(roboto), "pas de status spotify");
        }
      it.strftime(0, 30, id(roboto), TextAlign::BASELINE_LEFT, "%H:%M", id(homeassistant_time).now());

Et forcément ca m’affiche « Pas de statut Spotify »

Donc la conf ecran est OK, j’arrive juste pas à récupérer le contenu de l’entity HA dans ESPHome 🙁

Pouvez-vous m’aider ?

Merci bcp !

Ma configuration


System Information

version core-2023.8.4
installation_type Home Assistant Container
dev false
hassio false
docker true
user root
virtualenv false
python_version 3.11.4
os_name Linux
os_version 6.2.0-26-generic
arch x86_64
timezone Europe/Paris
config_dir /config
Home Assistant Community Store
GitHub API ok
GitHub Content ok
GitHub Web ok
GitHub API Calls Remaining 4996
Installed Version 1.32.1
Stage running
Available Repositories 1274
Downloaded Repositories 4
Home Assistant Cloud
logged_in false
can_reach_cert_server ok
can_reach_cloud_auth ok
can_reach_cloud ok
Dashboards
dashboards 1
resources 0
views 4
mode storage
Recorder
oldest_recorder_run 25 août 2023 à 12:41
current_recorder_run 25 août 2023 à 14:41
estimated_db_size 37.37 MiB
database_engine sqlite
database_version 3.41.2
Spotify
api_endpoint_reachable ok
___

Salut,

il n’y a pas beaucoup d’infos sur le « has_state », je ne sais pas par exemple si ça marche avec un text sensor de la même façon qu’avec un sensor.
As-tu essayé sans, juste en affichant le titre?
Et voir par après ce qu’il se passe si jamais il n’y a pas d’état dans HA?

Merci pour ta réponse.

J’ai testé comme ça :


esphome:
  name: esphome-spotify-info
  friendly_name: Spoti-infos

esp8266:
  board: nodemcuv2

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "3FIrKL8/ll093nbgtFmRkVyM+6YZI8SOMSGJDRqtnj4="

ota:

font:

  # gfonts://family[@weight]
  - file: "gfonts://Roboto"
    id: roboto
    size: 10

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esphome-Web-23Bc6D"
    password: "4eluVHoH4x80"

captive_portal:
web_server:

time:
  - platform: homeassistant
    id: homeassistant_time
    timezone: Europe/Paris

text_sensor:
  - platform: homeassistant
    id: spotify_song
    entity_id: sensor.spotify_song
    internal: true
    name: "spotify_song"
 
  - platform: homeassistant
    id: spotify_artist
    entity_id: sensor.spotify_artist
    internal: true

i2c:
  sda: D2
  scl: D1
  scan: true
  id: bus_a

display:
  - platform: ssd1306_i2c
    model: "SSD1306 64x48"
    #reset_pin: D0
    address: 0x3C
    lambda: |-

      it.printf(0, 0, id(roboto), "%s", id(spotify_song).state);
      std::string val = to_string(id(spotify_song).state);
      it.printf(4, 4, id(roboto), "%s",val);

Je pene que le contenu de state n’est pas bon, ça me renvoi ça :

/config/esphome-web-23bc6d.yaml: In lambda function:
/config/esphome-web-23bc6d.yaml:67:31: warning: format '%s' expects argument of type 'char*', but argument 6 has type 'std::string' {aka 'std::__cxx11::basic_string<char>'} [-Wformat=]
   67 |       it.printf(0, 0, id(roboto), "%s", id(spotify_song).state);
      |                               ^~~~
/config/esphome-web-23bc6d.yaml:69:31: warning: format '%s' expects argument of type 'char*', but argument 6 has type 'std::string' {aka 'std::__cxx11::basic_string<char>'} [-Wformat=]
   69 |       it.printf(4, 4, id(roboto), "%s",val);
      |                               ^~~~