Script with Fields -> Condition of field value

Hi! I’m trying to build a script with input fields to get music assistant playing the music on different devices. The first two field “ID” and “type” are working, but i’m not able to get the field “device” with an condition working! If device==Soundbar it should play on my soundbar, if device==ZIPP it should use my ZIPP2-Speaker…
At the moment i just try to check if the field “device” is “Soundbar”, but the condition is true no matter what input value i’m trying…

Hope somebody could help me…

Here is my script:

alias: "Music Assistant: Start playing"
sequence:
  - if:
      - condition: template
        value_template: "data_template: \"{{ device == 'Soundbar' }}\""
        enabled: true
    then:
      - metadata: {}
        data:
          media_type: "{{ type }}"
          media_id: "{{ ID }}"
        action: music_assistant.play_media
        target:
          entity_id: media_player.soundbar
    else:
      - metadata: {}
        data:
          media_type: "{{ type }}"
          media_id: "{{ ID }}"
        action: music_assistant.play_media
        target:
          entity_id: media_player.zipp_2
    enabled: true
fields:
  ID:
    name: Media ID
    description: Media ID
    required: true
    example: SWR3
  type:
    name: Mediatype
    description: Type
    required: true
    example: radio
  device:
    name: Device
    description: Speaker
    required: true
    example: Soundbar
mode: restart
icon: mdi:music
description: ""

Try this:

value_template: "{{ device == 'Soundbar' }}"

Thanks for your answer! I tried that but it didn’t work.

Sorry I did a mistake in my first post: The condition seems always to be FALSE, it exectues the ELSE-part and plays always on my ZIPP2-speaker while i’m executing:

action: script.music_assistant_wiedergabe_starten
data:
  ID: SWR3
  type: radio
  device: Soundbar

Check the script trace.

Sorry i’m not very familiar with that, but I tried:
I see that choice was “else” because of IF-result was “false”.

Further i can see:
if/condition/0
result: false
entities:

Download the trace and post it here.

Okay, here it is:

{
  "trace": {
    "last_step": "sequence/0/else/0",
    "run_id": "03ff71259163772bfccb4a8b242aba99",
    "state": "stopped",
    "script_execution": "finished",
    "timestamp": {
      "start": "2024-12-19T11:07:06.595465+00:00",
      "finish": "2024-12-19T11:07:06.816706+00:00"
    },
    "domain": "script",
    "item_id": "music_assistant_wiedergabe_starten",
    "trace": {
      "sequence/0": [
        {
          "path": "sequence/0",
          "timestamp": "2024-12-19T11:07:06.595790+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "script.music_assistant_wiedergabe_starten",
              "state": "off",
              "attributes": {
                "last_triggered": "2024-12-19T10:27:04.575234+00:00",
                "mode": "restart",
                "current": 0,
                "icon": "mdi:music",
                "friendly_name": "Music Assistant: Wiedergabe starten"
              },
              "last_changed": "2024-12-19T11:07:03.777148+00:00",
              "last_reported": "2024-12-19T11:07:03.777148+00:00",
              "last_updated": "2024-12-19T11:07:03.777148+00:00",
              "context": {
                "id": "01JFF9K1Q1Y4CEEPS25QJP2W8C",
                "parent_id": null,
                "user_id": null
              }
            },
            "ID": "SWR3",
            "type": "radio",
            "device": "Soundbar",
            "context": {
              "id": "01JFF9K4F3WCNHTFPPDEYFV0TD",
              "parent_id": null,
              "user_id": "57ff779842184834a926e0fccf69c851"
            }
          },
          "result": {
            "choice": "else"
          }
        }
      ],
      "sequence/0/if": [
        {
          "path": "sequence/0/if",
          "timestamp": "2024-12-19T11:07:06.595934+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "sequence/0/if/condition/0": [
        {
          "path": "sequence/0/if/condition/0",
          "timestamp": "2024-12-19T11:07:06.595960+00:00",
          "result": {
            "result": false,
            "entities": []
          }
        }
      ],
      "sequence/0/else/0": [
        {
          "path": "sequence/0/else/0",
          "timestamp": "2024-12-19T11:07:06.596233+00:00",
          "result": {
            "params": {
              "domain": "music_assistant",
              "service": "play_media",
              "service_data": {
                "media_type": "radio",
                "media_id": "SWR3",
                "entity_id": [
                  "media_player.zipp_2_grau"
                ]
              },
              "target": {
                "entity_id": [
                  "media_player.zipp_2_grau"
                ]
              }
            },
            "running_script": false
          }
        }
      ]
    },
    "config": {
      "alias": "Music Assistant: Wiedergabe starten",
      "sequence": [
        {
          "if": [
            {
              "condition": "template",
              "value_template": "value_template: \"{{ device == 'Soundbar' }}\"",
              "enabled": true
            }
          ],
          "then": [
            {
              "metadata": {},
              "data": {
                "media_type": "{{ type }}",
                "media_id": "{{ ID }}"
              },
              "action": "music_assistant.play_media",
              "target": {
                "entity_id": "media_player.wohnzimmer_soundbar"
              }
            }
          ],
          "else": [
            {
              "metadata": {},
              "data": {
                "media_type": "{{ type }}",
                "media_id": "{{ ID }}"
              },
              "action": "music_assistant.play_media",
              "target": {
                "entity_id": "media_player.zipp_2_grau"
              }
            }
          ],
          "enabled": true
        }
      ],
      "fields": {
        "ID": {
          "name": "MedienID",
          "description": "Medien-ID (URL, Name des Elements zur Wiedergabe)",
          "required": true,
          "example": "SWR3"
        },
        "type": {
          "name": "Medientyp",
          "description": "Typ des abzuspielenden Inhaltes (Radio, Wiedergabeliste, ...)",
          "required": true,
          "example": "radio"
        },
        "device": {
          "name": "Wiedergabegerät",
          "description": "Wiedergabegerät auf dem abgespielt werden soll",
          "required": true,
          "example": "Soundbar"
        }
      },
      "mode": "restart",
      "icon": "mdi:music",
      "description": ""
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01JFF9K4F3WCNHTFPPDEYFV0TD",
      "parent_id": null,
      "user_id": "57ff779842184834a926e0fccf69c851"
    }
  },
  "logbookEntries": [
    {
      "state": "on",
      "entity_id": "script.music_assistant_wiedergabe_starten",
      "icon": "mdi:music",
      "when": 1734606426.595634,
      "context_user_id": "57ff779842184834a926e0fccf69c851"
    },
    {
      "state": "idle",
      "entity_id": "media_player.zipp_2_grau",
      "icon": "mdi:speaker",
      "when": 1734606426.6214526,
      "context_user_id": "57ff779842184834a926e0fccf69c851",
      "context_state": "on",
      "context_entity_id": "script.music_assistant_wiedergabe_starten"
    },
    {
      "state": "off",
      "entity_id": "script.music_assistant_wiedergabe_starten",
      "icon": "mdi:music",
      "when": 1734606426.8160057,
      "context_user_id": "57ff779842184834a926e0fccf69c851",
      "context_state": "on",
      "context_entity_id": "script.music_assistant_wiedergabe_starten"
    },
    {
      "state": "playing",
      "entity_id": "media_player.zipp_2_grau",
      "icon": "mdi:speaker",
      "when": 1734606427.4308136,
      "context_user_id": "57ff779842184834a926e0fccf69c851",
      "context_state": "on",
      "context_entity_id": "script.music_assistant_wiedergabe_starten"
    }
  ]
}

You did not change the condition to what I wrote.

Oh sorry… i found my fault:
I inserted it into the visual editor so it came to the result above and didn’t work :frowning:
I changed to yaml code editor and now it works! Thank you very much!

1 Like