Use variable from script in other automation

I have a script with variable that stores volume level. How can I use this variable in other automation? Is there a way to make it global or export?

make it a template sensor, template number, or an input_number.

Or share the automation and script and we can tell if you if you even need to store the value

Would it even help me, because script is triggered by other automation and value is changing dynamically?

It really depends on your script and automation. There are ways to make “global variables” however 9 times out of 10 they aren’t needed. If you really need one, the simplest way would be to have the script set an input_number.

You mean use that variable in input number block?

No, you’d use a service to set the input_number to the value you want to store (in the script). Then you’d use the input number in your other automations.

that service is available only in yml? Because I don’t see such block.

It’s not a block, it’s an action. And you need to create the input_number first.

ok, I see it. Trying to add it into my script.

Reading the documentation, input_number is confusing.
How to create the input_number first? Should it be like this: {{ volume }}

In the script I have volume variable

sequence:
  - variables:
      volume: '{{ state_attr(''media_player.mpd'', ''volume_level'') }}'

You’d use '{{ state_attr(''media_player.mpd'', ''volume_level'') }}' as the value field for input_number.set_value. You’ll have to swap the field over to a template by switching to yaml and putting in the template.

now I have:

action: input_number.set_value
metadata: {}
data:
  value: "{{ state_attr('media_player.mpd', 'volume_level') }}"

and error: must contain at least one of entity_id, device_id, area_id, floor_id, label_id.

What id should I use and where to put it?

Yeah…

1 Like

I added at the top of the script:

input_number:
  vol:
    name: 

and in action:


  - action: input_number.set_value
    target: 
      entity_id: input_number.vol
    data:
      value: "{{ state_attr('media_player.mpd', 'volume_level') }}"

error when saving:

Message malformed: extra keys not allowed @ data['input_number']

That’s not how it works.

You can create it in the UI through the Helpers Settings or in YAML in your configuration.yaml file.

Input number - Home Assistant

1 Like

Have you never created a helper before?

No, that’s why input_number is so confusing. I thought there should be an easy way, like everything in a single script.

Well after you create that helper, use that helper entity_id in your script.

I don’t know that most users would call it the “easy way” compared to using the UI, but you can do something like that directly in YAML using Packages.