After the last few posts saying that the tests for ‘eq’ to some attribute wasn’t working for some reason when I knew it was working for me I’ve been playing and doing some testing. I’m pretty sure I’ve got it worked out. Pardon the resulting wall of text…
As a baseline for reference running this in the template editor:
{{ states | selectattr('domain', 'in', 'media_player') | join(', ') }}
results in this:
<template state media_player.basement_dot=standby; is_volume_muted=False, media_content_type=standby, media_position_updated_at=2019-10-20T22:27:08.362695-04:00, source=Local Speaker, source_list=['Local Speaker'], available=True, last_called=False, friendly_name=Basement Dot, supported_features=56253 @ 2019-10-20T01:24:31.620621-04:00>, <template state media_player.big_room_dot=standby; is_volume_muted=False, media_content_type=standby, media_position_updated_at=2019-10-20T23:18:36.161143-04:00, source=Local Speaker, source_list=['Local Speaker'], available=True, last_called=False, friendly_name=Big Room Dot, supported_features=56253 @ 2019-10-20T01:24:31.625131-04:00>, <template state media_player.browser_mod_ae20a518_c1d40858=unavailable; volume_level=1, is_volume_muted=False, media_content_id=, type=browser_mod, deviceID=ae20a518-c1d40858, supported_features=21005 @ 2019-10-20T20:18:02.004933-04:00>, <template state media_player.browser_mod_c504add7_eda09c09=idle; volume_level=1, is_volume_muted=False, media_content_id=, type=browser_mod, deviceID=c504add7-eda09c09, supported_features=21005 @ 2019-10-20T23:47:27.469141-04:00>, <template state media_player.chromecastaudio1984=off; friendly_name=Livingroom Cast Audio, supported_features=21389 @ 2019-10-20T03:49:46.138632-04:00>, <template state media_player.computer_room_dot=standby; is_volume_muted=False, media_content_type=standby, media_position_updated_at=2019-10-20T23:40:56.720371-04:00, source=Local Speaker, source_list=['Local Speaker'], available=True, last_called=False, friendly_name=Computer Room Dot, supported_features=56253 @ 2019-10-20T01:24:31.616608-04:00>, <template state media_player.garage_dot=standby; is_volume_muted=False, media_content_type=standby, media_position_updated_at=2019-10-20T23:24:08.370505-04:00, source=Local Speaker, source_list=['Local Speaker'], available=True, last_called=False, friendly_name=Garage Dot, supported_features=56253 @ 2019-10-20T01:24:31.601384-04:00>, <template state media_player.kitchen_dot=standby; is_volume_muted=False, media_content_type=standby, media_position_updated_at=2019-10-20T23:45:43.846097-04:00, source=Local Speaker, source_list=['Local Speaker', "Jeff's GS5"], available=True, last_called=False, friendly_name=Kitchen Dot, supported_features=56253 @ 2019-10-20T01:24:31.596867-04:00>, <template state media_player.livingroom_dot=standby; is_volume_muted=False, media_content_type=standby, media_position_updated_at=2019-10-20T23:33:45.512348-04:00, source=Local Speaker, source_list=['Local Speaker'], available=True, last_called=False, friendly_name=Livingroom Dot, supported_features=56253 @ 2019-10-20T01:24:31.629507-04:00>, <template state media_player.master_bedroom_dot=standby; is_volume_muted=False, media_content_type=standby, media_position_updated_at=2019-10-20T23:45:25.508480-04:00, source=Local Speaker, source_list=['Local Speaker'], available=True, last_called=True, friendly_name=Master Bedroom Dot, supported_features=56253 @ 2019-10-20T01:24:31.592372-04:00>
It seems that for some reason the jinja test for “equal to” doesn’t like comparing things to a boolean (true/True/false/False).
this works in narrowing down the output of the above because 'browser_mod" is a string not a boolean:
{{ states | selectattr('domain', 'in', 'media_player') | selectattr('attributes.type', 'eq', 'browser_mod') | map(attribute='entity_id') | join(', ') }}
and results in this:
media_player.browser_mod_ae20a518_c1d40858, media_player.browser_mod_c504add7_eda09c09
but this doesn’t yield any results because “True” is a boolean:
{{ states | selectattr(‘domain’, ‘in’, ‘media_player’) | selectattr(‘attributes.available’, ‘eq’, ‘True’) | map(attribute=‘entity_id’) | join(’, ') }}
But jinja assumes that if there is no test given that you are trying to test for a boolean.
So this actually works because the “available” attribute is a boolean value (True):
{{ states | selectattr('domain', 'in', 'media_player') | selectattr('attributes.available') | map(attribute='entity_id') | join(', ') }}
And results in this:
media_player.basement_dot, media_player.big_room_dot, media_player.computer_room_dot, media_player.garage_dot, media_player.kitchen_dot, media_player.livingroom_dot, media_player.master_bedroom_dot
And if you wanted to be explicit in the test that you want to run then this is the way to write it (notice there is no quotation marks around True to prevent turning it into a string):
{{ states | selectattr('domain', 'in', 'media_player') | selectattr('attributes.available', 'eq', true) | map(attribute='entity_id') | join(', ') }}
and results in the same output as above:
media_player.basement_dot, media_player.big_room_dot, media_player.computer_room_dot, media_player.garage_dot, media_player.kitchen_dot, media_player.livingroom_dot, media_player.master_bedroom_dot
So finally to get the entity_id’s of all all media players whose states are either idle or playing and that has the shuffle attribute as true then this is what you would use:
{% set on_states = ['idle','playing'] %}
{{ states | selectattr('domain', 'in', 'media_player') |
selectattr('state', 'in', on_states) |
selectattr('attributes.shuffle', 'eq', true) |
map(attribute='entity_id') | join(', ') }}
I don’t have any media players that have the shuffle attribute so please test it and let me know if it works.