How to only show the first state attribute in a list

I’m trying to populate only the first state in an attribute that contains a list.

State Attributes for media_player.living_room

volume_level: 0.07
is_volume_muted: false
media_content_id: >-
  x-sonos-spotify:spotify%3atrack%3a3EWMoDIm6lzuR0zQKtuCJX?sid=12&flags=8224&sn=4
media_content_type: music
media_duration: 197
media_position: 9
media_position_updated_at: '2021-05-12T13:49:08.524206+00:00'
media_title: Up Down (feat. Florida Georgia Line)
media_artist: Morgan Wallen
media_album_name: If I Know Me
shuffle: true
repeat: 'off'
sonos_group:
  - media_player.drew_s_desk
  - media_player.living_room
  - media_player.kitchen_2
queue_position: 1
friendly_name: Living Room
entity_picture: >-
  /api/media_player_proxy/media_player.living_room?token=c72a0fcf49be6294e20dabed4d2cf1d2ce8458ac4307863e9ac87ff6ac3c8ac2&cache=30a3b65cd5bea8db
supported_features: 457279

My Sensor configuration

- platform: template
  sensors:
    sonos_master:
      entity_id: media_player.living_room
      value_template: "{{ state_attr('media_player.living_room', 'sonos_group') }}"

This is what the sensor shows as its state

['media_player.drew_s_desk', 'media_player.living_room', 'media_player.kitchen_2']

What I’d like it to show is only the first state attribute in the list, in this example: media_player.drew_s_desk.

Thank you in advance for any help

Adding .0 will retrun the first item in a list

- platform: template
  sensors:
    sonos_master:
      entity_id: media_player.living_room
      value_template: "{{ state_attr('media_player.living_room', 'sonos_group').0 }}"
2 Likes

Perfect! Thank you very much for the quick response and super simple resolution. Much appreciated.

1 Like

No problem. For future reference, items in a list are indexed [0, 1, 2, 3... n].

1 Like

Hi, what if the state, is in a “level” further down, i.e. ( there are 4 lists in the attributes )

list:
  '1':
    cap: '2'
    max_rate: 1000
    link_rate: 1000
    brown: '0'
    brown_len: '0'
    blue: '0'
    blue_len: '0'
    green: '0'
    green_len: '0'
    orange: '0'
    orange_len: '0'
    linkrecover: '1'
    timeout: '1'
    state: true
  '2':
    cap: '2'
    max_rate: 1000
    link_rate: 1000
    brown: '0'
    brown_len: '0'
    blue: '0'
    blue_len: '0'
    green: '0'
    green_len: '0'
    orange: '0'
    orange_len: '0'
    linkrecover: '1'
    timeout: '1'
    state: true
  '3':
    cap: '2'
    max_rate: 1000
    link_rate: 0
    brown: '0'
    brown_len: '0'
    blue: '0'
    blue_len: '0'
    green: '0'
    green_len: '0'
    orange: '0'
    orange_len: '0'
    linkrecover: '1'
    timeout: '1'
    state: false
  '4':
    cap: '2'
    max_rate: 1000
    link_rate: 0
    brown: '0'
    brown_len: '0'
    blue: '0'
    blue_len: '0'
    green: '0'
    green_len: '0'
    orange: '0'
    orange_len: '0'
    linkrecover: '1'
    timeout: '1'
    state: false
device_class: connectivity
icon: mdi:ethernet-cable

I have no idea how to create a ( 4) template-sensor based upon the state in each (sub-lists)
It’s the 4 Lan-Ports of my Router

Edit: this gives my the whole dict: ( 1 )

{{ state_attr('binary_sensor.asusr_port_status_lan', 'list' ).1 }}

Never mind , got it ( sometimes it helps writing down your thoughts ) :slight_smile:

{{ state_attr('binary_sensor.asusr_port_status_lan', 'list' ).1.state }}

:frowning: , apparently above only works in Dev-Tools , not in template-card

— Somehow i manage to end up where i wanted, Icon turns green, when cable( with device in other end ) is plugged in the Router-port (state: true)… if cable “falls out/fails” or device/devices(all) in other end goes offline,( state: false) then color “falls back” to default-grey
13.02.2023_11.50.20_REC

Edit: must be do to the device_class

1 Like