Updating a helper from a template

I am trying to write a template that can be triggered to take a measurement and then update a couple helpers with new values. I can use the existing helper values without issue, but assigning the updated values back to the helpers is proving to be elusive. I’m sure there are other ways to do what I am wanting, but I’ve been stumped by what I feel should be a simple line of code and I refuse to let it beat me.

I want to sample a number when a room door is opened, but only when it is opened. I am trying to capture the average and standard deviation of the number for other use. The template I have written so far uses the Welford online algorithm to do the required math and is working as expected. Where I am struggling is when I try to update the Mean, Variance, and Number_Of_Samples helpers with the new values.

{## Create shorthand variables ##}
{% set Data = float(states("input_number.sample")) %}
{% set Mean = float(states("input_number.mean")) %}
{% set Variance = float(states("input_number.variance")) %}
{% set Samples = float(states("input_number.number_of_samples")) + 1 %}

{% set Mnext = Mean + (Data - Mean) / Samples %}
{% set Variance = Variance + (Data - Mean) * (Data - Mnext) %}
{% set Mean = Mnext %}

This gets me the result I am looking for, I just need to assign the values back to the helpers. This last mile is proving harder than anticipated. Any help in this matter would be appreciated.

You just have to use the input_number.set_value service to set the value of each helper: https://www.home-assistant.io/integrations/input_number/

Assuming that you’re doing this in a script, you can set variables like this: https://www.home-assistant.io/docs/scripts/#variables and then use them later in the input_number.set_value service calls.

You don’t need helpers; use Trigger-based Template Sensors.

I think it’s better suited for your intended application. Whereas an Input Number helper can be easily modified in the UI (probably not ideal for your purposes), a Template Sensor is read-only in the UI so a user can’t tweak the value computed by your template.

I agree with both of you that my efforts would be better spent using template variables and/or using a template sensor, and I will shift my efforts for this project accordingly. It makes too much sense not to. However, I am going to still pursue updating a helper for academic reasons now. I can be stubborn like that.

I’m currently building it in the Template section of the Developer Tools to test out, @rccoleman I think you are right about using input_number.set_value, but I am having trouble using it in this context. I have tried

{% set input_number.set_value.input_number.test_number = 45 %}

as my first attempt, but it results in the error TemplateSyntaxError: expected token 'end of statement block', got '.'
As a second pass I tried using this

{% set states("input_number.set_value.input_number.test_number") = 45 %}

but it simply results in a very similar but different error TemplateSyntaxError: expected token 'end of statement block', got '('

I can’t set it up the way it is shown in in the links you sent, because I’m not working in a YAML environment. The template tool simply converts the required YAML to displayable text.

Because you’re attempting to use it in an unsupported way.

input_number.set_value is a service call and not a Jinja2 function. In other words, what you’re trying to do there is something of your own invention and there are no examples of this unusual usage in the documentation or in this community forum.

Input Number - Services
Service calls

Makes sense, I thought it was far fetched when I started typing it, but the interface started offering auto-complete options and my helper was among those listed. This led me to believe I was on the right track so I played around with it for a bit. I have searched the jinja2 page looking for something that would facilitate what I was looking for and while it has a lot of great options, I’m struggling to find something that works for this.