Trying to reject a group ive setup

Ive got a template set to monitor the number of TVs on in the house…for a number of reason…so Ive been using a template as below to strip out the media_player devices that I don’t need to monitor leaving just the TVs in question…

  • platform: template

    sensors:

    number_tvs_on:

    friendly_name: Number of TV On
    
    value_template: >-
    
      {{ states.media_player
    
                | rejectattr('entity_id', 'eq', 'media_player.spotify_jouster')
    
                | rejectattr('entity_id', 'eq', 'media_player.studydot')
    
                | rejectattr('entity_id', 'eq', 'media_player.studysony')
    
                | rejectattr('entity_id', 'eq', 'media_player.studysonyplex')
    
                | rejectattr('entity_id', 'eq', 'media_player.gardenroomdot')
    
                | rejectattr('entity_id', 'eq', 'media_player.gardenroomdotsonos')
    
                | rejectattr('entity_id', 'eq', 'media_player.gardenroomsonos')
    
                | rejectattr('entity_id', 'eq', 'media_player.gardenroomsony')
    
                | rejectattr('entity_id', 'eq', 'media_player.gardenroomsonyplex')
    
                | rejectattr('entity_id', 'eq', 'media_player.appletvk')
    
                | rejectattr('entity_id', 'eq', 'media_player.kitchenshow')
    
                | rejectattr('entity_id', 'eq', 'media_player.kitchenshowsonos')
    
                | rejectattr('entity_id', 'eq', 'media_player.kitchenplaybarsonos')
    
                | rejectattr('entity_id', 'eq', 'media_player.loungedot')
    
                | rejectattr('entity_id', 'eq', 'media_player.loungedotsonos')
    
                | rejectattr('entity_id', 'eq', 'media_player.livingroomsonos')
    
                | rejectattr('entity_id', 'eq', 'media_player.madiecho')
    
                | rejectattr('entity_id', 'eq', 'media_player.madisonos')
    
                | rejectattr('entity_id', 'eq', 'media_player.masterbedroomshow')
    
                | rejectattr('entity_id', 'eq', 'media_player.masterbedroomshowsonos')
    
                | rejectattr('entity_id', 'eq', 'media_player.masterbedroomsonos')
    
                | rejectattr('entity_id', 'eq', 'media_player.ensuitesonos')
    
                | rejectattr('entity_id', 'eq', 'media_player.ensuitealexasonos')
    
                | rejectattr('entity_id', 'eq', 'media_player.ethandot')
    
                | rejectattr('entity_id', 'eq', 'media_player.ethandot')
    
                | rejectattr('entity_id', 'eq', 'media_player.ethandotsonos')
    
                | rejectattr('entity_id', 'eq', 'media_player.ethansonos')
    
                | rejectattr('entity_id', 'eq', 'media_player.outdoor')
    
                | rejectattr('entity_id', 'eq', 'media_player.nvidiashield')
    
                | rejectattr('entity_id', 'eq', 'media_player.nvidiashieldplex')
    
                | selectattr('state', 'eq', 'playing')
    
                | list | count + (states.media_player)
    
                | selectattr('state', 'eq', 'on')
    
                | list | count }}
    

So its looking for Tvs with the ON and Playing state…

I created a group called media_player.rejectedmediaplayers and added that to the above template and removed everything else and the template returns the group itself as being ON and all other previously ignored media players…is there a way to get around this…or wont this do what I am trying to do

From my end, its just a way to make things simpler going forward and adjust a group as items get added without having to go through and adjust the YAML (which I know wont take that long)

TIA

Your template doesn’t seem to match your description. You are adding two counts. The first is every media player that is “playing” that isn’t in your list of rejections. The second is every media player that is “on”, without rejecting any media players… do none the non-tv media players ever have a state of “on”?

Use the expand() function to access the entity ids of the group members. In the count expression you will need to reject the group members and the group itself. It is much more compact to reject or select things in a list by using the in test. You can use the same test to select entities with both “on” or “playing” as their state.

{% set not_tvs = expand('media_player.rejectedmediaplayers') 
| map(attribute='entity_id') | list + ['media_player.rejectedmediaplayers'] %}
{{ states.media_player 
| rejectattr('entity_id', 'in' , not_tvs) 
| selectattr('state', 'in', ['on', 'playing']) | list | count }}

EDIT: Fixed [ ] typo in template

The first item in the following list is a list:

[not_tvs, 'media_player.rejectedmediaplayers']

so it will look like this:

[ ['media_player.ensuitealexasonos', 'media_player.etc'],  'media_player.rejectedmediaplayers' ]

The following template won’t be able to find a matching entity_id in the list within a list:

rejectattr('entity_id', 'in', [ ['media_player.ensuitealexasonos', 'media_player.etc'],  'media_player.rejectedmediaplayers' ])

Example:

I suggest you simply add another rejectattr filter to reject 'media_player.rejectedmediaplayers separately.

Example:

{{ states.media_player
  | rejectattr('entity_id', 'eq', 'media_player.rejectedmediaplayers')
  | rejectattr('entity_id', 'in', state_attr('media_player.rejectedmediaplayers', 'entity_id'))
  | selectattr('state', 'eq', 'on') | list | count }}

Depending on jouster’s reply to your question, the final line of the template may need to be what you suggested: selectattr('state', 'in', ['on', 'playing']) | list | count

1 Like

Dang it @123… I was just coming back to fix that list mistake :slight_smile:

I was going to do it with list concatenation…

{% set not_tvs = expand('media_player.rejectedmediaplayers') 
| map(attribute='entity_id') | list + ['media_player.rejectedmediaplayers'] %}
{{ states.media_player 
| rejectattr('entity_id', 'in' , not_tvs) 
| selectattr('state', 'in', ['on', 'playing']) | list | count }}
1 Like