Use response of shell_commandas value_template for switch

Hi,

I’m trying to control a docker container via HA.
The shell_commands already work, but I want to incorporate it as a switch and for that use the response_value of a status command.

the shell commands are:

start_container: ssh ... docker compose up -d
stop_container: ssh ... docker compose down
status_container: ssh ... docker compose  ps --format {{ "{{.State}}" }}

so the status command returns the following, if the container is running:

stdout: running
stderr: ""
returncode: 0

The switch is currently like this:

- platform: template
    switches:
      docker_container:
        turn_on:
          action: shell_command.start_container
        turn_off:
          action: shell_command.stop_container

Now I want to use the status_container command for the value_template of the switch.

I searched all the template documentation, but did not find a way to do it. Is there one?

I use trigger-based template sensors, which allows to execute actions returning data before assessing the actual template.

The doc does not says otherwise for template switches, so I assume it could work the same way.

Ex:

- trigger:
    - platform: time
      at: "06:32:00"
    - platform: homeassistant
      event: start
  action:
    - service: shell_command.tibi_get_history
      response_variable: tibi_history
  sensor:
    - name: Tibi organique ytd
      unique_id: tibi_organique_ytd
      state_class: total
      device_class: weight
      unit_of_measurement: kg
      state: >
        {% set value_json = tibi_history['stdout'] | from_json %}
        {% if "history" not in value_json %}
          unavailable
        {% else %}
          {% set rest_history = value_json.history[0].card.products | selectattr('status', "eq", 'active') | selectattr('fraction', "eq", 'GFT') | map(attribute='emptyings') | list %}
          {% set ns = namespace(weight=0) -%}

          {% for k in rest_history[0] %}
            {% set ns.weight = ns.weight +  rest_history[0][k]["weight"]| float %}
          {% endfor %}
          {{ ns.weight }}
        {% endif %}