Sensor template goes to unavailable

So, up until recently (prob 6-ish weeks ago?) my media_state and media_type sensors were working fine:

Blockquote
media_type:
value_template: >
{% if states.media_player.xbox.attributes %}
{{ states.media_player.xbox.attributes.media_library_name }}
{% else %}
None
{% endif %}
media_state:
value_template: >
{% if states.media_player.xbox.state %}
{{ states.media_player.xbox.state }}
{% else %}
None
{% endif %}

However, now when the xbox media player goes unavailable (as in i switch it off for the night), the sensors go to unavailable too, instead of just “none”, and become unresponsive until I restart home assistant.

any ideas how to fix this?

You should use the template editor to see the state of the device when the device is off.

In python and jinja when a you use an if statement on a string with characters in it, the statement will return true.

My guess is that the state is ‘unavailable’, which is a populated string. This means your if statement is passing and you are throwing unavailable upwards.

This won’t explain why it’s becoming unresponsive. In fact, something else may be going on as well. Using the template sensor when it’s off should point you in the correct direction. Once you know how it behaves, you can fix your template.

So, the plot thickens. Turns out it’s the media player itself that is unavailable. On a fresh restart of home assistant it comes up when I switch the Xbox on, and works fine until I switch it off. Then it never comes out of the unavailable state. Anyone know how to fix this?

what component are you using?

fix might be too big a word for it, but ive templated (with a value_template and icon_template) that situation to be clear as what is happening:

48

then again, I dont have a media_player.xbox but use the xbox-live platform.
How did you realize the media_player?

That’s what I use too. I don’t have an xbox media player device either. It’s why I asked about the component he is using. Something is reporting an xbox media player, I’m wondering what it is.

Sorry for the late reply, the component is Plex. The Xbox media player is a Plex client

Does the xbox still report in plex?

Maybe this is a problem with plex atm?

Yeah, Xbox shows up in Plex. The Plex sensor in home assistant shows 1 user watching too

A yes, Plex. Can be a little bit confusing, nt to say frustrating. depending on the platform (and your setting in the Plex component of course)

you might try something like this:

- platform: template
  sensors:
    watching_mp_plex_safari:
      friendly_name: "Watching Media player Plex"
      value_template: >-
       {% if states.media_player.plex_safari.state == 'playing'%}
         {% for attr in states.media_player.plex_safari.attributes %}
           {%- if not attr=="friendly_name" and not attr=="unit_of_measurement"
                                           and not attr=="custom_ui_state_card" -%}
             {{attr}} {{states.media_player.plex_safari.attributes[attr]}}
           {%- endif %}
         {%- endfor -%}
       {% else %} Plex Safari is {{states.media_player.plex_safari.state}}
       {%- endif %}
    watching_mp_plex_web_safari:
      friendly_name: "Watching Media web player Plex"
      value_template: >-
       {% if states.media_player.plex_plex_web_safari.state == 'playing'%}
         {% for attr in states.media_player.plex_plex_web_safari.attributes %}
           {%- if not attr=="friendly_name" and not attr=="unit_of_measurement"
                                           and not attr=="custom_ui_state_card" -%}
             {{attr}} {{states.media_player.plex_plex_web_safari.attributes[attr]}}
           {%- endif %}
         {%- endfor -%}
       {% else %} Plex web Safari is {{states.media_player.plex_plex_web_safari.state}}
       {%- endif %}

    watching_plex:
      friendly_name: "Watching Plex"
      value_template: >-
           {% if not states.sensor.plex.state == '0' %}
             {% for attr in states.sensor.plex.attributes %}
               {%- if not attr=="friendly_name" and not attr=="unit_of_measurement"
                                            and not attr=="custom_ui_state_card"
                                            and not attr=="icon" -%}
                {{attr}} is watching {{states.sensor.plex.attributes[attr]}}
               {%- endif %}
             {%- endfor -%}
           {% else %} {{states.sensor.plex.state}} playing
           {% endif %}

and see if your Xbox shows up again?

Did you ever find a solution or rectify this issue? I still seem to be having it on 69.1

I did, let me quote myself:

There was a change to the Plex plugin about 2 months ago to remove unavailable players - https://github.com/home-assistant/home-assistant/pull/13156

Some new config variables were added too - https://github.com/home-assistant/home-assistant.github.io/commit/48d08be9078667d31eaa83a3672f7edfa23678ef

So, I added remove_unavailable_clients: false to my config file under the Plex component, and now I’m all good. The automations work every time now, not just the first time