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.
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?
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.
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 %}
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