Input_number to trigger button repeated difference (volume button to slider)

I have a seemingly simple logical solution, but could not find any way to implement it and have searched high and low. I would appreciate any advice and input.

Summary: I have buttons for volume+/- via Broadlink IR and want to translate this to a volume slider.

Idea:

  • Create input_number (to use as slider on the dashboard)
  • Some event/automation that triggers when input_number changes (sliding the volume). (i got stuck here and could not even get this to work)
  • Get the difference from the new value to the old value - perhaps keep a duplicate input_number for the previous value.
  • Execute the volume+/- service/button X times. X being the difference from the previous step.
  • Update the “previous value” to the new value.

e.g.

  • Slide from 60 to 40
  • X = -20 (X < 0 means Vol- (X > 0 Vol+))
  • So call Vol- button 20 times.

Is this even possible or am I chasing my own tail. I have seen some posts trying other methods to achieve a similar thing, but no success. This seems like a pretty common use case.

I’ve never used Broadlink, so I can’t be sure the remote service is 100% right, but this should give you an idea of how to approach it:

trigger:
  - platform: state
    entity_id: input_number.volume_slider
    not_from:
      - unknown
      - unavailable
condition: []
action:
  - variables:
      count: '{{ trigger.to_state.state | int(0) - trigger.from_state.state | int(0) }}'
      command: '{{ iif( count > 0, 'volume+', 'volume-') }}'
  - service: remote.send_command
    target:
      entity_id: remote.x
    data:
      device: z
      command: '{{ command }}'
      num_repeats: '{{ count }}'

Since there’s no true feedback of the volume status, and it can be changed by other remotes or service calls, it’s possible to get into a situation where the actual volume is too high but the slider cannot lower it and vice versa.

1 Like

Wow! This worked. Thank you so much. I never saw example or documentation with the variables.
Missing closing bracket ) in the iif cost me like an hour :sweat_smile: but got it working.
Thank you so much.

Note for others:
This works, but it is a bit laggy and does not maintain proper state if changed from other sources as the answer mentioned, but I am fine with that for now and is exactly what I needed.