Call Script from Automation - send variable

Hi!

I’m new to Home Assistant, so possibly what I’m trying to do is trivial but I can’t get it to work and can’t find any example of something similar.

I’m trying to call a script from an automation and passing a variable “position” that will be used in two script actions (one is a condition and other is to set a device position).

I’m able to call the service from the automation without variables, but when adding the variable I can’t get it to work.

For my automation I have the following code:

alias: LowerBlinds
description: ''
trigger:
  - platform: time
    at: '18:00'
condition: []
action:
  - service: script.setblinds
    data:
      position: 10
mode: single

Now in the Script I want to use the value of the variable “position” (10) so I’m writing it this way:

alias: SetBlinds
fields:
      position:
        description: 'The position for blind'
        example: '25'
sequence:
  - condition: numeric_state
    entity_id: cover.ecozinha
    attribute: current_position
    above: {{ position }}
  - device_id: 2812797da98b437f32d90e2ccb79144e
    domain: cover
    entity_id: cover.ecozinha
    type: set_position
    position: {{ position }}
mode: single

When saving I get the following error:
Message malformed: expected float for dictionary value @ data[‘sequence’][0][‘above’]

I already tried several different ways according to the documentation and examples I found, also tried to use the generic “script.turn_on” service but can’t get it to work.

Thanks!

Hi! Thank you for your suggestion.
Sorry for not have mentioned before, I also tried that way, and get the same error.

Try

"{{ position | float }}"

Why cant you use an input_text instead of this variable? It would give more control and this automation can be easily done.

@Burningstone thanks for the suggestion, I tried this way:

alias: SetBlinds
sequence:
  - condition: numeric_state
    entity_id: cover.ecozinha
    attribute: current_position
    above: "{{ position | float }}"
  - device_id: 2812797da98b437f32d90e2ccb79144e
    domain: cover
    entity_id: cover.ecozinha
    type: set_position
    position: "{{ position | float }}"
mode: single
fields:
  position:
    description: The position for blind
    example: '25'

…but unfortunately I got the same error :frowning:

@sheminasalam not sure what you mean by using an input_text. I’m calling the script from an automation, how should I do it with an input_text, can you share some example?

Thanks!

What i meant was that you can store the value of position in an input_text entity. By this way you will be able to change the position to any value just by changing using the sericve input_text.set_value. And in your script instead of using the variable “position”, you can call the state of this input_text to get the position.

1 Like

@sheminasalam thanks for your suggestion, sounds like an option to solve my problem.

So, here is what I have done so far:

Created a new input_text variable called “input_text.eescrit_25” and set value to 10.

Changed my automation, as now I have the value in the input_text I don’t need to send it when calling the script as a service:

alias: LowerBlinds
description: ''
trigger:
  - platform: time
    at: '18:00'
condition: []
action:
  - service: script.setblinds
mode: single

Changed the script to get the value from input_text:

alias: SetBlinds
sequence:
  - condition: numeric_state
    entity_id: cover.ecozinha
    attribute: current_position
    above: '{{ input_text.eescrit_25.value }}'
  - device_id: 2812797da98b437f32d90e2ccb79144e
    domain: cover
    entity_id: cover.ecozinha
    type: set_position
    position: '{{ input_text.eescrit_25.value }}'
mode: single

I still get the same error, possibly I’m missing something. Is this the correct way to get the value of the input_text?

I tried:
'{{ input_text.eescrit_25.value }}'
'{{ input_text.eescrit_25.value | float }}'
'{{ input_text.eescrit_25 }}'
{{ input_text.eescrit_25 }}
input_text.eescrit_25

But non of them works.

Try this for your script.

alias: SetBlinds
sequence:
  - condition: template
    value_template: >-
      {{state_attr('cover.ecozinha', 'current_position') | int >=
      states('input_text.eescrit_25') | int }}
  - service: cover.set_cover_position
    data:
      entity_id: cover.ecozinha
      position: '{{states(''input_text.eescrit_25'') | int }}'
mode: single

Also instead on input_text you can look into input_number which will give you a slider too.

1 Like

@sheminasalam thanks a lot for the suggestion, that works! :grinning:

1 Like

Ok, I think the problem is that you can’t template above/below in the condition.

@sheminasalam’s workaround certainly works, but it’s not scalable when you use this in multiple places and defeats the purpose of the script called conpletely.

Changing the condition to a template condition should work and not require an intermediate input_text (also because it’s a number an input_number qould make more sense in this case).

alias: SetBlinds
sequence:
  - condition: template
    value_template: >
      {{state_attr('cover.ecozinha', 'current_position') | int >= position }}
  - service: cover.set_cover_position
    data:
      entity_id: cover.ecozinha
      position: '{{ position | int }}'
1 Like

@Burningstone absolutely, also tested your suggestion, this way I can reuse the script for any required position.
I can even pass the entity id I want to affect as another parameter, so the script become more generic.

Thank you all for the help and suggestions! I’m still starting to learn how HA works :slight_smile:

1 Like