Sending Variables to a script from a lovelace button, script receives "0" no matter what

Dear forum,

I do have a testbutton that does this:

type: button
tap_action:
  action: call-service
  service: script.testscript
  data:
    position_var: '42'
entity: input_number.roll_soll_position

The script look like this:

testscript:
  alias: testscript
  sequence:
  - service: cover.set_cover_position
    data: 
      entity_id: cover.eg_ez_roll_esphome      
      position: "{{states('position_var') | int}}"
  - service: input_number.set_value
    data: 
      entity_id: input_number.helfer_testnummer         
      value: "{{states('position_var') | int}}"
  mode: single

The script casts the “position_var” to int, since finally I want to send the “input_number.roll_soll_position” to it, which is a float.

No matter what I send with the button, the script always interprets the value “0”.

I am stuck in a one-way-dead-end-street. What am I missing?

Cheers,
Christian

2 Likes
type: button
tap_action:
  action: call-service
  service: script.testscript
  service_data:  # <-----###############################
    position_var: '42'
entity: input_number.roll_soll_position
testscript:
  alias: testscript
  sequence:
  - service: cover.set_cover_position
    data: 
      entity_id: cover.eg_ez_roll_esphome      
      position: "{{ position_var }}" # <-----###############################
  - service: input_number.set_value
    data: 
      entity_id: input_number.helfer_testnummer         
      value: "{{ position_var }}" # <-----###############################
  mode: single
2 Likes

Thanks, that works so far.

if I now set this:

type: button
tap_action:
  action: call-service
  service: script.testscript
  service_data:
    position_var: input_number.roll_soll_position # <----------------
entity: input_number.roll_soll_position

I get an error “…expected int for dictionary value…”. Where (and how :wink: ) would I optimally put the cast float-> int?

In your script change {{ position_var }} to {{ position_var | int }}.

1 Like

What exactly are you trying to do? It doesn’t make sense to me. Seems like you’re using way too many input_numbers for a button. Also, if you want to pass a number OR a value from an entity, you have to adjust your template in your script to account for that.

testscript:
  alias: testscript
  sequence:
  - service: cover.set_cover_position
    data: 
      entity_id: cover.eg_ez_roll_esphome      
      position: >
        {{ states(position_var) | int or position_var | int }}
  - service: input_number.set_value
    data: 
      entity_id: input_number.helfer_testnummer         
      value: >
        {{ states(position_var) | int or position_var | int }}
  mode: single
2 Likes

So the final goal of this is to have one script that can be called for all my blinds.

The frontend will have a template cover for lovelace. The lovelace cover buttons as well as my wall switches will start automations that will call the script. Also giving it the corrosponding “window open” switch and a “default position” to the script, which then acts accordingly.

In my example, I was trying to debug where I loose the actual number and therefore I did the second sevice call, sending the number sent into the script to a input number displaying its outcome. Finally the “service: input_number.set_value” will be gone.

@Burningstone: changing the code to your proposal results in the same error:

“Fehler beim Aufrufen des Diensts script/testscript. expected int for dictionary value @ data[‘position’]”

You should use persistent_notifications instead of creating an entity, it will pop up in your notification feed.

service: persistent_notification.create
data_template:
  message: '{{ position_var }}'

As petro already pointed out, you need to get the state of the input nubmer, currently you’re passing the name of the entity not the state.

Maybe I’m confused…

Why is the script using “states(‘position_var’)” as if “position_var” is an entity? Is that a new thing or has it always been that way and I just missed it or is it not really an entity and they shouldn’t be using it as such?

The variable was being transmitted as data to the script. It’s not an entity as far as I know.

ok, taking your input into account, I need to feed the variable as int into the script. This means, that the cast float -> int must happen in the call of the script.

So if I use the templating development tools, this:

{{states('input_number.roll_soll_position') | int}}

delivers: 50. If I remove the cast, it delivers 50.0.

If I put this into the button:

type: button
tap_action:
  action: call-service
  service: script.testscript
  service_data:
    position_var: {{states('input_number.roll_soll_position')}}
entity: input_number.roll_soll_position

and reopen the button, this happens:

type: button
tap_action:
  action: call-service
  service: script.testscript
  service_data:
    position_var:
      '[object Object]': null
entity: input_number.roll_soll_position

So, it comes down to the question: How do I transmitt an input_number.xyz (float) element to a script that expects an int as a variable?

He hands over the entity_id as a variable to the script and not the state of the entity_id, that’s why he needs states(position_var) to get the state of the entity_id passed to the script.

@CeeCee
You can’t just use a template in lovelace like you tried, you need something like this.

I gave you the answer 2 days ago but you ignored it. I’ll post it again

script:
  testscript:
    sequence:
    - service: cover.set_cover_position
      data: 
        entity_id: cover.eg_ez_roll_esphome      
        position: >
          {{ states(position_var) | int or position_var | int }}

This will allow the script to accept an input_number or a number. So either of these will work.

tap_action:
  action: call-service
  service: script.testscript
  service_data:
    position_var: input_number.roll_soll_position 

or

tap_action:
  action: call-service
  service: script.testscript
  service_data:
    position_var: 5
3 Likes

Sorry for not using your code. After a small adjustment, it works, thanks!

testscript:
  alias: testscript
  sequence:
  - service: cover.set_cover_position
    data: 
      entity_id: cover.eg_ez_roll_esphome      
      position: "{{ states(position_var) | int or position_var | int }}"
  - service: persistent_notification.create
    data_template:
      message: '{{ position_var }}'      
  mode: single

For some reason the “” were necessary.

Yes they are if you do a single line, however if you copied my code exactly, it would have worked because I use the multiline indicator.

3 Likes