Syntax for using input_number in script

Despite my best efforts, I’ve been unable to use the value of an input_number helper in my simple script. I found several other topics related to this, e.g., 442654, but none of the syntax suggestions that I found are working for me.

Here’s my very simple script:

alias: Set temporary lock code
sequence:
  - device_id: 710d15ba02714a94140ab92391d2d001
    domain: zwave_js
    entity_id: lock.hall_garage_lock
    type: set_lock_usercode
    code_slot: '{{ states("input_number.lock_user_id") }}'
    usercode: "1000"
mode: single
icon: mdi:door-closed-lock

In the visual editor:

image

Here’s the error message that I receive:

Message malformed: not a valid value for dictionary value @ data['type']

And a screenshot:

image

I have tried several variants for specifying the code_slot, including flipping the order of the double & single quotes, but to no avail. It only seems to accept hard-coded numbers for the value of code_slot and usercode.

Any pointers would be appreciated…I’m sure it’s something obvious that I’m just missing.

Device actions, conditions, and triggers do not accept templates… use a Call Service action.

Thanks for the tip! I’m sooo close to getting it working now, but one more hiccup…

Here’s the new relevant portion of the script:

service: zwave_js.set_lock_usercode
data:
  code_slot: "{{ states('input_number.lock_user_id') }}"
  usercode: "{{ states('input_number.lock_code') }}"
target:
  entity_id: lock.hall_garage_lock

When it runs, it pulls the values but it is sending them without quotes, presumably because they are numbers. Here’s what is sent in the trace:

  service_data:
    code_slot: 40
    usercode: 1000

What I need though is for it to send them as strings, e.g.:

  service_data:
    code_slot: "40"
    usercode: "1000"

I’ve verified that the latter works by hardcoding it. I just need to figure out how to achieve this when encoding it in the template.

You could try:

code_slot: {{ '"%s"' % states('input_number.lock_user_id') }}
usercode: {{ '"%s"' % states('input_number.lock_code') }}

Thanks, @MaestroMetty. It didn’t seem to work though. For some reason, when I save those changes it kept resetting code_slot to ‘null’ and it reformatted usercode as:

"{{ states("input_number.lock_code") | string }}"

Continuing along that route, I’ve tried casting as int then string but am unable to get the representation that I need. For example, with Script A, it gets encoded as a number and there are no quotes added at all:

Script A:

data:
  code_slot: "{{ states(\"input_number.lock_user_id\") | int | string }}"
  usercode: "{{ states(\"input_number.lock_code\") | int | string }}"

Trace A:

  service_data:
    code_slot: 40
    usercode: 1000

Yet in Script B, if I add an additional escaped double quote to the template, it gets encoded with a single quote AND a double quote. I just need one or the other :).

Script B:

data:
  code_slot: "\"{{ states(\"input_number.lock_user_id\") | int | string }}\""
  usercode: "\"{{ states(\"input_number.lock_code\") | int | string }}\""

Trace B:

  service_data:
    code_slot: '"40"'
    usercode: '"1000"'

Any more ideas on what I’m doing incorrectly here?

Nevermind, although Script A above looks like it’s sending a number over to the service call, it seems to actually work. Thanks for the idea to cast the values!

could you use the pipe character | or the > symbol
and indent the template 2 characters further on the next line?

alias: Set temporary lock code
sequence:

  - device_id: 710d15ba02714a94140ab92391d2d001
    domain: zwave_js
    entity_id: lock.hall_garage_lock
    type: set_lock_usercode
    code_slot: |
      '{{ states("input_number.lock_user_id") }}'
    usercode: "1000"
mode: single
icon: mdi:door-closed-lock

That looks like it might be the answer, if he needed it.
Dev Tools > Template yields this…

code_slot: |
  '{{ states("input_number.alexa_word_duration") }}'

image

or with

  "{{ states("input_number.alexa_word_duration") }}"

image

I don’t think the template will get rendered if bother block scalar indicator and quotes are used in actual YAML…

That could be true…