I would like to have a generic binary_sensor which tells me whether any of my media players has a specific string in its attribute media_title.
(My goal behind it)
I find advertisement breaks quite annoying. The purpose is to detect whether my favorite radio station plays advertisements right now because it puts a signal word into the media_title. Therefore, I might be able to automate something like lowering the volume or playing something different while advertisements are being broadcast.
I have defined the mediaplayer group media_player.mediaplayers which contains all my players. I would like to examine the whole group to make it generic in order to find the desired information.
The problem is that not all media players provide the media_title attribute all the time or are offline so I have to filter for it. Unfortunately, I do not get it going although I have spent hours of trying. The following example does not work but may illustrate what I would like to get:
the Template editor throws an <generator object select_or_reject at 0x...> error. So I assume the subsequent filters do not work anyway. Is that correct?
Post the information for one of your media_players that contains the signal word in its media_title attribute. It can be a screenshot of the media _player as displayed in Developer Tools > States.
In fact I am looking for the word Werbung but this will occur only for a couple of minutes at the end of every hour. So I currently use the word SWR3 which will occur on a regular base between songs.
For reference - this is my final solution which works perfectly:
binary_sensor:
- platform: template
sensors:
# returns True if <SIGNALWORD> is being broadcast on any media player
media_player_radio_station_plays_advertisement_anywhere:
friendly_name: 'Media Player: Radio station plays advertisement anywhere?'
value_template: >
{% set advertisements = states.media_player
| selectattr('attributes.media_title', 'defined')
| selectattr('attributes.media_title', 'search', '<SIGNALWORD>')
| list | length %}
{{ advertisements >= 1 }}
Thanks again to Taras @123 for your excellent support!
For future reference, when creating new Template Sensors you should consider using modern format. The example you posted is a Template Binary Sensor in, what is now known as, legacy format.
Legacy format has not been deprecated but it will no longer receive enhancements. For future-proofing, it’s suggested to use modern format.
Here’s your Template Binary Sensor in modern format.
template:
- binary_sensor:
name: 'Media Player: Radio station plays advertisement anywhere'
state: >
{% set advertisements = states.media_player
| selectattr('attributes.media_title', 'defined')
| selectattr('attributes.media_title', 'search', '<SIGNALWORD>')
| list | length %}
{{ advertisements >= 1 }}
The main thing to keep in mind is that modern format Template Sensors have their own domain name called template. In other words, modern format Template Sensors, Template Binary Sensors, and Trigger-based Template (Binary) Sensors are configured under the template: key.
Be advised that you are not obligated to convert your existing, legacy-based Template Sensors to modern format. It’s entirely optional.
If decide to convert them, be advised the conversion process must be done in a specific way otherwise you might lose the sensor’s history (if that’s important to you).
For more information, refer to the following post: