ESPHome Yaml for Display - Media player state

Hi all,

I know very little about the configuration required to achieve this and project to starting to loose all the fun and quickly becoming a chore.

I have a touch panel coming along nicely and managed to get most features and functions I require up and running, thanks mainly to this report i found using an identical device:
repo: https://github.com/strange-v/ha_deck

I trying to get a button checked status to be true when a media device is playing.
I have this working for light a connected via HA, when the light is on the button is checked.

You might laugh at what i have written here as i have very little experience with yaml / esphome and now need some assistance if anyone can provide the yaml required that would be great.

Current yaml snippets in question:

sensor:
  - platform: homeassistant
    id: den_media
    name: "Den Media State"
    entity_id: media_player.den

hd_device_wt32s3_86s:
  id: device
  brightness: 75  

ha_deck:
  id: deck
  main_screen: ${SCREEN_MAIN}
  inactivity:
    period: 120 # seconds
    blank_screen: true
  on_inactivity_change:
    lambda:  |-
      if (x) {
        id(device).set_brightness(id(inactive_screen_brightness).state);
      } else {
        id(device).set_brightness(id(screen_brightness).state);
      }
  screens:
    - name: ${SCREEN_MAIN}
      widgets:
        - type: button
          position: 108, 316
          text: Play
          icon: 󰐎
          toggle: false
          enabled: return true;
          checked: return id(den_media).state;

Basically I need the “playing” / “paused” state reported by the sensors to be converted to a state that “checked” will accepted

This all complies fine but then in the logs i get:
[14:28:36][W][homeassistant.sensor:015]: ‘media_player.den’: Can’t convert ‘playing’ to number!
Not sure why this would be number, assumer Boolean?

Any assistance at all with this would be great, copoilot / chatgpt where useless.

Please start by formatting your code

Code_format

This is because sensor: is ALWAYS a number. The only way to import the sensor from HA is to use text_sensor: - e.g:

text_sensor:
  - platform: homeassistant
    id: den_media
    name: "Den Media State"
    entity_id: media_player.den

Now - once you have done that you can either check the value returned directly in your lambda - that is compare for “playing” etc, or create a template binary sensor that will update when the text sensor updates.

If you need assistance with the latter let us know.

1 Like

Thanks Darly! took me awhile to figure out the lambda (which looks like nothing now looking back) but all is working now. thank you for solving this!
Now to focus on the fun stuff like fix the button size and positions :confused:

Sensor setup:

text_sensor:   
  - platform: homeassistant
    id: den_media
    name: "Den Media State"
    entity_id: media_player.den

Button:

       - type: button
          position: 108, 316
          text: Play
          icon: 󰐎
          toggle: false
          enabled: return true;
          checked: |-
            lambda: return id(den_media).state == "playing";
          on_click: 
            homeassistant.service:
              service: media_player.media_play_pause
              data:
                entity_id: media_player.den
1 Like