Node Red: Spotify Source matching

Hey all,

So I’m trying to figure out how to use one node to pass on a msg if it matches one or more Spotify sources as a follow me music function.

I have it down to two nodes, but Im wondering if I can get it to one using the render template node?

I currently have this

{{ states.media_player.spotify.attributes.source }}

In my render template node, but Im unsure what I need to add to get it to filter multiple sources and trigger a call service node after that.

If anyone can share a value template that achieves this, would be much appreciated.

In case someone stumbles on this looking for something similar; here is my two node solution :slight_smile:

image

Current state the Spotify entity with no value to pass along anything. then a function node with the following in the “On Message” tab;

// Define the sources you want to filter for
const allowedSources = ["Kitchen", "Living Room"]; // Add your allowed sources here


// Check if the incoming source is in the allowed list
if (allowedSources.includes(msg.data.attributes.source)) {
  // Pass the message along if the source is allowed
  node.send(msg);
}

You can also disallow so it will pass anything except for

// Define the sources you want to block
const disallowedSources = ["Kitchen", "Living Room"]; // Add your disallowed sources here
// Check if the incoming source is in the disallowed list
if (disallowedSources.includes(msg.data.attributes.source)) {
  // Stop the message if the source is disallowed
} else {
  // Pass the message along if the source is not disallowed
  node.send(msg);
}

In the current-state node

image

$entity().attributes.source in ["Kitchen", "Living Room"]
1 Like

Thank you so much for that, amazing! This also allows me to disallow sources as well. Couldn’t be happier :slight_smile:

I have one more question if you can help? :slight_smile:

If I wanted to add only pass on msgs if spotify is playing on these sources. is this possible?

Bard gave me

$entity().state == "playing" and $entity().state.attributes.source in ["Bedroom Speaker", "Living Room Speaker"]

But it seems that == is not a acceptable character

single =

1 Like

Ok no errors, but Im very confused.

It does output, but it does not appear to filter the sources or output to the, if state = true

You can see it comes up as playing when I trigger an input, but its red?

$entity().state = "playing" and $entity().state.attributes.source in ["Kitchen", "Living Room"]

So that should be; if the spotify entity is “playing” and the source attributes = either “Kitchen” or “Living Room” then send msg though the “if state” is true, gate

But no matter what source is playing it always triggers to debug 8?

I’ve restarted HA, no difference :frowning:

Sorry to get you stuck in the weeds like this, you think its going to be a sample answer then BAM! HAHA

Oh ignore the sources on the screen shot ill fix all that when Im done :slight_smile:

should be

$entity().attributes.source
1 Like

You are a Node Red god! HAHA

Thank you! That has streamlined so much for me :slight_smile: