Reduce 5 almost identical sensor templates to 1?

HI,

For my plex sensors, I use these 5 individual but almost the same template sensors:

  watching_plex_safari:
    friendly_name: 'Plex Safari'
    value_template: >
      {% if states('media_player.plex_safari') %}
        {% if states('media_player.plex_safari')in ['playing','idle','paused','stopped']%} 
           {{states('media_player.plex_safari')}}
        {%  endif %}
      {% else %} Offline
      {% endif %}
    entity_picture_template: >
     {% if states('media_player.plex_safari') == 'playing' %}
        {{state_attr('media_player.plex_safari','entity_picture')}}
     {% else %} /local/various/plex.png
     {% endif %}

  watching_plex_ipad_red:
    friendly_name: 'Plex iPad red'
    value_template: >
      {% if states('media_player.plex_ipad_red') %}
        {% if states('media_player.plex_ipad_red')in ['playing','idle','paused','stopped']%} 
           {{states('media_player.plex_ipad_red')}}
        {%  endif %}
      {% else %} Offline
      {% endif %}
    entity_picture_template: >
     {% if states('media_player.plex_ipad_red') == 'playing' %}
        {{state_attr('media_player.plex_ipad_red','entity_picture')}}
     {% else %} /local/various/plex.png
     {% endif %}

  watching_plex_telefoon:
    friendly_name: 'Plex Telefoon'
    value_template: >
      {% if states('media_player.plex_telefoon') %}
        {% if states('media_player.plex_telefoon')in ['playing','idle','paused','stopped']%} 
           {{states('media_player.plex_telefoon')}}
        {%  endif %}
      {% else %} Offline
      {% endif %}
    entity_picture_template: >
     {% if states('media_player.plex_telefoon') == 'playing' %}
        {{state_attr('media_player.plex_telefoon','entity_picture')}}
     {% else %} /local/various/plex.png
     {% endif %}

  watching_plex_xbox:
    friendly_name: 'Plex Xbox-one'
    value_template: >
      {% if states('media_player.plex_xbox') %}
        {% if states('media_player.plex_xbox')in ['playing','idle','paused','stopped']%} 
           {{states('media_player.plex_xbox')}}
        {%  endif %}
      {% else %} Offline
      {% endif %}
    entity_picture_template: >
     {% if states('media_player.plex_xbox') == 'playing' %}
        {{state_attr('media_player.plex_xbox','entity_picture')}}
     {% else %} /local/various/plex.png
     {% endif %}

  watching_plex_auditorium:
    friendly_name: 'Plex Auditorium'
    value_template: >
      {% if states('media_player.plex_auditorium') %}
        {% if states('media_player.plex_auditorium')in ['playing','idle','paused','stopped']%} 
           {{states('media_player.plex_auditorium')}}
        {%  endif %}
      {% else %} Offline
      {% endif %}
    entity_picture_template: >
     {% if states('media_player.plex_auditorium') == 'playing' %}
        {{state_attr('media_player.plex_auditorium','entity_picture')}}
     {% else %} /local/various/plex.png
     {% endif %}

As you can see the only difference is in the name, and the entities named in the templates.

Could I compress this to 1 sensor creating these automatically, with a for loop
referencing the names?

for plex entities with name in [‘safari’,‘iPad_red’,‘telefoon’,‘xbox’,‘auditorium’]

create sensor:

  watching_plex_{{name}}:
    friendly_name: 'Plex {{name}}'
    value_template: >
      {% if states('media_player.plex_{{name}}') %}
        {% if states('media_player.plex_{{name}} ') in ['playing','idle','paused','stopped']%} 
           {{states('media_player.plex_{{name}} ')}}
        {%  endif %}
      {% else %} Offline
      {% endif %}
    entity_picture_template: >
     {% if states('media_player.plex_{{name}}') == 'playing' %}
        {{state_attr('media_player.plex_{{name}} '','entity_picture')}}
     {% else %} /local/various/plex.png
     {% endif %} 

above won’t work since it uses a template in a template, but is only to try to show what i am looking for.

@petro what would you suggest?

thanks for having a look

I don’t think there is anything you can do. Template switches can’t be globbed. I mean you could potentially turn them into python scripts but I don’t think you can have a script return a result to a template. At least I’ve never tried to. @Tinkerer have any extra wisdom to add?

I want to do something very similar.

It wouldn’t be too hard to make an external python script and jinja template that could expand a file of sensors which would get included by the config.yaml. You could just update the script and refresh its output each time you wanted to create or modify sensors. Although, having a built in way to do this would be handy.

I mean, if you go to a script length then you don’t need to do any of that extra jazz.

Just make an automation that fires on state changes for your entity. Then use the python_script component to create and update the sensors.