Text variables in if-then

Hi all!

I trying to make a script that speaks via several mediaplayers a texts.

Mediaplayer’s name and text to speak I put into two variables in any automations and then it call this script. Mediaplayer works fine with this variables, but I can not make valid if-then using variables.

How can I replace “media_player.2floor_2floor” with “media_player.{{ mediaplayer }}_{{ mediaplayer }}”?

All listing here:

sequence:
  - data:
      title: media_player.{{ mediaplayer }}_{{ mediaplayer }}
      message: "{{ text }}"
    action: persistent_notification.create
    enabled: true
  - if:
      - condition: state
        entity_id: media_player.2floor_2floor
        state: playing
    then:
      - wait_for_trigger:
          - entity_id:
              - media_player.2floor_2floor
            to: idle
            trigger: state
        timeout:
          hours: 0
          minutes: 0
          seconds: 30
          milliseconds: 0
        continue_on_timeout: false
  - action: media_player.play_media
    target:
      entity_id: media_player.{{ mediaplayer }}_{{ mediaplayer }}
    data:
      media_content_id: media-source://media_source/local/00001.mp3
      media_content_type: audio/mpeg
    metadata:
      title: 00001.mp3
      thumbnail: null
      media_class: music
      children_media_class: null
      navigateIds:
        - {}
        - media_content_type: app
          media_content_id: media-source://media_source
  - metadata: {}
    data:
      cache: true
      media_player_entity_id: media_player.{{ mediaplayer }}_{{ mediaplayer }}
      message: "{{ text }}"
      language: ru
    target:
      entity_id: tts.google_en_com
    enabled: true
    action: tts.speak
fields:
  mediaplayer:
    selector:
      text: null
    name: Mediaplayer
    description: Через что говорить
    required: true
  text:
    selector:
      text: null
    name: Text
    description: Что говорить
    required: true
alias: Говорилка
description: ""

You can’t, as state conditions (and triggers) do not support templates. You need to rewrite it as a template condition.

- if: " {{ is_state('media_player.' ~ mediaplayer ~ '_' ~ mediaplayer, 'playing') }}"

But why ohh why do you name your entitities that way…?! :woozy_face:

2 Likes

It’s not me, it’s ESP32 automatically calls :slightly_smiling_face:

Yes, this works. But still can’t make correct template for condition trigger

Change it to a wait_template instead of wait_for_trigger:

- wait_template: " {{ is_state('media_player.' ~ mediaplayer ~ '_' ~ mediaplayer, 'idle') }}"

You can also keep wait_for_trigger with a template trigger, but if it already evaluates to true when first evaluated it will not fire until it flips to false and then back to true (just like a state trigger). A wait_template will proceed immediately if true.

1 Like

Yes, it work too. Thank you!