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…