Lovelace card to pass number input to a script

I’m trying to have a Lovelace entities card send a number parameter to a script based on user input to a text helper. I created the following YAML, which executes my script but only works if an actual value is specified for stepcount. I don’t get any error and the script is invoked but the stepcount parameter is empty. I’ve tried a number of simpler variations but nothing works.

type: entities
entities:
  - entity: input_text.test_text
  - type: call-service
    name: " "
    icon: mdi:music
    action_name: Go to station
    service: script.denon_favorites_reverse
    data:
      stepcount: "{{ 'input_text.test_text'|int(0) }}"

Why input text instead of input number?

Entities cards do not support templating… even cards that do support templating rarely support it in actions.

Also, the expression {{ 'input_text.test_text'|int(0) }} will always return 0… you would need to use the states() function if you want to retrieve the state value of the entity.

If you post your script config, we can help you modify it to work.

You don’t need to send the number to the script.
When you run the script the script can fetch the number

I was trying to avoid having to modify this script, which is already stretching my HA skills, but:

sequence:
  - data:
      command: /goform/formiPhoneAppDirect.xml?SIFAVORITES
    action: denonavr.get_command
    target:
      entity_id: media_player.denon_avr_x1200w
    enabled: true
  - delay:
      hours: 0
      minutes: 0
      seconds: 6.25
    enabled: true
  - data:
      entity_id: media_player.denon_avr_x1200w
      command: /goform/formiPhoneAppDirect.xml?SIFAVORITES
    action: denonavr.get_command
    enabled: false
  - delay:
      hours: 0
      minutes: 0
      seconds: 2
    enabled: false
  - repeat:
      count: "{{ stepcount }}"
      sequence:
        - delay:
            hours: 0
            minutes: 0
            seconds: 0.5
          enabled: true
        - data:
            command: /goform/formiPhoneAppDirect.xml?MNCUP
          action: denonavr.get_command
          target:
            entity_id: media_player.denon_avr_x1200w
        - delay:
            hours: 0
            minutes: 0
            seconds: 1
          enabled: false
    enabled: true
  - data:
      entity_id: media_player.denon_avr_x1200w
      command: /goform/formiPhoneAppDirect.xml?MNENT
    action: denonavr.get_command
    enabled: true
  - action: notify.mobile_app_iphone
    metadata: {}
    data:
      message: " {{ stepcount }}"
    enabled: false
  - action: input_text.set_value
    metadata: {}
    data:
      value: "{{ station | default(showstation) }}"
    target:
      entity_id: input_text.tunedstation
description: Denon Favorite Station
fields:
  stepcount:
    description: Number of UP steps
  station:
    selector:
      text: null
    name: station
variables:
  stepcount: "{{ stepcount | default( states('sensor.randomsteps')|int(0), true ) }}"
  showstation: "{{ states('sensor.randomsteps')|int(0) }}"
alias: Denon Favorites Reverse

I was using text because I thought input_number required the slider, I wanted to be able to specify a number.

nope.

1 Like

That makes sense. The question I have here is how to allow for multiple conditions in the current YAML:

  1. Stepcount could be supplied by a calling button (current YAML does this). I have Lovelace buttons that invoke the script and pass a specific stepcount value.

  2. If stepcount not suppllied, check the value of the random helper. I have a single Lovelace button that invokes the script but does not supply a stepcount.

Now the idea is to modify the script so that if stepcount is not supplied but a helper named something like “input_number_steps” is not zero, assign the value of input_number_steps to stepcount.

So maybe that is the solution, to add that condition to the script and clear out input_number_steps at the end? In an attempt to do this, I added an if-then condition to the start of the script, but I’m stuck on how to assign the value of the helper to the stepcount field. I know I would use

"{{ states('input_number_steps' }}" 

for the value, but how do I assign the value to the variable? Do I need to do this within the variable: section?

For anyone wondering about this question of how to assign an input_number value to a script variable/field in a condition. In this example, stepcount is the variable to be assigned to, Because the script resets the helper to zero at the end of the script the condition below is fulfilled only if the script is called when a value has specifically been assigned to the input_number helper:

sequence:
  - if:
      - condition: numeric_state
        entity_id: input_number.stepsdirectaccess
        above: 0
    then:
      - variables:
          stepcount: "{{ states('input_number.stepsdirectaccess') }}"