Range for value in payload_template

I have setup current sensor template for max current to used in ev charging.
I want to send this to mqtt as payload, but my sensor gives value greater than should be send.
Value should be betwene 6 to 16. I tried to setup ELSE IF THEN in payload_template but could get it to work.

service: mqtt.publish
metadata: {}
  qos: "2"
  retain: false
  payload_template: "{{ states('sensor.lataukseen_kaytettava_max_virta') }}"
  topic: evcc/loadpoints/2/maxCurrent/set

How should i do it, is there other ways than ELSE IF THEN?

This will limit your payload to a value between 6 and 16:

payload_template: "{{ ([6, states('sensor.lataukseen_kaytettava_max_virta'), 16]|sort)[1] }}"

Let your sensor value be represented by x. The template sorts the list of numbers, [6, x, 16] by increasing size then picks the middle value.


Sensor value too small (0):
[6, 0, 16] → [0,6,16] → 6

Sensor value in range (12):
[6,12,16] → [6,12,16] → 12

Sensor value too big (200):
[6,200,16] → [6,16,200] → 16

Thank you, got that working now, i tried that earlier, but i think i was missing one of quotes…
What does [1] stand for?

You use the index operator [ ] to access an item or items in a list. To select a single item you just put the item’s index number in the operator. Jinja indexing starts at 0, so [1] selects the second item from the list.