How to use input_text in a button tap_action

I am storing a string in an input_text

input_text:
  text1:
    name: NameLWAppId
    initial: LwAppIdValue

I have this Button

show_name: true
show_icon: true
type: button
entity: ""
name: MQTT Button
tap_action:
  action: perform-action
  perform_action: mqtt.publish
  target: {}
  data:
    topic: >-
      "{{ states('input_text.text1') }}"/xxx/yyy
    retain: false
    payload: "somePayload"

The MQTT message is published, but no variable substitution happens. This is the published MQTT message
"{{ states('input_text.text1') }}"/xxx/yyy

What I want the MQTT topic to be is:
LwAppIdValue/xx/yyy

What am I doing wrong?

Assuming that the button card supports templates. It, like most core cards, does not.

Put your action in a script (that does support templates) and call that instead.

Thanks.

What do you suggest?

Just edited my post with a suggestion.

:grinning: Thanks. That was what I was missing. Works perfectly now

OK - I get it now … a Button Card does not support templates … so no way for the button card to grab a saved input_text value, and pass it onto a script.

I have a case where I need to click a button to fire a script, and that script needs a number of values that are stored as input_text that look like this

  • lht_12345_param1
  • lht_12345_param2
  • lht_12345_param3

The button does know 12345 before it is clicked, so I can only pass 12345 into the script.

The script now needs to get the stored values:

  • {{ state.input_text.lht_12345_param1.state }}
  • {{ state.input_text.lht_12345_param2.state }}
  • {{ state.input_text.lht_12345_param3.state }}

What I can’t work out is how to get state.input_text.lht_12345_param1.state when all I have is 12345?

Of course - if I am approaching this problem from the wrong angle - please make suggestions.

button action

tap_action:
  action: perform-action
  perform_action: script.mqtt_publish
  data:
    text: 12345

In your script

"{{ states('input_text.lht_'~text~'_param1') }}"
"{{ states('input_text.lht_'~text~'_param2') }}"
"{{ states('input_text.lht_'~text~'_param3') }}"

Please take note of this warning:

https://www.home-assistant.io/docs/configuration/templating/#states