Passing a random number parameter to a tap_action script

I have the following script as the tap_action on a custom-button-card. It passes an integer to a script to tell the script how many times to arrow up in a menu. As written, the script expects an integer value (x) for stepcount. What I’d like to do is substitute for the value x the state of a random helper I created that will supply a random number between 0 and 16.

tap_action:
  action: call-service
  service: script.denon_favorites_reverse
  service_data:
    stepcount: x
    station: Foobar

When I look at {{ states.sensor.randomsteps.state }} in Developer Tools, it correctly returns an integer, but that specification causes the execution of this tap action to barf and not run, which makes sense now that I think about it because the call to the script doesn’t evaluate the sensor value, it just tries to call the script with the value it has at the time of the call. Is there a way to supply the value of sensor.randomsteps as the stepcount?

Almost no cards support templates in card actions, so you would need to handle the template logic in the script itself.

If you post the config for script.denon_favorites_reverse, we can help you come up with a way to do it.

Here’s the script that uses the passed stepcount:

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 }}"
    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(0) }}"
alias: Denon Favorites Reverse

Just change the default of the stepcount variable:

variables:
  stepcount: "{{ stepcount | default( states('sensor.randomsteps')|int(0), true ) }}"

That should allow you to just not provide a stepcount in the calling action when you want it to be random.

tap_action:
  action: call-service
  service: script.denon_favorites_reverse
  service_data:
    station: Foobar

FWIW, you’ve got some excessive quotes in the notification. It should be just:

  - action: notify.mobile_app_iphone
    metadata: {}
    data:
      message: "{{ stepcount }}"
2 Likes

Much appreciated!