Reading sensor data from device: UDP port 12414

Hi, I am trying to read UDP packets. I have tested reading using python outside Home Assistant and it works. However, within AppDaemon no data is read; the command:
data,addr=self.sock.recvfrom(25)
keeps waiting and does not return anything even though packets are being sent from the device.

This is part of my code for your reference:

import appdaemon.plugins.hass.hassapi as hass
import socket
class Ph803(hass.Hass):
    def initialize(self):
        self.log("start of initialization Ph803 1.0******************")
        self.listen_event(self.test,"PH803_TEST")

    def test(self, event_name, data, kwargs):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.sock.bind(( '', 12414))

        data, addr = self.sock.recvfrom(25) # buffer size
        self.log("ph803 bytes read = %s" % data.hex(':'))
        self.close_sock()

Any feedback would be appreciated.

Thank you.

Couple of things…

You’re using self.sock within a method but self.sock is not defined within the initialize() method.

What you’re intending to do is going to lead to thread starvation unless you spin up the socket into its own thread or use an asynchronous socket handler. Callbacks are intended to process data very quickly then release the thread.

Alternatively, you can use a separate python application as the socket listener and feed data into HA or AD through MQTT when the desired data is received over the socket.

Hi, Proggie,

Thanks a lot for your reply. Mi intention is to open the socket in the initialization segment. I also understand that I should create an independent thread to listen to packets and then have the values available. The point is that the packets are never being read despite of the traffic (I checked with a python application outside HA.

Is there any thing like a firewall within HA that could be blocking the UDP traffic?

Thank you.

If you’re running AD as an HA add-on (runs in docker) or independently within a docker container then the docker container needs to allow access on the host from that port.

@proggie I have some problem, trying to run modbus server in AppDaemon. I figure this is issue with Port setting. But :slight_smile: I can not find proper config/dockerfile to change it do you know where is it located ?

I see you created a new thread for this as you should have, thank you.