Change an input number from template?

Im new to home assitant and have done som succesful copy pasting of stuff without really knowing exacty how everything works…

I have a slider defined in configuration.yamal

input_number: 
  statesw: 
    name: Stateinverter
    initial: 2
    min: 2
    max: 3

This works fine for manually changing from 2 to 3…

I also have a template sensor that uses the value of this state togeter with other values to decide what to do. I would like to change the input_number.statesw in this template, I have added a # where I want it to happen… is this possible??

template: 
    sensor:
      - name: "switchstate"
        state: >
          {%set AA=states('sensor.current_l1') | float %}
          {%set Maxvalue=states('input_number.slider1') | float%}
          {%set BB=(Maxvalue-AA)%}
          {%if AA<20 and states('input_number.statesw')|int== or states('input_number.start_ladd')|int==22%} 
            3
            #Here I want my slider to change to 3 !!! 

          {% else %}
            2
          {% endif %}
          

That’s a job for an automation, using a service call to adjust the value.

ok… dont know what you mean really but I guess its not possible to write something here then… .a bit frustrating I must say… .

Where did you get the idea that it’s possible to do what you tried to do?

There’s not a single example of that anywhere in the documentation or in this community forum. Why not? Because that’s not how you set an entity’s value.

The value of an entity like an input_number can be set using a service call like input_number.set_value.

A service call is part of Home Assistant’s scripting language, not Jinja2’s templating language.

Im new to home assistant so Im really confused at the moment

It felt (from a c++ backgground (simple stuff…)) reasonable that after a couple of if statements its possible to do more than setting swithstate to 3 in this case.

Not sure why you would assume that what’s possible in C++ is also possible in a templating language like Jinja2.

Have you reviewed the Templating documentation? If you haven’t, I recommend that you set aside some time to study it and learn how Home Assistant employs Jinja2.

Perhaps what you want is to create an automation that detects when the value of sensor.switchstate changes to 3 then sets the input_number to 3. Does that meet your requirements?

well I think it is reasonable to assume stuff based on a best guess, many things are similar but in this case I’m way off that’s for sure :slight_smile:

Anyway, I have managed to define an automation that triggers on a change in the sensor and changes the slider just as you purpose,