Use variables instead of number input helper?

In an automation I have this sequence whose purpose it is to slowly fade a light to a certain brightness but to abort if the brightness is changed manually:

sequence:
  - service: input_number.set_value
    data:
      value: "{{ states.light.tubes.attributes.brightness }}"
    target:
      entity_id: input_number.tubes_fade_last_brightness
  - repeat:
      until:
        - condition: or
          conditions:
            - condition: numeric_state
              entity_id: light.tubes
              attribute: brightness
              above: 178
            - condition: template
              value_template: >-
                {{ states.light.tubes.attributes.brightness !=
                int(states.input_number.tubes_fade_last_brightness.state) }}
      sequence:
        - service: input_number.set_value
          data:
            value: "{{ int(states.input_number.tubes_fade_last_brightness.state) + 5 }}"
          target:
            entity_id: input_number.tubes_fade_last_brightness
        - service: light.turn_on
          data:
            brightness: "{{ states.input_number.tubes_fade_last_brightness.state }}"
          target:
            entity_id: light.tubes
        - delay:
            hours: 0
            minutes: 0
            seconds: 2
            milliseconds: 0

In an effort to reduce complexity (and to learn more about Home Assistant scripts) I was wondering if instead of the helper input_number.tubes_fade_last_brightness I could use variables?

Of course if there’s a completely different way to achieve the same goal I’m also interested to hear it.

I’ve wondered the same. So far I’ve been using hidden helpers. Variables would be more elegant is some cases.

Also, the input helpers spam the logbook.

You can have something like this:

sequence:
  - variables:
      last_brightness: "{{ state_attr('light.tubes', 'brightness') | int(0) }}" 
  - repeat:
      until:
        - condition: or
          conditions:
            - condition: numeric_state
              entity_id: light.tubes
              attribute: brightness
              above: 178
            - condition: template
              value_template: "{{ state_attr('light.tubes', 'brightness') | int(0) != last_brightness }}"
      sequence:
        - variables:
            last_brightness: "{{ last_brightness + 5 }}"
        - service: light.turn_on
          data:
            brightness: "{{ last_brightness }}"
          target:
            entity_id: light.tubes
        - delay:
            hours: 0
            minutes: 0
            seconds: 2
            milliseconds: 0

Please be aware that variables will disappear when your automations are reloaded, or when it starts again, or when Home Assistant is restarted, etc… It lives only for the time that instance of your automation is running. Which is probably ok in your case.
If your light supports transition, you should use that instead… Maybe you try to set the brightness to 0 with a transition of 30s (or whatever if the total time you want) and it might be easier and smoother than using this automation.

So, you can try either this:

          service: light.turn_off
          data:
            transition: 30
          target:
            entity_id: light.tubes

Or this:

          service: light.turn_on
          data:
            transition: 30
            brightness: 0
          target:
            entity_id: light.tubes

By the way, you will make much easier to help if you share your full automation instead of just a part of that.

1 Like