Count mqtt topics

Hello,
Glances is publishing my docker containers but there is no value for the count of the running ones. I could either count the number of topics in glances/server/55ee9907d6e6/containers or count the number of status=running messages with wildcard: glances/server/55ee9907d6e6/containers/+/status

Below image is from process explorer:

I need a sensor in home assistant with the current running number of containers.
If anyone can help with the jinja that would be great.

Happy to be proved wrong, but I don’t think that’s possible within HA: the information simply isn’t available. You’d need something external like mosquitto to count up the topics; or easier, a command line sensor to query the docker host.

Thank you, then it is no wonder I could not make it.
So I created an ssh key and placed it in my server for authorized_keys. I can ssh into my server from home assistant without authorization now.

I have checked the command line docs and created the below entry in my config:

command_line:
    sensor:
      name: Running Containers Count
      command: 'ssh -i /config/ssh_key -o StrictHostKeyChecking=no [email protected] "docker ps -q | wc -l"'
      unit_of_measurement: "containers"
      scan_interval: 60

Still something aint right. The sensor is zero. Any idea?

Are you sure you tested ssh and set up the key from the HA container as opposed to the host? I have a similar sensor (!included under command_line):

- sensor:
    command: "ssh -i /config/keys/nas_rsa [email protected] /usr/local/bin/docker stats --no-stream"
    unique_id: bddd9232-a164-4285-8eca-f5bd093cbc1a
    name: Docker stats
    scan_interval: 600
    value_template: [TEMPLATE TO EXTRACT ITEMS]

I occasionally need to attach to the HA container to confirm the key change when the Docker system is updated.

Odd that it’s not unknown. Are you sure you’re connected to the correct Docker?

Here I’m running the command in the home assistant terminal and getting a response:

I’m not sure how HA interprets the return value. Is it a string?

So is this format wrong? do I need a value template to parse the response?

command_line:
  - sensor:
      name: Running Containers Count
      command: 'ssh -i /config/ssh_key -o StrictHostKeyChecking=no [email protected] "docker ps -q | wc -l"'
      unit_of_measurement: "containers"
      scan_interval: 60

If it looks like a number, you’re fine. My script returns some data that needs manipulating.

Are there any errors in the logs? Try removing the unit of measurement until you’re getting a number back.

I haven’t used the add-on (I’m running HA in a container), but I’m still not convinced that your SSH session in the screenshot is running in the HA container as opposed to the host. What do you get back if you execute ps at that prompt?

This is what I get from a console attached to my HA container:

NAS:/config# ps
PID   USER     TIME  COMMAND
    1 root      0:00 /package/admin/s6/command/s6-svscan -d4 -- /run/service
   16 root      0:00 s6-supervise s6-linux-init-shutdownd
   17 root      0:00 /package/admin/s6-linux-init/command/s6-linux-init-shutdownd -d3 -c /run/s6/basedir -g 3000 -C -B
   25 root      0:00 s6-supervise s6rc-oneshot-runner
   26 root      0:00 s6-supervise s6rc-fdholder
   34 root      0:00 /package/admin/s6/command/s6-ipcserverd -1 -- /package/admin/s6/command/s6-ipcserver-access -v0 -E -l0 -i data/rules -- /package/admin/s6/command/s6
   65 root      0:00 s6-supervise home-assistant
  142 root      4h09 python3 -m homeassistant --config /config
  173 root      0:45 /bin/go2rtc -c /tmp/go2rtc_1l1ia7ay.yaml
 8643 root      0:00 bash
 8651 root      0:00 ps

I also need to specify the path to my Docker executable when connecting via ssh from HA, hence the /usr/local/bin/docker.

I was under the impression that this is HA shell. Is there a way to connect to HA directly using SSH tho? How to test my command on host?

I think you’re right, although your installation (HAOS?) is different from mine.

Perhaps try a simpler command first, like uname (and remove the unit of measurement). That’ll tell you if ssh is working from the sensor config.

Its working. HA needs a restart. I only reloaded the config each time. The parameter appeared but the command line was not doing anything

1 Like