AppDaemon Serial Setup - AttributeError: module 'serial' has no attribute 'Serial'

Tags: #<Tag:0x00007f73b4e633e8>

Has anyoe had luck with AppDaemon and serial?

Attempting to set up a serial communication between a RPI4 and Arduino Mega via AppDaemon. Running it in the RPI terminal it is working but trying to implement it through Appdaemon is throwing errors.

to strip it back to the basics I’m using code I found in the following thread:

I’ve included pyserial to the AddDaemon Configuration

my code is:

import appdaemon.plugins.hass.hassapi as hass
import serial

class SerialCall(hass.Hass):



  def initialize(self):
    self.log("openSerial")

    ser = serial.Serial(
        port = '/dev/ttyACM0',
        baudrate = 9600,                # 9600 bauds
        bytesize = serial.SEVENBITS,    # 7bits
        parity = serial.PARITY_EVEN,    # even parity
        stopbits = serial.STOPBITS_ONE, # 1 stop bit
        xonxoff = False,                # no flow control
        timeout = 1
        )
    self.log("yep")
    for i in range(10):
        # read a data line
        x = ser.readline()
        # clean the string
        x = x.decode('utf-8').rstrip()
        # split the fields
        c=x.split(' ')
        # log (for the moment) one of the data
        if c[0] == "STINTS":
            self.log("%s: %s", c[0], c[1])

    ser.close()

According to the log

2020-08-09 14:07:31.642869 INFO AppDaemon: Initializing app SerialSendRecieve using class SerialCall from module serial
2020-08-09 14:07:31.655541 INFO SerialSendRecieve: openSerial
2020-08-09 14:07:31.658861 WARNING SerialSendRecieve: ------------------------------------------------------------
2020-08-09 14:07:31.660339 WARNING SerialSendRecieve: Unexpected error running initialize() for SerialSendRecieve
2020-08-09 14:07:31.661456 WARNING SerialSendRecieve: ------------------------------------------------------------
2020-08-09 14:07:31.663408 WARNING SerialSendRecieve: Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/appdaemon/app_management.py", line 145, in initialize_app
    await utils.run_in_executor(self, init)
  File "/usr/lib/python3.8/site-packages/appdaemon/utils.py", line 276, in run_in_executor
    response = future.result()
  File "/usr/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/appdaemon/apps/serial.py", line 11, in initialize
    ser = serial.Serial(
AttributeError: module 'serial' has no attribute 'Serial'

2020-08-09 14:07:31.664537 WARNING SerialSendRecieve: ------------------------------------------------------------

the initial logs indicate pyserial has been downloaded/installed with no issue. so at a loss at this stage.

to complicate things further also having issue when I try to include the JSON library from python, through import json it commands are not being recognised.

any help or insight would be greatly appreciated.

for those playing at home:
issue was pointed out by renetode, i had a naming error with the file and once changing had to restart.

@ReneTode thank you for following that up, I just gave it another crack and its kicked off with no issues, I think you may have hit the nail on the head with the naming issues. I appropriately name it:

HopesAndDreams:
    module: whyIsSerialDumb
    class: pleaseWork

with the follwoing code:

import appdaemon.plugins.hass.hassapi as hass
import serial
import json

class pleaseWork(hass.Hass):



  def initialize(self):
    self.log("openSerial")
    ser = serial.Serial(
        port = '/dev/ttyACM0',
        baudrate = 9600,                # 9600 bauds
        bytesize = serial.SEVENBITS,    # 7bits
        parity = serial.PARITY_EVEN,    # even parity
        stopbits = serial.STOPBITS_ONE, # 1 stop bit
        xonxoff = False,                # no flow control
        timeout = 1
        )
    buffer = ""
    
    while True:
        try:
                buffer = ser.readline()
                self.log(buffer)
                doc = json.loads(buffer)
    
                read_b1 = doc["b1"]
                read_b2 = doc["b2"]
    
                read_s1 = doc["s1"]
                read_s2 = doc["s2"]
                
                self.log(read_b1)
                buffer = ""
        except json.JSONDecodeError:
                self.log("Error : try to parse an incomplete message")

and everything is working just fine. spent a solid three days on that!