If-then template checking passed attribute

I struggle to find a working configuration in a script how to write the if template statement checking the passed data from the custom button.

I am calling the script with the custom button tap action:

tap_action:
  action: call-service
  service: script.rpi_white_container
  data:
    cont_name: firefox4
    cont_status: |
      [[[ 
        var status =states['sensor.container_firefox4'].state; 
        return status
      ]]]

I know i received the data in the script, because in the trace it shows:

cont_name: firefox4
cont_status: Up

and this code works:

service: shell_command.rpi_white_containers_start
data:
  cont_name: "{{ cont_name }}"

However, I need the if statement to check if the “cont_status” is “Up” or “Exited” so I should call a different shell command, and the following script code does not work, i guess the problem is in the if statement:

if:
  - condition: template
    value_template: |
      {{ cont_status }} = "Up"
then:
  - service: shell_command.rpi_white_containers_stop
    data:
      cont_name: "{{ cont_name }}"
else:
  - service: shell_command.rpi_white_containers_start
    data:
      cont_name: "{{ cont_name }}"

Any help is greatly appreciated.

if:
  - condition: template
    value_template: |
      {{ cont_status  == "Up" }}
then:
  - service: shell_command.rpi_white_containers_stop
    data:
      cont_name: "{{ cont_name }}"
else:
  - service: shell_command.rpi_white_containers_start
    data:
      cont_name: "{{ cont_name }}"
1 Like

You can simply template the service call. No need for if-then-else.

  - service: "shell_command.rpi_white_containers_{{ iif(cont_status == 'Up', 'stop', 'start') }}"
    data:
      cont_name: "{{ cont_name }}"
1 Like

Thank you very much, I feel so silly now, should get some sleep
sorry for wasting the time on such silly stuff. :man_facepalming:

no worries. just keep the mindset that {{ }} tells the system to evaluate what’s inside. you wanted it to evaluate the == as well, so it belongs inside.

1 Like

Thank you very much again.