I got to revive this, because I realized there is a problem: this sensor will update any time media_player.mpd_schlafzimmer
changes the media_conent_id
attribute; as per this template, that is correct. However, I want this sensor only to update when John Sinlair
is playing.
So I changed to the snippet below. Now it should only trigger if media_artist
matches; however, it will still always change the sensor value when anything changes about media_player.mpd_schlafzimmer
. But if there is something other than “John Sinclair 2000” playing, it will set empty values for the sensor and its attributes.
What do I need to change? Let’s say I am currently listening to John Sinclair 2000/F001 Im Nachtclub der Vampire
; the template sensor will display the correct value. This value should stay until I play something else by “John Sinclair 2000”. So if I play Misftis/12 Hits From Hell/Some Song.mp3
, I want the sensor to stay the way it is (with the value that is not the current playback value any longer, but the most recent playback of “John Sinclair 2000”) until I play another episode. I hope I explained this understandably… the template_sensor is supposed to keep track of what episode of this play we listened to last, not of what is currently playing (unless it happens to also be by “John Sinclair 2000”). This way, if we don’t remember where we left off, we can look at the sensor and start the next episode accordingly…
template:
- trigger:
- platform: template
value_template: "{{ state_attr('media_player.mpd_schlafzimmer', 'media_artist') == 'John Sinclair 2000' }}"
- sensor:
- name: "John Sinclair"
state: >
{% if state_attr("media_player.mpd_schlafzimmer", "media_artist") == "John Sinclair 2000" %}
{% set mci = state_attr("media_player.mpd_schlafzimmer", "media_content_id") %}
{% set x = mci.split('/')[2] %}
{% set y = x.split(' ', 1) %}
{{ x }}
{% endif %}
attributes:
Folge: >
{% if state_attr("media_player.mpd_schlafzimmer", "media_artist") == "John Sinclair 2000" %}
{% set mci = state_attr("media_player.mpd_schlafzimmer", "media_content_id") %}
{% set x = mci.split('/')[2] %}
{% set y = x.split(' ', 1) %}
{{ y[0] }}
{% endif %}
Titel: >
{% if state_attr("media_player.mpd_schlafzimmer", "media_artist") == "John Sinclair 2000" %}
{% set mci = state_attr("media_player.mpd_schlafzimmer", "media_content_id") %}
{% set x = mci.split('/')[2] %}
{% set y = x.split(' ', 1) %}
{{ y[1] }}
{% endif %}
picture: >
{% if state_attr("media_player.mpd_schlafzimmer", "media_artist") == "John Sinclair 2000" %}
{% set hass = "url:8123" %}
{% set img = state_attr("media_player.mpd_schlafzimmer", "entity_picture") %}
{{ hass }}{{ img }}
{% endif %}