Addition several input numbers in sensor template

I want to make a remaining time sensor. I can do it with one Input number, but I have 5 Input numbers (input_number.1, input_number.2 etc.) and 5 switches (with the different input number values). I’d like to cumulate them (input_number.1 + input_number.2 +…+input_number.5, and switch.switch1…switch.switch5) and then calculate the remaining time for all of them. With 1 input_number and 1 switch I have this working code below, but I don’t know how to add the 5 numbers together to calculate.

  remaining time:
    friendly_name: Remaining
    entity_id:
      - input_number.1
      - sensor.time
      - switch.switch1
    value_template: >
      {% if is_state('switch.switch1', 'on') %}
        {{ [ (states('input_number.1')|int - (as_timestamp(now()) - as_timestamp(states.switch.switch1.last_changed))/60)|round(0) ,0 ] | max }}
      {% else %}
        0
      {% endif %}
    unit_of_measurement: "min"

Not really sure what you are trying to achieve, but did you had a look at

Not exactly. Some details. I’m working on my irrigation system. I have zones (these are the switches) and I have different programs (script). Each program can run the same switches for different time duration. It works perfecly. The predefined running time of the zones (switches) are set by input buuleans (for each swich in each program). Thet wotks great.
I’d like to make a “remaining time” sensor which shows the remaining time of the program (not the actual running switch) based on the input_number and the elapsed time. The example above shows only one zone’s (switch) remaining time, but I’d like to see the remaining time of the whole program.

In the template editor (under Developer Tools):

{% set total = states('input_number.1')|float +
               states('input_number.2')|float +
               states('input_number.3')|float +
               states('input_number.4')|float +
               states('input_number.5')|float %}
{{ total }}

Then do what you want with the total variable.

The |float converts the state (which is always a string) into a number to allow addition.

The neat layout above is optional: you can just put them on the same line with no breaks.

1 Like

Thanks a lot! I’ll give a try. One remaining issue that my code counts witch switch1.last_changed, but I think I can change it to script last changed (the script runs the switch sequences).

If that doesn’t work, and you want the most recent, you could use:

{{ [states.switch.switch1.last_changed,
    states.switch.switch2.last_changed,
    states.switch.switch3.last_changed,
    states.switch.switch4.last_changed,
    states.switch.switch5.last_changed]|max }}

The brackets [...] denote a list, and the max filter gives you the largest value out of that list.

Then I should use “min” since I need which started earlier (total time - first switch start time). Am II right?

Yes, min will give you the smallest, assuming all have a valid value. You’ll still have to do the as_timestamp conversion on that template, of course.

1 Like

Thanks again! Finally it works with script. Now I have to find out to leave the decimals totally 15 min instead of 15.0 min

Use |int instead of |float. Paste the code you’re using if you still have a problem.

I was too fast. Unfortunately counting from script run doesn’t work correctly. Can you please help, how can I change the following with your solution (looking for the first activated switch)? I know I have to change somehow the “as_timestamp(states.sensor.program_1_total_time.last_changed”

  program_1_remainingo:
    entity_id:
      - sensor.total_time
      - sensor.time
      - script.program_1
    value_template: >
      {% if is_state('script.program_1', 'on') %}
        {{ [ (states('sensor.total_timeo')|int - (as_timestamp(now()) - as_timestamp(states.sensor.program_1_total_time.last_changed))/60)|round(0) ,0 ] | max }}
      {% else %}
        0
      {% endif %}
    unit_of_measurement: "min"
 program_1_remainingo:
    entity_id:
      - sensor.total_time
      - sensor.time
      - script.program_1
    value_template: >
      {% if is_state('script.program_1', 'on') %}
        {% set change_time = [states.switch.switch1.last_changed,
                              states.switch.switch2.last_changed,
                              states.switch.switch3.last_changed,
                              states.switch.switch4.last_changed,
                              states.switch.switch5.last_changed]|min %}
        {{ [ (states('sensor.total_timeo')|int - (as_timestamp(now()) - as_timestamp(change_time))/60)|round(0) ,0 ] | max }}
      {% else %}
        0
      {% endif %}
    unit_of_measurement: "min"