Is there a way to execute a script that receives variables, from a template?

I’ve created a script that I want to reuse for several sensors, depending on a couple of entry variables.
Is there a way to use it directly on a template sensor ( it returns a value that will be assigned to the sensor), or am I forced to create an automation that will run the script with and assign the returned value to an helper (for example)…

Thanks

Post the script so we can understand how you’re using it to assign a value to a sensor entity.

The script would be something like this

alias: "Amazing Script"
sequence:
  - variables:
      current: "{{ states(device) | float(0) }}" # device is global var with the name of a sensor
      flow: "{{ states('sensor.flow') | float(0) }}"
      buffer: "{{ states('sensor.buffer') | float(0) }}"
  - if:
      - condition: template
        value_template: "{{ current == 0 }}"
    then:
      - variables:
          duration: "{{ buffer / flow }}"
      - stop: End
        response_variable: duration
    else:
      - variables:
          duration: "{{ current / flow }}"
      - stop: End
        response_variable: duration
mode: parallel
icon: mdi:battery-clock-outline
max: 10

and I’d like to use it in a way similar to this (if possible :smiley: )

template:
  - sensor:
      - unique_id: template_test
        state: "{{ duration }}"
        - service: script.amazing_script
          data:
            device: sensor.well_pump_power
            refValue: 2200
          target:
              entity_id: duration

Thanks

Is that the script you said, in your first post, that you had created (i.e. a script that works)? Or is it a hypothetical/aspirational script?

The reason why I ask because what you posted, both the script and Template Sensor, are invalid. They employ features that don’t currently exist in the latest release (2023.7.X) such as response_variable in a script EDIT (Wrong; it does) and service calls in a Template Sensor.

Rather than post pseudo-code, please describe the intended goal.

The script is a shorter / demo version of the entire script I did.
I did test the script on 2023.7.1 and it logged the right values…

Didn’t actually test the response_variable part because I was trying to go full on using it from a template and didn’t test it on an automation first :smiley:… but I got that info from the documentation. and well… the changelog mentions the scripts are now returning values since 2023.7.0

This feature is also added to scripts! So, you can make your scripts respond and use that response in your automations. This is a great way to make your scripts more dynamic and flexible.

About the template… I know it doesn’t work like that… and that’s basically my question… if there is any way for us to use scripts with parameters directly on a template to get a return value from it.

Thanks

I stand corrected; please accept my apologies. response_variable is in fact currently supported in 2023.7.X.

As for the Template Sensor, it currently doesn’t support service calls so you cannot make it call your script.

You can make the script publish the value of its response_variable to a custom event.

Then make a Trigger-based Template Sensor that subscribes to your custom event and receives the published value.

alias: "Amazing Script"
mode: parallel
icon: mdi:battery-clock-outline
max: 10
variables:
  current: "{{ states(device) | float(0) }}" # device is global var with the name of a sensor
  flow: "{{ states('sensor.flow') | float(0) }}"
  buffer: "{{ states('sensor.buffer') | float(0) }}"
  duration: >
    {% set value = buffer if current == 0 else current %}
    {{ {'value': value / flow } }}
sequence:
- stop: End
  response_variable: duration

then running it…

- service: script.amazing_script
  data:
    device: sensor.well_pump_power
    refValue: 2200
  response_variable: result

then using it in a template later on in the script that ran it…

{{ result.value }}

Highlighted here


The documentation says that the response_variable must be a key value pair. I believe that means it should be a dictionary. I haven’t tested this out but that’s what I would assume based on what I’ve seen.

1 Like

Oh nice! Thank you both. gonna try it later :wink:

After reading your answers more carefully, I still don’t get how the service / event can be triggered automatically for it then update the value on the template sensor. I guess that for now I’ll have to use an automation for that :frowning:

How is the script you created “triggered automatically”?

that’s my quest :smiley:

Our quest is trying to understand what you are attempting to do.

An automation has triggers, a script doesn’t.

You created a script. What would do you envision would call the script and when?


BTW, your script refers to a “global parameter” named device. Home Assistant doesn’t natively support global parameters (so you’ll need to re-design that part).


NOTE

I am beginning to get the impression that this is an XY Problem. We’re trying to solve the technique you asked for but it may be more efficient if we focus on what you are ultimately trying to achieve and we’ll tell you the best way to do it.

Lets reset the thread then! :slight_smile:

I WANT to reuse code responsible for updating several (template) sensors
SO I created a script that receives parameters and returns a value
IN ORDER TO use it has a blackbox on the declaration of said sensors.

I used a script because I assumed it would be the best way to achieve the goal of code reusability…
And I’m trying to avoid having automations triggering it for each sensor, because It is messy… (If I wouldn’t care about being messy… i’d probably just declare several input_number sensors… and have an automation for each one updating them…)

So… is there a better way?

Thanks

To do what?

You still haven’t explained what you plan to have trigger updates to the sensors, or where the data for the sensors’ values are coming from…

Input numbers are not sensors, they are helpers.

From this statement, it seems like what you need to look at is Trigger-based Template sensors.

Template Sensors update themselves. They don’t need anything to update them.

A Trigger-based Template Sensor adds the ability to make the Sensor update itself according to the triggers you choose.

yes i know, but that way i’v to copy the logic on every template i’m declaring…

Then consider centralizing the logic in a custom Jinja2 macro. Each Template Sensor calls the same macro with different parameters.

Reference:
Reusing templates

1 Like

yep. that will do it!

Thanks :slight_smile:

1 Like