Help needed with setting input_select in a view using java

I’m trying to conditionally set the option value for my blind as shown below.
If the current state is 0 or 100, I want to open else close it.
The code works if I just set option: ‘open’ or option: ‘close’ but if I try to return this value in the script below it fails. Is there anything obviously wrong with my code?
Note that I’m trying 2 different methods for my if statements but neither method seems to work.

thanks

      tap_action:
        action: call-service
        service: input_select.select_option
        service_data:
          option: |
            [[[
              let cmd = 'close';
              if (input_select.office_blinds.state == '0'){
                cmd = 'open';
              }
              if is_state('input_select.office_blinds','100'){
                cmd = 'open';
              }
              return cmd;
            ]]];  
        target:
          entity_id: input_select.office_blinds

Which card is this?

Very few core cards support templating, JavaScript or jinja.

Put your action in a script and call that as the tap action (scripts are services).

If that is a part of a custom:button-card, I think you’re mixing Javascript with Jinja. Try


              if (states['input_select.office_blinds'].state == '0'){
                cmd = 'open';
              }
              if (states['input_select.office_blinds'].state == '100'){
                cmd = 'open';
              }
              return cmd;
            ]]];  

or shorter:


  [[[
    let select = states['input_select.office_blinds'].state;
    let cmd = '';
    if (select == '0' || select == '100'){
      cmd = 'open';
    }
    else {
      cmd = 'close';
    }
    return cmd;
  ]]]

or even shorter:


  [[[
    let select = states['input_select.office_blinds'].state;
    let cmd = '';
    return (select == '0' || select == '100')
      ? cmd = 'open'
      : cmd = 'close';
  ]]]

The double pipe || stands for or

Thanks!
That Worked!

I wish Home Assistant supported only one language :slight_smile:
Too confusing for me :wink:
I guess that’s the price you pay for something that is so versatile.