Shell command stucks on use of variables in automation

docker management stucks on use of variables in automation

Goal: to manage (start/stop) my 14 dockercontainers within homeassistant.

Problem: The use of variables to start and stop dockers so that i dont have to configure 14 start and stop services and 28 conditions in an automation
I have one(on and off) hard coded scenario working for the nginx docker, but want to use values from a list, to manage all dockers, without to much code…

Current parts:

frontend:
two choise lists
1. input_select.dockeraction: The docker action, contains start and stop
2. input_select.dockercontainers, dockernames,like the one mentioned, nginx

frontend

              - type: entity
                entity: input_select.dockercontainers
                name: Docker containers
              - type: entity
                entity: input_select.dockeraction
                name: Docker actions

Automation with 3 scenarios, with each two conditions, start or stop to try out my dockermanagement
The first scenario, with two conditions start and stop, does work. But needs 13 extra scenarios and 26 conditions to manage the other dockers
Scenario 2 and 3, conditions 3-4, or 5-6 are a first trial to use the choise lists an pass the choise to a service. The scenarions use a different general service. Difference between scenario 2 and 3 is the use of internal variables in the automation.

my automation

alias: dockeraction
description: ""
trigger:
  - platform: state
    entity_id:
      - input_select.dockeraction
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_select.dockeraction
            state: start
            enabled: false
        sequence:
          - service: shell_command.docker_start_nginx
            data: {}
      - conditions:
          - condition: state
            entity_id: input_select.dockeraction
            state: stop
            enabled: false
        sequence:
          - service: shell_command.docker_stop_nginx
            data: {}
      - conditions:
          - condition: state
            entity_id: input_select.dockeraction
            state: start
            enabled: true
        sequence:
          - service: shell_command.docker_general
            data:
              dockeraction: "{{ states('input_select.dockeraction') }}"
              dockercontainer: "{{ states('input_select.dockercontainers') }}"
      - conditions:
          - condition: state
            entity_id: input_select.dockeraction
            state: stop
            enabled: true
        sequence:
          - service: shell_command.docker_general
            data:
              dockeraction: "{{ states('input_select.dockeraction') }}"
              dockercontainer: "{{ states('input_select.dockercontainers') }}"
      - conditions:
          - condition: state
            entity_id: input_select.dockeraction
            state: start
            enabled: false
        sequence:
          - service: shell_command.docker_general
            data:
              dockeraction: " {{ data_a }} "
              dockercontainer: " {{ data_c }} "
      - conditions:
          - condition: state
            entity_id: input_select.dockeraction
            state: stop
            enabled: false
        sequence:
          - service: shell_command.docker_general
            data:
              dockeraction: " {{ data_a }} "
              dockercontainer: " {{ data_c }} "
variables:
  data_a: "{{ states('input_select.dockeraction') }}"
  data_c: "{{ states('input_select.dockercontainers') }}"
mode: single

My Entries in configuration.yaml
two specific shells for starting / stopping nginx docker, these two works!
one general shell: docker_general for managing all dockers at an easy way, this one has to work with variables but dont yet…

shell_command: 
  docker_start_nginx: sshpass -f /config/bash/pw.txt ssh -o StrictHostKeyChecking=no pi@rpiserverk "docker start nginx"
  docker_stop_nginx: sshpass -f /config/bash/pw.txt ssh -o StrictHostKeyChecking=no pi@rpiserverk "docker stop nginx"
  docker_general: sshpass -f /config/bash/pw.txt ssh -o StrictHostKeyChecking=no pi@rpiserverk "docker {{dockeraction}} {{dockercontainer}}"

My environment
rpi 4
Operating System: Raspbian GNU/Linux 11 (bullseye)
Kernel: Linux 6.1.21-v8+
Architecture: arm64

Docker Homeautomation container, 64 bit
    ghcr.io/home-assistant/raspberrypi4-64-homeassistant:latest
    Home Assistant 2023.10.0.dev20230906
    Frontend-versie: 20230906.0.dev - latest

Read a lot about automation, variables etc. But stuck with this result… still a rookie after months…

A little bit further.
Got the basics working with variables!
Last problem to solve is to send an extra variable in the dataset: server.
Some dockers are on another raspberry, so i have to send the server in the dataset.
Problem is that the content of variable server is not a fixed value but 7 dockers are on server_a and other 7 on B.

alias: dockeraction2
description: ""
trigger:
  - platform: state
    entity_id:
      - input_select.dockeraction
condition: []
action:
  - service: shell_command.docker_general
    data:
      dockeraction: "{{ states('input_select.dockeraction') }}"
      dockercontainer: "{{ states('input_select.dockercontainers') }}"
mode: single

And solved it…
Can manage all my dockers now…
for those who needs this, here is the code (automation and part of configuration.yaml
serverpart to make the server variable where the dockers are managed is partially working. At the moment only for the local (host) server. Another remote server has still some issues…

alias: dockeraction
description: ""
trigger:
  - platform: state
    entity_id:
      - input_select.dockeraction
condition: []
action:
  - service: shell_command.docker_general
    data:
      dockeraction: "{{ states('input_select.dockeraction') }}"
      dockercontainer: "{{ states('input_select.dockercontainers') }}"
      server: |
        {% if is_state('input_select.dockercontainers', 'motioneye') %}
          rpiservers
        {%-elif is_state('input_select.dockercontainers', 'nextpvr') %}
          rpiservers
        {%-elif is_state('input_select.dockercontainers', 'joomla') %}
          rpiservers
        {%-elif is_state('input_select.dockercontainers', 'squeezebox') %}
          rpiservers
        {%-elif is_state('input_select.dockercontainers', 'portainer') %}
          rpiservers
        {%-elif is_state('input_select.dockercontainers', 'unify') %}
          rpiservers
        {%-elif is_state('input_select.dockercontainers', 'wireguard') %}
          rpiservers
        {%-elif is_state('input_select.dockercontainers', 'watchtower') %}
          rpiservers
        {% else %}
          rpiserverk
        {% endif %}
mode: single

shell_command: 
  docker_general: sshpass -f /config/bash/pw.txt ssh -o StrictHostKeyChecking=no pi@{{server}} "docker {{dockeraction}} {{dockercontainer}}"