REST api with UI slider not working

I am trying to change a setting in WLED via the WLED api through the home assistant UI by means of a slider. The slider value shall be passed via a HTTP post request (with the HA REST API) enabling seamless update of some WLED settings (that are part of a usermod, so not integrated in the HA WLED integration).

image

For this I’ve created two sliders and REST configuration as follows:

input_number:
  aan_tijd:
    name: aan tijd
    initial: 5
    min: 1
    max: 60
    step: 1
  wipe_speed:
    name: wipe snelheid
    initial: 150
    min: 20
    max: 1000
    step: 10
        
rest_command:
  staircase_animate:
    url: "http://192.168.1.61/json/state"
    method: post
    content_type: "application/json"
    payload: '{"staircase":{"segment-delay-ms": {{ wipe_speed }},"on-time-s": {{ aan_tijd }}}}'

When I call the rest_command.staircase_animate service in the developer tools service and input, for example the following:

{
  "wipe_speed":"150",
  "aan_tijd":"10"
}

This direct command works well.

I seem however not to be able to get the slider values to be passed to the REST api and then call the rest_command manually, enabling the updated slider values to be sent to WLED.

Any suggestion would be very welcome.

You need an automation that triggers when you change the slider (input_number I assume?).

Trigger: input number changes
Action: Calk your rest command with value from input slider

You need to fix your template:

payload: '{"staircase":{"segment-delay-ms": {{ states("input_number.wipe_speed") }},"on-time-s": {{ states("input_number.aan_tijd") }} }}'

Or maybe this if it is expecting quoted numbers (strings) for the delay and on time:

payload: >
  {"staircase":{"segment-delay-ms": "{{ states('input_number.wipe_speed') }}","on-time-s": "{{ states('input_number.aan_tijd') }}" }}

The automation is the next step when the input slider state changes, indeed. Currently I call the update towards WLED via the “uitvoeren” (apply) button as you see in the image above.

Thank you Tom, that is very helpful.

I did add the “input_number” already in earlier attempts to couple the input number variable into the REST command, but in a different way and without success. Now I’ve tried your approaches, but both do not bring any result. It now looks like:

input_number:
  aan_tijd:
    name: aan tijd
    initial: 5
    min: 1
    max: 60
    step: 1
  wipe_speed:
    name: wipe snelheid
    initial: 150
    min: 20
    max: 1000
    step: 10
        
rest_command:
  staircase_animate:
    url: "http://192.168.1.61/json/state"
    method: post
    content_type: "application/json"
    payload: >
      {"staircase":{"segment-delay-ms": "{{ states('input_number.wipe_speed') }}","on-time-s": "{{ states('input_number.aan_tijd') }}" }}

The script that executes the REST API looks very simly like:

trap_animatie:
  alias: trap animatie
  sequence:
  - service: rest_command.staircase_animate
    data: {}
  mode: single
  icon: mdi:stairs

This is giving me headaches and is a steep learning to understand the REST, STATES and JSON formatting. Further suggestions would be utmost welcome.

@Burningstone I’ve been humbling around a bit; I found out that I cannot enter the value from the input slider straight away into the rest command as defined in configuration yaml, so you are absolutely right; The rest API is now executed via an automation.

This is how it now works, hopefully an example that others can benefit from:

Configuration.yaml:

input_number:
  aan_tijd:
    name: aan tijd
    initial: 5
    min: 1
    max: 60
    step: 1
  wipe_speed:
    name: wipe snelheid
    initial: 150
    min: 20
    max: 1000
    step: 10
        
rest_command:
  staircase_animate:
    url: "http://192.168.1.61/json/state"
    method: post
    content_type: "application/json"
    payload: '{"staircase":{"segment-delay-ms": {{ wipe_speed }},"on-time-s": {{ aan_tijd }}}}'

Automation:

- id: '1613663986382'
  alias: '[staircase] animation changed'
  description: ''
  trigger:
  - platform: state
    entity_id: input_number.aan_tijd
  - platform: state
    entity_id: input_number.wipe_speed
  condition: []
  action:
  - service: rest_command.staircase_animate
    data:
      wipe_speed: '{{ states(''input_number.wipe_speed'') | int }}'
      aan_tijd: '{{ states(''input_number.aan_tijd'') | int }}'
  mode: single

That’s exactly what I was talking about :slight_smile: glad that you got it to work

1 Like