Alexa send_text_command with variable input not working

Hi, I am trying to set up a basic button on my dashboard that has a slider which establishes an input, and then pass that value to alexa via the send_text_command.

I have tried several variants to make this work, but the send_text_command just sends the straight text to Alexa, it does not seem to parse the {{ }} as variables. I can go look at my alexa logs and see that HA is just sending the full string.

Here is a version of my yaml:

  - type: custom:mushroom-number-card
    fill_container: true
    icon_type: icon
    entity: input_number.bedroom_fan_input
    primary_info: state
    secondary_info: none
    icon: mdi:ceiling-fan
    tap_action:
      action: perform-action
      perform_action: alexa_devices.send_text_command
      target: {}
      data:
        device_id: 0420c740dc12a164f59744a83e242acb
        text_command: >-
          {{"Set bedroom fan to "~ states('input_number.bedroom_fan_input')
          ~ "%"}}

I have also tried is with text_command: Set bedroom fan fan to {{states(ā€˜input_number.bedroom_fan_input’)}}%

Both of these parse correctly in the debugger on the card configuration of the dashboard, but both just pass the text as typed directly to alexa, which of course doesn’t work.

Am I doing something wrong, or is there another way to pass a variable as text_command to alexa?

There is nothing wrong with your template, it’s where you are trying to use it that is the problem.

The vast majority of cards do not support templates at all, and even those that do rarely support them in card actions. The standard workaround is to move your templated action to a script and call the script from your card.

It works, but the text command has to be set without the brackets surrounding the entire command. Here’s an example that I use -

action: alexa_devices.send_text_command
metadata: {}
data:
  device_id: 77c812c0bf615f6516b5686c6560e6a3
  text_command: change volume to {{ states('input_number.all_echo_volume') | int }} percent

EDIT notice the " | int "? I’m thinking that’s where yours is failing. Alexa is expecting a number and without it states is only providing a string.

OP’s issue has nothing to do with the fact that they are using string concatenation inside the template delimiters or the int filter.

Your example is also only ā€œproviding a stringā€ā€¦ there’s no such thing as a mixed-data-type text command.

Well isn’t that the whole point of the pipe? To apply a filter and turn the string into an integer (number)? Or maybe I’m misunderstanding how jinja works… idk. I’m thinking the issue is how Alexa wants and expects to receive the command. Because my command works and their’s at it’s current iteration does not.

Yes. Test it in the Template editor tool and check the Result type:

versus the result of just the template:

The rest of the text command value will force it to be a string… the int makes no difference.

OP’s issue has nothing to do with Alexa. They are trying to use a template where it isn’t supported i.e. a card action. Your command would also not work in a card action.

Ahhh, I see what you’re saying. I apologize. I reread the OP and your following post.

So instead of a card action, perhaps the OP should use the template directly in an automation with the input_number helper? Or a script like you had recommended. That’s what I’m doing to control the volume. Then they’d only need to include the helper in an entity card.

What I meant by string, as opposed to integer, was the number itself within the text command. Without the | int, the number, itself, would appear in quotations to Alexa. When Alexa Devices first rolled out this feature, that was an issue. I don’t know if it still is and I don’t really care to test it to find out, lol. My command works as-is. Sorry for the misunderstanding

Thank you both for the prompt replies. I have this working now. I will share what I am doing for others that might benefit. I am not sure if this is the most efficient way to do this, so open to other feedback.

I created a script:

sequence:
  - variables:
      fan_percent: "{{ states('input_number.bedroom_fan_input') | int}}"
  - action: alexa_devices.send_text_command
    data:
      device_id: 0420c740dc12a164f59744a83e242acb
      text_command: Set bedroom fan fan to {{fan_percent}}

Then from the card, I just call the script:

  - type: custom:mushroom-number-card
    fill_container: true
    icon_type: icon
    entity: input_number.bedroom_fan_input
    primary_info: state
    secondary_info: none
    icon: mdi:ceiling-fan
    tap_action:
      action: perform-action
      perform_action: script.new_script
      target: {}

obviously I need to name the script something descriptive, etc, but this is now functioning.

1 Like