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)…
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 scriptEDIT (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 … 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.
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.
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
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.
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…)