ESPhome expert - help with simple if condition

esphome:
  name: sonos_display_4_lines
  platform: ESP8266
  board: nodemcuv2

wifi:
  ssid: "bertybassett_2Ghz"
  password: "Whatever"

# Enable logging
logger:

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

ota:
  password: "WhatEver"

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

  - platform: homeassistant    
    id: sonos_artist
    entity_id: sensor.sonos_artist
    internal: true

  - platform: homeassistant    
    id: sonos_album
    entity_id: sensor.sonos_album
    internal: true    

  - platform: homeassistant    
    id: sonos_track
    entity_id: sensor.sonos_track
    internal: true
 
  - platform: homeassistant    
    id: sonos_playing
    entity_id: media_player.tv_sonos
    internal: true


i2c:
  sda: D2
  scl: D1

display:
  - platform: lcd_pcf8574
    dimensions: 20x4
    address: 0x27
    id: my_lcd
    lambda: |-
      if XXXXXXXXXX
        id(my_lcd).backlight();
        fflush(stdout);
        it.printf(0, 0, "Volume %s%%", id(sonos_volume).state.c_str());
        it.printf(0, 1, "Artist %s", id(sonos_artist).state.c_str());
        it.printf(0, 2, "Album  %s", id(sonos_album).state.c_str());
        it.printf(0, 3, "Track  %s", id(sonos_track).state.c_str());
      } else { 
        id(my_lcd).no_backlight();
      }

I want to turn on back light only when media_player.tv_sonos = playing. What is the magic if condition that I need to use?

could it be this simple

if sonos_playing = playing

1 Like

Set up a homeassistant sensor for the state of media_player.tv_sonos and use this

2 Likes

Ok will do thanks

  • platform: template
    sensors:
    sonos_playing:
    friendly_name: “sonos_playing”
    value_template: ‘{% if state(“media_player.tv_sonos”) == “playing”%}on{% else %}on{% endif %}’

what is wrong with this?

I get sensor.sonos_playing unavailable friendly_name: sonos_playing

Also got this in ESPhome

display:
  - platform: lcd_pcf8574
    dimensions: 20x4
    address: 0x27
    id: my_lcd
    lambda: |-
      if (id(sonos_playing).state = "on") 
        id(my_lcd).backlight();
        fflush(stdout);
        it.printf(0, 0, "Volume %s%%", id(sonos_volume).state.c_str());
        it.printf(0, 1, "Artist %s", id(sonos_artist).state.c_str());
        it.printf(0, 2, "Album  %s", id(sonos_album).state.c_str());
        it.printf(0, 3, "Track  %s", id(sonos_track).state.c_str());
      } else { 
        id(my_lcd).no_backlight();
      }

here is the answer for any one slightly interested

HA configuration.yaml

binary_sensor:
      sonos_playing:
        friendly_name: "Is sonos_playing"
        value_template: "{{ is_state('media_player.tv_sonos', 'playing' ) }}"

ESPhome

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

  - platform: homeassistant    
    id: sonos_artist
    entity_id: sensor.sonos_artist
    internal: true

  - platform: homeassistant    
    id: sonos_album
    entity_id: sensor.sonos_album
    internal: true    

  - platform: homeassistant    
    id: sonos_track
    entity_id: sensor.sonos_track
    internal: true

binary_sensor:
  - platform: homeassistant    
    id: sonos_playing
    entity_id: binary_sensor.sonos_playing
    internal: true
    
    

i2c:
  sda: D2
  scl: D1

display:
  - platform: lcd_pcf8574
    dimensions: 20x4
    address: 0x27
    id: my_lcd
    lambda: |-
      if(id(sonos_playing).state) {
        id(my_lcd).backlight();
        fflush(stdout);
        it.printf(0, 0, "Volume %s%%", id(sonos_volume).state.c_str());
        it.printf(0, 1, "Artist %s", id(sonos_artist).state.c_str());
        it.printf(0, 2, "Album  %s", id(sonos_album).state.c_str());
        it.printf(0, 3, "Track  %s", id(sonos_track).state.c_str());
      } else {
        id(my_lcd).no_backlight();
      }
1 Like