Use difference between previous and current value of slider in a for loop

Hello :wave:

I have a slider (name = input_number.slider_fan; min = 0; max = 6; step = 1). If the slider is set to a different value, I want to use the difference between the previous and current value of the slider in a for loop in an automation. In fact, I need two different loops depending on whether dif > 0 and dif < 0.

For example, if the slider is on position 3, and next is set to position 6 (dif = 3), for loop A in the automation should run 3 times. Likewise, if slider is on position 6 and next set to position 2 (dif = -4), for loop B in the automation should run 4 times.

I found several examples in which trigger.from_state.state has been used, but I cannot figure out how to make this run (compute dif). Moreover, but I can be wrong in my understanding of trigger, the trigger for the automation should be any move in the slider, not a specific from.state.

Any help is appreciated. Let me know if more info is needed.

If you don’t specify either from or to, I think the automation will fire every time the value of the input_number changes.

If you don’t specify a from or to state it will trigger on any state or attribute change.

If you specify a null to state it will only trigger on any state change.

In both cases the trigger variables (trigger.from_state and trigger.to_state) will still be available for you to use.

e.g. trigger on all state and attribute changes;

trigger:
  - platform: state
    entity_id: input_number.foo

trigger on all state changes but ignore attribute changes:

trigger:
  - platform: state
    entity_id: input_number.foo
    to:

Thank you! I now understand the trigger part.

But how do I compute the difference between the previous and current slider (input) value in this user case?

  - service: domain.some_service
    target:
      entity_id: domain.object_id
    data:
      some_data: "{{ trigger.to_state.state|float(0) - trigger.from_state.state|float(0) }}"

e.g.

  - service: light.turn_on
    target:
      entity_id: light.my_light
    data:
      brightness: "{{ trigger.to_state.state|float(0) - trigger.from_state.state|float(0) }}"
1 Like

Ok. This means that I first need to make a new helper called ligth.my_light that, in turn, gets the value data or some_data?

No that was just an example. What do you want to do with the difference in value?

i want to run a for-loop for X times, where X is the difference between the current and previous value of the input (slider) (see my original question). Please let me know if that’s unclear.

        repeat:
          while:
            - condition: template
              value_template: "{{ repeat.index <= trigger.to_state.state|float(0) - trigger.from_state.state|float(0) }}"
          sequence:
            - your actions here

It works. Thank you :tada:

Is it also possible to use this value template as condition. So that, when the dif > 0, the first loop is triggered, and else the second loop?

alias: Test
description: ""
trigger:
  - platform: state
    entity_id:
      - input_number.slider_fan
condition: []
action:
  - repeat:
      while:
        - condition: template
          value_template: >-
            {{ repeat.index <= trigger.to_state.state|float(0) -
            trigger.from_state.state|float(0) }}
      sequence:
        - device_id: 5c1d4262d954a2c17c65edf66802538c
          domain: light
          entity_id: 5016ef36490a76606a85e2aff2b97182
          type: flash
        - delay:
            hours: 0
            minutes: 0
            seconds: 2
            milliseconds: 0
  - repeat:
      while:
        - condition: template
          value_template: >-
            {{ repeat.index <= trigger.from_state.state|float(0) -
            trigger.to_state.state|float(0) }}
      sequence:
        - device_id: 5c1d4262d954a2c17c65edf66802538c
          domain: light
          entity_id: 5016ef36490a76606a85e2aff2b97182
          type: flash
        - delay:
            hours: 0
            minutes: 0
            seconds: 5
            milliseconds: 0
mode: single

Yes, use the template condition in an if/then action:

https://www.home-assistant.io/docs/scripts/#if-then

However I don’t think you will need it. Lets say you trigger from 4 to 6.

The first loop will run as the repeat index will start at 0 and 0 is less than 2.

The second loop wont run as 0 is not less than -2.

Now if you trigger from 7 to 4:

First loop wont run ( 0 < -3 is false). Second loop will run (0 < 3 is true).

1 Like