Docker setup with Shelly and CoIoT

Hi,

i have set up home assistant through docker and currently I’m a bit stuck with the Shelly integration and CoIoT: I have installed a Shelly 2.5 and noticed, that it takes HA a very long time to pick up changes on the switches (up to 30 seconds). From reading around, I found out that I had to enable CoIoT in the Shellies and point them to the HA instance and the port 5683. I set up the Shelly accordingly, rebooted it and exposed the port 5683 through my docker-compose.yml so it could talk to HA. but the issue still persists. I went to see whats running on port 5683, but there isn’t actually running anything on that port within the HA container, that probably explains why it does not work.

This is the guide i followed:

I made sure nothing was running on the port in the container:


docker exec -it 5353a0f6d859 sh
/config # nc -lv -p 5683 0.0.0.0
listening on 0.0.0.0:5683 ...

/config # nc -lv -p 8123 0.0.0.0
nc: bind: Address in use # <- expected, the webserver runs on that port

How can I get HA to listen on that port and do whatever it is supposed to do there?

1 Like

Update: I found out that HA actually IS listening on that port, but on UDP:

/config # netstat -an | grep 5683
udp        0      0 0.0.0.0:5683            0.0.0.0:*

so I updated my docker-compose.yml accordingly:

    ports:
      - "5683:5683/udp"

and made sure the configuration worked:

$ docker-compose ps
             Name                Command   State           Ports
-------------------------------------------------------------------------
home-assistant_homeassistant_1   /init     Up      0.0.0.0:5683->5683/udp

However, the issue STILL persists, it takes forever until HA picks up the state change of any of the switches. Is there a way I can debug what HA is receiving there so I can find out if the Shelly is actually broadcasting stuff there?

Update 2:
I started nc locally and pointed the Shelly to my computers ip:port to see if it broadcasts, and I get immediate feedback whenever I toggle the switches:

$ nc -ul 5683
       �	SHSW-25#C45BBE608884#2�C��
�{"G":[[0,9103,0],[0,1102,"stop"],[0,1103,-1],[0,2101,1],[0,2102,""],[0,2103,0],[0,2201,0],[0,2202,""],[0,2203,0],[0,4102,0.00],[0,4104,0],[0,6103,"normal"],[0,3104,55.00],[0,6101,0],[0,9101,"roller"],[0,4108,227.14]]}P
��cits�
       �	SHSW-25#C45BBE608884#2�C��
                                          �{"G":[[0,9103,0],[0,1102,"stop"],[0,1103,-1],[0,2101,0],[0,2102,""],[0,2103,0],[0,2201,0],[0,2202,""],[0,2203,0],[0,4102,0.00],[0,4104,0],[0,6103,"normal"],[0,3104,55.00],[0,6101,0],[0,9101,"roller"],[0,4108,227.14]]}P
��cits�
       �	SHSW-25#C45BBE608884#2�C��
                                          �{"G":[[0,9103,0],[0,1102,"stop"],[0,1103,-1],[0,2101,1],[0,2102,""],[0,2103,0],[0,2201,0],[0,2202,""],[0,2203,0],[0,4102,0.00],[0,4104,0],[0,6103,"normal"],[0,3104,55.00],[0,6101,0],[0,9101,"roller"],[0,4108,227.14]]}P
��cits�
�{"G":[[0,9103,0],[0,1102,"stop"],[0,1103,-1],[0,2101,0],[0,2102,""],[0,2103,0],[0,2201,0],[0,2202,""],[0,2203,0],[0,4102,0.00],[0,4104,0],[0,6103,"normal"],[0,3104,55.00],[0,6101,0],[0,9101,"roller"],[0,4108,227.14]]}

So the Shelly is behaving as expected and broadcasting to the configured target, however my HA instance does not seem to pick up on it.

I solved it by running the HA container in host network and privileged mode, which is very unsatisfying, but I could not get the udp port forward to the container to work otherwise

in docker-compose.yml for the HA service:

    network_mode: host
    privileged: true
1 Like

Ahhhhh so following the instructions worked. Amazing.

I came across the same issue, but things seems to work well after opening port 5683/udp. Thanks for that tip!

You can check if packets arrive in your container using tcpdump -i eth3 udp port 5683 -vv -X. You probably need to install tcpdump using apk install tcpdump, and change eth3 to the right interface in your container. You can list all interfaces in the container using ip a.

2 Likes

I just wanted to add my compose. I found adding the port twice, once with and once without the /udp flag did the trick. I tried both separately and it didn’t work.


  homeassistant:
    container_name: hass
    env_file:
      - stack.env
    image: homeassistant/home-assistant
    volumes:
      - /data/DBs/HASS:/config
      - /etc/localtime:/etc/localtime:ro
    ports:
      - 192.168.41.5:8123:8123
      - 192.168.41.5:4357:4357
      - 192.168.41.5:5683:5683
      - 192.168.41.5:5683:5683/udp
    restart: unless-stopped
1 Like

The installation instructions for HA container require network=host and privileged.

docker run -d \
  --name homeassistant \
  --privileged \
  --restart=unless-stopped \
  -e TZ=MY_TIME_ZONE \
  -v /PATH_TO_YOUR_CONFIG:/config \
  --network=host \
  ghcr.io/home-assistant/home-assistant:stable

I confirm that it works like a charm only adding 5683:5683 and 5683:5683/udp!

just a side note: restart: unless-stopped does not bring the container back up if you stop the docker daemon or reboot the pc/server!
I had a very bad day after a reboot of our company dev server because my colleague set all the containers to that restart policy :sweat_smile:
From that day on I always use restart: always. If you stop the container manually, it stays stopped.
I suppose it was not a good choice of terms made by docker because I see unless-stopped too often.

I’ve had the same problem for a few days, all the Shellys don’t connect anymore… I inserted the CoIoT on port 5683 into the Shellys, but it didn’t solve it. HA is on Docker on Windows, should I open ports on Docker or router?

Windows docker does not support host-mode (ran into the same problem with BroadLinkManager)

In case anyone stumbles across this, too:

My HASS was not listening on 5683, either, and it was driving me mad because the official documentation doesn’t say anything about what to do after setting up CoIoT.

The UDP packets were going into the docker container (checked with tcpdump), but nothing happend in the HASS. I thought the device should appear in the notifications as discovered device. And netstat revealed there wasn’t anyone listening on 5683 in the container.

You need these routes open (firewall/vlan/docker/etc.):

  • 80/tcp: HASS → Shelly
  • 5683/udp: Shelly → HASS

And the HASS will only start listening on 5683 after it has seen the first CoIoT device via the attempt of adding it via port 80.

2 Likes

Well, that was the most valuable information from this thread!
(For me, at least.)

I had to move my dockerized HA to another host (with changed IP, unfortunately) and all was up and running in no time except for the Shellies with their CoIoT stuff.
Since I hate network_mode: host, I have forwarded:

#    network_mode: host
    ports:
      - ${SRV_IP1}:8123:8123
      - ${SRV_IP1}:40000:40000
      - ${SRV_IP1}:161:161/udp
      - ${SRV_IP1}:1900:1900/udp
# since using the mDNS repeater (SmartHome stack) this port must be freed here
#      - ${SRV_IP1}:5353:5353/udp
      - ${SRV_IP1}:5683:5683/udp

which worked for months.

Not after the relocation, though.

I even switched back to the darn net=host, but… nothing.

After reading this thread, I nudged one Shelly via its web interface and the stats (of its colleagues too) came floating in.

Thank you, @uncaught !

1 Like

The documentation says to run docker with host networking. If you don’t, it is up to you to make it work.