Need your help with automation

Hi,

I have created such automation to log media player titles:

id: '1720000000001'
alias: Log song name
description: Logs song name
trigger:
  - platform: device
    device_id: 00000000000000000000000000000001
    domain: media_player
    entity_id: 00000000000000000000000000000002
    type: playing
condition: []
action:
  - service: logbook.log
    metadata: {}
    data:
      name: Living Room Speaker currently playing
      entity_id: media_player.living_room_speaker
      domain: media_player
      message: >
        {{ states.media_player.living_room_speaker.attributes.media_artist }} -
        {{ states.media_player.living_room_speaker.attributes.media_title }}
mode: single

When I manually click “Next” or “Previous”, it adds log as expected:

But when the next track starts automatically, it does not log track name, just logs that state was changed to “Playing”:

As I am listening to state “Playing”, why does it not trigger when it changes automatically? Should I listen to a different state? If so, then to what state?
Thanks

Update: It looks to trigger that automatically too, but:

  • manually it logs title when song starts playing
  • automatically it logs title when song stops playing and new one starts playing (aka it logs previous title).

Any ideas why is it happening and how to fix it?

try:

trigger:
  platform: state
  entity_id:
    - media_player.-your_entity_id-
  attribute: media_title
1 Like

I believe we need to add a condition also.
Otherwise it will log when nothing is playing.

trigger:
  - platform: state
    entity_id:
      - media_player.living_room_speaker
    attribute: media_title
condition:
  - condition: state
    entity_id: media_player.living_room_speaker
    state: playing
action:
  - service: logbook.log
    metadata: {}
    data:
      name: Living Room Speaker currently playing
      entity_id: media_player.living_room_speaker
      domain: media_player
      message: >
        {{ states.media_player.living_room_speaker.attributes.media_artist }} -
        {{ states.media_player.living_room_speaker.attributes.media_title }}
mode: single

You don’t need to mask your entity names or device names, they are local for you.

1 Like

Thanks TNT_Larsn and Hellis81. Seems to work as expected without the condition mentioned by Hellis81.

Only one minor issue:
If same title is played next - it is not logged (as the trigger runs when the state of media_player.living_room_speaker attribute media_title changes).

What could be the condition to log if the next title is same? I sometimes play same title in a loop and I would like in long term to have some counts on most popular tracks I play.

That’s because a trigger is, unlike a condition, only listening to the state change and not the actual state. If you’re playing a song, the “playing” state doesn’t end by switching to the next song. The media player is still playing.

If you switch manually, you’re breaking the “playing” state, so the next song will switch to “playing” and that’s where your trigger reacts.

A trigger checks, if a treshhold is passed. In that moment it reacts (triggers). A condition checks, if a value is (already) over a treshhold at exact that moment of checking.

I am not sure this is the case (at least here), at least logs show that it goes into “idle” state and immediately changes back to “playing”:

image

Also my further observations and update of original post:

Just interesting to understand all that, but anyway TNT_Larsn’s solution is more graceful.

The reason I added the condition is because I thought it would log something when you stopped playing.

To make sure it logs the same song when repeating you could perhaps try numeric trigger on attribute media_position below 5 or something.
This should trigger every time a new song starts.

Wouldn’t it be easier to log it in Google sheets?
It has some features that probably would make it easier to analyze the songs.

You could also add a date time to your logs so you could see songs per day etc.

Valid thought. I checked it out of curiosity, but it does not log at all with that condition. I guess title changes not on “playing” but on a different state.

And thanks for numeric trigger hint - I’ll check what I can do with it.