I’m completely new to HA and I’m testing some integrations and features atm.
I searched the net, docs and forum - maybe the wrong way - and maybe that’s why I can’t find a solution/answer for my (stupid?) queston.
I’d like to have the possibility to make a calculation and pass input parameters to this calculation. As always the calc should give a return parameter/result.
I have lots of sensors giving me temperature values. Each sensor has its own name and value, for sure. With this as input I want to do some calculation. But I don’t want to create a helper for each sensor with the same calculation. I want one calculation helper, pass the temp-value to this helper and get the result.
For the most part, that is not really a thing in Home Assistant. Certain manually configured integrations like REST or MQTT allow modifying the received value before it is posted as the entity’s state by using Templating. Check the documentation for the integration you are using to see if value_template is listed as a valid configuration variable. Otherwise, you will need to use a secondary entity like a Template sensor/Helper or Filter sensor
You did not clarify where and how you will be using the returned value(s). There are almost always 2 or more ways to do anything in Home Assistant. Knowing the requirements of your specific use case really helps us narrow down the suggestions we give, so that you (as a new user) don’t get overly frustrated by the process.
For example, one way to create reusable function is through Global Template Macros. But if your goal is just to send your self a notification with your sensor values and you don’t need to use that specific data anywhere but in that one automation/script, there’s no need to set up a macro that is accessible everywhere.
Here’s an example of a Jinja template macro similar to the function you posted above:
{%- macro my_function(ent) %}
{{ ent }}: {{ (states(ent)|float(0) * 3.1415 / 1000)|round(2) }}
{%- endmacro %}
{{ my_function('sensor.a') }}
#or in a loop:
{% for x in ['sensor.a', 'sensor.b', 'sensor.c', 'sensor.d']%}
{{ my_function(x) }}
{% endfor %}