Calculation function with input parameter and output

Hello together,

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 example:

Input:
sensor_temp1 - value1
sensor_temp2 - value2
sensor_temp3 - value3

output:
sensor_temp_calc1 - result1
sensor_temp_calc2 - result2
sensor_temp_calc3 - result3

How can I achieve this?

A good way to handle my problem would be that a calculation can be applied “on-the-fly” in sensor settings.
So there would be no need for a helper.

Is there an (HACS) integration, add-on or something else available?

Many thx in advance.
BR

Get the result where?

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

Hi,

thx for your answer.

in my case MQTT is holding my entities, and unfortunately there is no option for value_template.

Maybe you know other programming languages like c#.net or vb.net?

public int MyFunctionName (int param1, int param2)
{
int result = [... do some calc with param1 and param2 ...]
return result;
}

So this is reuseable with different input params without having to struggle with something like this f.e.:

result1 = param1 * 3,1415 / 1000
result2 = param2 * 3,1415 / 1000
result3 = param3 * 3,1415 / 1000
result4 = param4 * 3,1415 / 1000
result5 = param5 * 3,1415 / 1000

Output can be a sensor(-value). But I do I really need 5 template sensors for e.g. 5 temperature sensors like this?

Template1
{{ (states('sensor_temp1_value1') | float) / 100 }}

Template2
{{ (states('sensor_temp2_value2') | float) / 100 }}

Template3
{{ (states('sensor_temp3_value3') | float) / 100 }}

Template4
{{ (states('sensor_temp4_value4') | float) / 100 }}

Template5
{{ (states('sensor_temp5_value5') | float) / 100 }}

Maybe this is a better explanation what i want to achieve(?).

The MQTT Sensor platform does support value_template.

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 %}