Input_number manipulation

Am I attempting the impossible or do I have a simple mistake…?

I want to press one of several buttons on a dashboard which will set a variable ‘option’ to one of a number values and then call a script.
The values for the ‘option’ can be x and a number or y and a number, eg x10.

The script is to increase an input_number by the amount passed.

The error I get in the log is ‘mapping values are not allowed here’


script:
  go_to:
    alias: move by x or y
    sequence:
      - input_number.set_value
        target:
          entity_id: input_number.x_coord
        data:
          value: >
            {%if option[0]=='x'%}
              {{option[1:]|int(0)+states('input_number.x_coord')|int(0)}}
            {%endif%}


I don’t think you’re attempting anything impossible, but I also don’t think you’ve provide us enough info to help…

Well, when I reload the scripts on the Developer page and then review the Log under the System section I get the error message mentioned.

Is that the sort of clarity needed?

Scratch that…

You are missing the action: key in front of input_number.set_value:man_facepalming:

1 Like

Howeve, whilst it now doesn’t error on the reload of scripts, I get

Failed to perform the action script/go_to. expected float for dictionary value @ data[‘value’]

when I call it. If I send myself a message in the script doing the maths I get it fine, it is assigning it to the input_number that offends things.

Any further wisdom !

input_number:

  x_coord:
    initial: 2500
    min: 0
    max: 40000

  y_coord:
    initial: 2500
    min: 0
    max: 40000

Are you sure the error is from the right script? It sounds like something you would get when the “if” fails, since there’s no default. You might want to set it up to use a YAML condition instead of a Jinja guard clause:

script:
  go_to:
    alias: move by x
    sequence:
      - condition: template
        value_template: "{{option[0]=='x'}}"
      - action: input_number.set_value
        target:
          entity_id: input_number.x_coord
        data:
          value: "{{option[1:]|int(0)+states('input_number.x_coord')|int(0)}}"

or, remove it altogether by templating the entity ID to cover both axes/input_number entities:

script:
  go_to:
    alias: move by x or y
    sequence:
      - variables:
          ent: "{{'input_number.'~option[0]~'_coord'}}"
      - action: input_number.set_value
        target:
          entity_id: "{{ent}}"
        data:
          value: "{{option[1:]|int(0)+states(ent)|int(0)}}"

I preferred your earlier variant, but it seemed to not add, just assign. This version errors
Failed to perform the action script/go_to. Template rendered invalid entity IDs: input_number.~option[0]~_coord

Double check the quotes… it looks like I had some “pretty” ones infecting the assignment for ent. I’ve corrected them above.

This works !!

  go_to:
    alias: move by x or y
    sequence:
      - variables:
          ent: >
            {{'input_number.'~option[0]~'_coord'}}
      - action: input_number.set_value
        target:
          entity_id: >
            {{ent}}
        data:
          value: "{{option[1:]|int(0)+states(ent)|int(0)}}"

Thanks for your help and elegant solution.

1 Like