Use cycling input select values in template

i’ve got a standing fan with an IR remote i am trying to automate. it has 8 speeds that cycle up or down (pressing ‘down’ when on ‘1’ goes to ‘8’). i want the IR command to be fired and the input select to be updated (so i can monitor the state) when i press and ‘up’ or ‘down’ button on the ui

I was trying to create and automation to send the code to the IR remote, but my brain is stuck on how to template this to make it simple. i had tried

{%- if trigger.from_state.state < trigger.to_state.state -%} select_next
      {%- else -%} select_previous {%- endif -%}

but that doesn’t account for the cycling “up” from ‘8’ to ‘1’ and down from ‘1’ to ‘8’. I could make a separate script just for this setting as its the only ‘cyclical one’ but i’m sure there’s a better way. any help is appreciated.

as a service call…

- service: >
    {%- if trigger.from_state.state | float < trigger.to_state.state | float -%}
      input_select.select_next
    {%- else %}
      input_select.select_previous
    {%- endif %}
  target:
    entity_id: input_select.abc

or

- service: >
    input_select.select_{{ 'next' if trigger.from_state.state | float < trigger.to_state.state | float else 'previous' }}
  target:
    entity_id: input_select.abc

or

- service: >
    input_select.select_{{ iif(trigger.from_state.state | float < trigger.to_state.state | float, 'next', 'previous') }}
  target:
    entity_id: input_select.abc

hey thanks for the quick response. i’m not sure i’m understanding though. won’t this give me the same problem if its comparing the ‘from’ state to the ‘to’ state since 8 > 1, but the remote actually ‘decreases’ from 1 to 8 in a cycle if i press it when its on 1?

just call that out specifically then

{% set before = trigger.from_state.state | float %}
{% set after = trigger.to_state.state | float %}
{% if before == 8 and after == 1 %}
  input_select.select_next
{% elif before == 1 and after == 8 %}
  input_select.select_previous
{% elif before < after %}
  input_select.select_next
{% else %}
  input_select.select_previous
{% endif %}

Ah ok got it! Thank you very much