Sennevds / media_player.template

Template media_player for Home Assistant
MIT License
126 stars 23 forks source link

[FEATURE REQUEST] Ability to add multiple entity ids to services. #43

Closed maxi1134 closed 1 year ago

maxi1134 commented 1 year ago

First of all, I wanna say thanks for this card! It has been wonderful.

I am now trying to create a template media_player that would basically control the media_players in the room in which I am.

I tried using the following with no success: [see code]

Would it be possible to try and implement this possibility? It would be much appreciated!

  template_following_media_player:
    friendly_name: Template Following Media Player
    device_class: speaker
    value_template: >
            {% set ns = namespace( cast_entity = "media_player.livingroom_chromecast", template_entity = "" ) %}
            {% set ns.final_entity = ns.cast_entity %}   {# Declare Variables#}                
            {% for media_player in states.media_player %} 
              {% if "plex" in media_player.entity_id and media_player.state != "unavailable" %}
                {% set entity = media_player.entity_id %}
                {% set media_title = state_attr( entity, 'media_title') %}
                {% set media_series_title = state_attr( entity, 'media_series_title') %}
                {% set media_series_episode = state_attr( entity, 'media_espisode') %}
                {% set media_series_season = state_attr( entity, 'media_season') %}
                  {% if state_attr( ns.final_entity, 'media_title') %}
                    {% if  state_attr( ns.cast_entity, 'media_title') in ( media_title | string )%}
                    {% set ns.template_entity = entity %}
                    {% endif %}
                  {% elif state_attr( ns.final_entity, 'media_series_title') %}
                    {% if  state_attr( ns.cast_entity, 'media_series_title') in ( media_series_title | string )%}
                    {% set ns.template_entity = entity %}
                    {% endif %}
                  {% endif %}                      
              {% endif %}                  
            {% endfor %}                
            {% if ns.template_entity == "" %}
                {% set ns.template_entity = ns.cast_entity %}
            {% endif %}
            {% if ns.template_entity != ns.cast_entity %}                  
              {{states(ns.template_entity)}}
            {% elif state_attr( ns.cast_entity, 'media_title')  %} 
              {{states(ns.cast_entity)}}
            {% else %}
              off
            {% endif %}
    next:
      service: media_player.media_seek
      data_template:
        entity_id: >
            {% set location = states('sensor.maxi_location_v3') %}  
            {% set ns = namespace( entities = [], final_entities = []  )%}

            {% for media_player in states.media_player %} 
              {% if location in media_player.entity_id  and  "template" not in media_player.entity_id and "plex" not in media_player.entity_id and media_player.state != "unavailable" and media_player.state != "off" %}
                {% set entity = media_player.entity_id %}
                {% set media_title = state_attr( entity, 'media_title') %}
                {% set media_series_title = state_attr( entity, 'media_series_title') %}
                {% set media_series_episode = state_attr( entity, 'media_espisode') %}
                {% set media_series_season = state_attr( entity, 'media_season') %}
                {% set ns.entities = ns.entities + [entity] %}
              {% endif %}                  
            {% endfor %}  

            {% for entity in ns.entities %}  
            {% set is_muted = state_attr(entity, "is_volume_muted") %}
              {% if is_muted == false %}
                 {% if "speaker" in entity %}
                   {% set ns.final_entities = ns.final_entities + [entity] %}
                {% endif %}
                {% if ns.final_entities | length == 0 %}
                 {% if "chromecast" in entity %}
                   {% set ns.final_entities = ns.final_entities + [entity] %}
                {% endif %}
                {% endif %}
              {% endif %}
            {% endfor %}
            {% for entity in ns.final_entities %}
            - {{entity}}{% endfor %}
        seek_position: 56000
    turn_on:
      service: media_player.turn_on
      data_template:
        entity_id: >
            {% set location = states('sensor.maxi_location_v3') %}  
            {% set ns = namespace( entities = [], final_entities = []  )%}

            {% for media_player in states.media_player %} 
              {% if location in media_player.entity_id  and  "template" not in media_player.entity_id and "plex" not in media_player.entity_id and media_player.state != "unavailable" and media_player.state != "off" %}
                {% set entity = media_player.entity_id %}
                {% set media_title = state_attr( entity, 'media_title') %}
                {% set media_series_title = state_attr( entity, 'media_series_title') %}
                {% set media_series_episode = state_attr( entity, 'media_espisode') %}
                {% set media_series_season = state_attr( entity, 'media_season') %}
                {% set ns.entities = ns.entities + [entity] %}
              {% endif %}                  
            {% endfor %}  

            {% for entity in ns.entities %}  
            {% set is_muted = state_attr(entity, "is_volume_muted") %}
              {% if is_muted == false %}
                 {% if "speaker" in entity %}
                   {% set ns.final_entities = ns.final_entities + [entity] %}
                {% endif %}
                {% if ns.final_entities | length == 0 %}
                 {% if "chromecast" in entity %}
                   {% set ns.final_entities = ns.final_entities + [entity] %}
                {% endif %}
                {% endif %}
              {% endif %}
            {% endfor %}
            {% for entity in ns.final_entities %}
            - {{entity}}{% endfor %}
    stop:
      service: media_player.media_stop
      data_template:
        entity_id: >
            {% set location = states('sensor.maxi_location_v3') %}  
            {% set ns = namespace( entities = [], final_entities = []  )%}

            {% for media_player in states.media_player %} 
              {% if location in media_player.entity_id  and  "template" not in media_player.entity_id and "plex" not in media_player.entity_id and media_player.state != "unavailable" and media_player.state != "off" %}
                {% set entity = media_player.entity_id %}
                {% set media_title = state_attr( entity, 'media_title') %}
                {% set media_series_title = state_attr( entity, 'media_series_title') %}
                {% set media_series_episode = state_attr( entity, 'media_espisode') %}
                {% set media_series_season = state_attr( entity, 'media_season') %}
                {% set ns.entities = ns.entities + [entity] %}
              {% endif %}                  
            {% endfor %}  

            {% for entity in ns.entities %}  
            {% set is_muted = state_attr(entity, "is_volume_muted") %}
              {% if is_muted == false %}
                 {% if "speaker" in entity %}
                   {% set ns.final_entities = ns.final_entities + [entity] %}
                {% endif %}
                {% if ns.final_entities | length == 0 %}
                 {% if "chromecast" in entity %}
                   {% set ns.final_entities = ns.final_entities + [entity] %}
                {% endif %}
                {% endif %}
              {% endif %}
            {% endfor %}
            {% for entity in ns.final_entities %}
            - {{entity}}{% endfor %}
    turn_off:
      service: media_player.turn_off
      data_template:
        entity_id: >
            {% set location = states('sensor.maxi_location_v3') %}  
            {% set ns = namespace( entities = [], final_entities = []  )%}

            {% for media_player in states.media_player %} 
              {% if location in media_player.entity_id  and  "template" not in media_player.entity_id and "plex" not in media_player.entity_id and media_player.state != "unavailable" and media_player.state != "off" %}
                {% set entity = media_player.entity_id %}
                {% set media_title = state_attr( entity, 'media_title') %}
                {% set media_series_title = state_attr( entity, 'media_series_title') %}
                {% set media_series_episode = state_attr( entity, 'media_espisode') %}
                {% set media_series_season = state_attr( entity, 'media_season') %}
                {% set ns.entities = ns.entities + [entity] %}
              {% endif %}                  
            {% endfor %}  

            {% for entity in ns.entities %}  
            {% set is_muted = state_attr(entity, "is_volume_muted") %}
              {% if is_muted == false %}
                 {% if "speaker" in entity %}
                   {% set ns.final_entities = ns.final_entities + [entity] %}
                {% endif %}
                {% if ns.final_entities | length == 0 %}
                 {% if "chromecast" in entity %}
                   {% set ns.final_entities = ns.final_entities + [entity] %}
                {% endif %}
                {% endif %}
              {% endif %}
            {% endfor %}
            {% for entity in ns.final_entities %}
            - {{entity}}{% endfor %}
    volume_up:
      service: media_player.volume_up
      data_template:
        entity_id: >
            {% set location = states('sensor.maxi_location_v3') %}  
            {% set ns = namespace( entities = [], final_entities = []  )%}

            {% for media_player in states.media_player %} 
              {% if location in media_player.entity_id  and  "template" not in media_player.entity_id and "plex" not in media_player.entity_id and media_player.state != "unavailable" and media_player.state != "off" %}
                {% set entity = media_player.entity_id %}
                {% set media_title = state_attr( entity, 'media_title') %}
                {% set media_series_title = state_attr( entity, 'media_series_title') %}
                {% set media_series_episode = state_attr( entity, 'media_espisode') %}
                {% set media_series_season = state_attr( entity, 'media_season') %}
                {% set ns.entities = ns.entities + [entity] %}
              {% endif %}                  
            {% endfor %}  

            {% for entity in ns.entities %}  
            {% set is_muted = state_attr(entity, "is_volume_muted") %}
              {% if is_muted == false %}
                 {% if "speaker" in entity %}
                   {% set ns.final_entities = ns.final_entities + [entity] %}
                {% endif %}
                {% if ns.final_entities | length == 0 %}
                 {% if "chromecast" in entity %}
                   {% set ns.final_entities = ns.final_entities + [entity] %}
                {% endif %}
                {% endif %}
              {% endif %}
            {% endfor %}
            {% for entity in ns.final_entities %}
            - {{entity}}{% endfor %}
    volume_down:
      service: media_player.volume_down
      data_template:
        entity_id: >
            {% set location = states('sensor.maxi_location_v3') %}  
            {% set ns = namespace( entities = [], final_entities = []  )%}

            {% for media_player in states.media_player %} 
              {% if location in media_player.entity_id  and  "template" not in media_player.entity_id and "plex" not in media_player.entity_id and media_player.state != "unavailable" and media_player.state != "off" %}
                {% set entity = media_player.entity_id %}
                {% set media_title = state_attr( entity, 'media_title') %}
                {% set media_series_title = state_attr( entity, 'media_series_title') %}
                {% set media_series_episode = state_attr( entity, 'media_espisode') %}
                {% set media_series_season = state_attr( entity, 'media_season') %}
                {% set ns.entities = ns.entities + [entity] %}
              {% endif %}                  
            {% endfor %}  

            {% for entity in ns.entities %}  
            {% set is_muted = state_attr(entity, "is_volume_muted") %}
              {% if is_muted == false %}
                 {% if "speaker" in entity %}
                   {% set ns.final_entities = ns.final_entities + [entity] %}
                {% endif %}
                {% if ns.final_entities | length == 0 %}
                 {% if "chromecast" in entity %}
                   {% set ns.final_entities = ns.final_entities + [entity] %}
                {% endif %}
                {% endif %}
              {% endif %}
            {% endfor %}
            {% for entity in ns.final_entities %}
            - {{entity}}{% endfor %}
    mute:
      service: media_player.volume_mute
      data_template:
        entity_id: >
            {% set location = states('sensor.maxi_location_v3') %}  
            {% set ns = namespace( entities = [], final_entities = []  )%}

            {% for media_player in states.media_player %} 
              {% if location in media_player.entity_id  and  "template" not in media_player.entity_id and "plex" not in media_player.entity_id and media_player.state != "unavailable" and media_player.state != "off" %}
                {% set entity = media_player.entity_id %}
                {% set media_title = state_attr( entity, 'media_title') %}
                {% set media_series_title = state_attr( entity, 'media_series_title') %}
                {% set media_series_episode = state_attr( entity, 'media_espisode') %}
                {% set media_series_season = state_attr( entity, 'media_season') %}
                {% set ns.entities = ns.entities + [entity] %}
              {% endif %}                  
            {% endfor %}  

            {% for entity in ns.entities %}  
            {% set is_muted = state_attr(entity, "is_volume_muted") %}
              {% if is_muted == false %}
                 {% if "speaker" in entity %}
                   {% set ns.final_entities = ns.final_entities + [entity] %}
                {% endif %}
                {% if ns.final_entities | length == 0 %}
                 {% if "chromecast" in entity %}
                   {% set ns.final_entities = ns.final_entities + [entity] %}
                {% endif %}
                {% endif %}
              {% endif %}
            {% endfor %}
            {% for entity in ns.final_entities %}
            - {{entity}}{% endfor %}
    set_volume:
      service: media_player.volume_set
      data_template:
        entity_id: >
            {% set location = states('sensor.maxi_location_v3') %}  
            {% set ns = namespace( entities = [], final_entities = []  )%}

            {% for media_player in states.media_player %} 
              {% if location in media_player.entity_id  and  "template" not in media_player.entity_id and "plex" not in media_player.entity_id and media_player.state != "unavailable" and media_player.state != "off" %}
                {% set entity = media_player.entity_id %}
                {% set media_title = state_attr( entity, 'media_title') %}
                {% set media_series_title = state_attr( entity, 'media_series_title') %}
                {% set media_series_episode = state_attr( entity, 'media_espisode') %}
                {% set media_series_season = state_attr( entity, 'media_season') %}
                {% set ns.entities = ns.entities + [entity] %}
              {% endif %}                  
            {% endfor %}  

            {% for entity in ns.entities %}  
            {% set is_muted = state_attr(entity, "is_volume_muted") %}
              {% if is_muted == false %}
                 {% if "speaker" in entity %}
                   {% set ns.final_entities = ns.final_entities + [entity] %}
                {% endif %}
                {% if ns.final_entities | length == 0 %}
                 {% if "chromecast" in entity %}
                   {% set ns.final_entities = ns.final_entities + [entity] %}
                {% endif %}
                {% endif %}
              {% endif %}
            {% endfor %}
            {% for entity in ns.final_entities %}
            - {{entity}}{% endfor %}
        volume_level: "{{ volume }}"
    current_is_muted_template: "{{state_attr( 'media_player.livingroom_chromecast', 'is_volume_muted') }}"
    media_image_url_template: >
            {% set ns = namespace( cast_entity = "media_player.livingroom_chromecast", template_entity = "" ) %}
            {% set ns.final_entity = ns.cast_entity %}   {# Declare Variables#}                
            {% for media_player in states.media_player %} 
              {% if "plex" in media_player.entity_id and media_player.state != "unavailable" %}
                {% set entity = media_player.entity_id %}
                {% set media_title = state_attr( entity, 'media_title') %}
                {% set media_series_title = state_attr( entity, 'media_series_title') %}
                {% set media_series_episode = state_attr( entity, 'media_espisode') %}
                {% set media_series_season = state_attr( entity, 'media_season') %}
                  {% if state_attr( ns.final_entity, 'media_title') %}
                    {% if  state_attr( ns.cast_entity, 'media_title') in ( media_title | string )%}
                    {% set ns.template_entity = entity %}
                    {% endif %}
                  {% elif state_attr( ns.final_entity, 'media_series_title') %}
                    {% if  state_attr( ns.cast_entity, 'media_series_title') in ( media_series_title | string )%}
                    {% set ns.template_entity = entity %}
                    {% endif %}
                  {% endif %}                      
              {% endif %}                  
            {% endfor %}                
            {% if ns.template_entity == "" %}
                {% set ns.template_entity = ns.cast_entity %}
            {% endif %}
            {% if ns.template_entity != ns.cast_entity %}                  
              {{state_attr(ns.template_entity, 'entity_picture')}}
            {% elif state_attr( ns.cast_entity, 'media_title')  %} 
              {{state_attr(ns.cast_entity, 'entity_picture')}} 
            {% else %}
              off
            {% endif %}
    media_series_title_template: "{{state_attr( 'media_player.livingroom_chromecast', 'media_series_title') }}"
    title_template: "{{state_attr( 'media_player.livingroom_chromecast', 'media_title') }}"
    media_episode_template: "{{state_attr( 'media_player.livingroom_chromecast', 'media_episode') }}"
    media_season_template: "{{state_attr( 'media_player.livingroom_chromecast', 'media_season') }}"
    unique_id: template_following_media_player
maxi1134 commented 1 year ago

Outputting {{ ns.final_entities}] works