Has anyone succeeded in working with response_variables?

In practice it doesn’t work. Passing response variables in the specified scope is still blank on the other side. So either the docs are wrong or there is a bug. In addition, what the community has been saying is this is simply not a workable paradigm, even if it did work as documented. Variables init’d outside of a branching scope should be lifted to the outer scope when set.

No, this 100% works if you keep it within the proper scope. I’m using it. Whatever you’re doing is not correct. Here’s a full example:

  - variables:
      calendar: calendar.xxx
  - service: calendar.get_events
    data:
      start_date_time: "{{ today_at() }}"
      end_date_time: "{{ today_at().replace(year=now().year + 2, day=1, month=1) }}"
    target:
      entity_id: "{{ calendar }}"
    response_variable: raw_events
  - variables:
      gathered: >
        {{ {'events': raw_events[calendar].events} }}

So please post your code. I’ll help you fix it.

EDIT: If you’re referring to this post:

If statements are a nested items. It won’t work with if statements, hence why you have to do…

So if the stop, which passes the response variable, is inside the conditional and the variable is populated inside of the same conditional it doesn’t pass the value. You’re saying this is by design?

Variables do not pass from inside a nested item to the outer layer.

e.g. this does not work.

- if:
  ...
  then:
  - variables:
       abc: 1
  else:
  ...
- variables:
    xyz: "{{ abc }}"

Yes, this is by design. That doesn’t mean it can’t change, just wasn’t implemented when variables were first introduced.

Thanks for clearing that up for me.

I have a similar question and I just can’t find such an example.
I have a script to call a service, just starting to program all things after.
Here is the script:

alias: Check and Add Spotify Favorite
sequence:
  - action: spotifyplus.check_track_favorites
    metadata: {}
    data:
      entity_id: media_player.spotifyplus_kevin_brown
    response_variable: favorite_exists
  - variables:
      test: |
        {{ favorite_exists }}
description: Checks and adds a favorite if the song is not already in favorites
icon: mdi:account-plus

The response in the service is like this (some info redacted):

this:
  entity_id: script.check_and_add_spotify_favorite
  state: 'off'
  attributes:
    last_triggered: '2024-10-22T22:07:03.487018+00:00'
    mode: single
    current: 0
    icon: mdi:account-plus
    friendly_name: Check and Add Spotify Favorite
  last_changed: '2024-10-22T22:07:03.769852+00:00'
  last_reported: '2024-10-22T22:07:03.769852+00:00'
  last_updated: '2024-10-22T22:07:03.769852+00:00'
  context:
    id: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    parent_id: null
    user_id: xxxxxxxxxxxxxxxxxxxxxx
context:
  id: xxxxxxxxxxxxxxxxxxxxx
  parent_id: null
  user_id: xxxxxxxxxxxxxxxxxxx
favorite_exists:
  user_profile:
    country: US
    display_name: Kevin Brown
    email: xxxxxxxxxxxxxxxxxxxxxxxxxx
    id: xxxxxxxxxxxxxxxxxxxx
    product: premium
    type: user
    uri: spotify:user:xxxxxxxxxxxxxxxxxxxxxxx
  result:
    2tUBqZG2AbRi7Q0BIrVrEj: false

The only thing I want is the “false” or “true” at the end. This literally means the current song is my favorites “true” or not “false”.

I cannot even seem to get the value. If I look at the variable, it is populated:

test:
  user_profile:
    country: US
    display_name: Kevin Brown
    email: xxxxxxxxxxxxxxxxxxx
    id: xxxxxxxxxxxxxxxxxxxxxxxx
    product: premium
    type: user
    uri: spotify:user:xxxxxxxxxxxxxxxxxxx
  result:
    2tUBqZG2AbRi7Q0BIrVrEj: false

The key “2tUBqZG2AbRi7Q0BIrVrEj” changes with each request.
I can get deeper with this:

variables:
  test: |
    {{ favorite_exists.result }}

Which yields:

test:
  50nfwKoDiSYg8zOCREWAm5: false

How do I get this value under result, what is the syntax? Simply put, in the following I would like to edit that template for test so that it returns “true” or “false” for further processing in the script to update GUI in certain ways.

- variables:
      test: |
        {{ favorite_exists.result**what_belongs_here** }}

Note: I suppose I can just create a new sensor that updates with each song change and just use that, but I was hoping not to do that

Solution is:

variables:
  test: "{{ (favorite_exists.result.values() | list)[0] }}"

Just a minor improvement- there’s no need to create a list and then pull out the first element. Just grab the first element directly:

variables:
  test: "{{ favorite_exists.result.values() | first }}"
2 Likes

Are you just trying to add the currently playing Spotify track to your track favorites via the spotifyplus service? If so, just call the save_track_favorites service:

action: spotifyplus.save_track_favorites
data:
  entity_id: media_player.spotifyplus_kevin_brown

It does not raise an error if the track is already a favorite.

Regarding your original question:

Copy / paste this into the Developer Tools \ Template tool:

{% set test = {
  "user_profile": 
  {
    "country": "US",
    "display_name": "Kevin Brown",
    "email": "xxxxxxxxxxxxxxxxxxx",
    "id": "xxxxxxxxxxxxxxxxxxxxxxxx",
    "product": "premium",
    "type": "user",
    "uri": "spotify:user:xxxxxxxxxxxxxxxxxxx"
  },
  "result":
  {
    "2tUBqZG2AbRi7Q0BIrVrEj": false
  }
}%}

User Profile Info:
  Display Name = {{ test.user_profile.display_name }}
  Country = {{ test.user_profile.country }}
  Product = {{ test.user_profile.product }}

Result Info:
  Spotify URI = {{ test.result.keys() | first }}
  Is Favorite = {{ test.result.values() | first }}

Will show these results:

User Profile Info:
  Display Name = Kevin Brown
  Country = US
  Product = premium

Result Info:
  Spotify URI = 2tUBqZG2AbRi7Q0BIrVrEj
  Is Favorite = False

Hope it helps!

Yes @thlucas … i was worried that the track would be added twice. Are you saying no need for a test to see if it is already there?

@kbrown01
correct - it it’s already a favorite then it does nothing; if not, it adds it.

The only limitation is that you have to call the appropriate service depending on the playing context:

  • for a track, call spotifyplus.save_track_favorites
  • for an artist (of the playing track), call spotifyplus.follow_artists
  • for an album (of the playing track), call spotifyplus.save_album_favorites
  • for a podcast episode, call spotifyplus.save_episode_favorites
  • for a podcast (of the playing episode), call save_show_favorites
  • for an audiobook, call spotifyplus.save_audiobook_favorites
  • for a playlist (that owns the playing track), call spotifyplus.follow_playlist