Home Assistant Community Add-on: AppDaemon 4

I followed the installation steps methodically, nowhere does it say to create this. Should this not have been created during install? Is it a symptom of a bigger problem?
I created the blank appdaemon.yaml file.
Now I get this

Traceback (most recent call last):
  File "/usr/bin/appdaemon", line 10, in <module>
    sys.exit(main())
  File "/usr/lib/python3.8/site-packages/appdaemon/__main__.py", line 384, in main
    admain.main()
  File "/usr/lib/python3.8/site-packages/appdaemon/__main__.py", line 216, in main
    if "secrets" in config:
TypeError: argument of type 'NoneType' is not iterable`Preformatted text`
type or paste code here

Ok, I also populated the file and it’s now running.

according to the addon readme there should have been a sample file created, so i guess something went wrong with your install.

Hi all,
In AppDaemon, is there a way to use /dev/ttyUSB0 which is a serial custom USB dongle with an FTDI chip. It works well, with a terminal on Windows, Linux, …

I’m using a RPi3B+, on HassOS 3.10 / Home Assistant 0.105.4

This device is visible in hassio supervisor:

serial:
   /dev/serial/by-id/usb-FTDI_FT230X_Basic_UART_D34L7RNO-if00-port0
   /dev/ttyUSB0
   /dev/ttyAMA0

In an SSH console, I can see frames by typing:

cat /dev/ttyUSB0

But if I write a python script within AppDaemon, /dev/ttyUSB0 is not visible:

serial.serialutil.SerialException: [Errno 2] could not open port /dev/ttyUSB0:
[Errno 2] No such file or directory: '/dev/ttyUSB0'

I’m a total newbie, but it seems that AppDaemon runs in a virtual space separated from real hardware.
So, is there a way to reach the hardware in AppDaemon?

Thank you.

[Edit]
My config settings:

system_packages: []
python_packages:
  - pyserial
init_commands: []

My python code:

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

#
# RS_TIC App
#
# Args:
#

class RS_TIC(hass.Hass):

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

    ser = serial.Serial(
        port = '/dev/ttyUSB0',
        baudrate = 9600,                # 9600 bauds
        bytesize = serial.SEVENBITS,    # 7bits
        parity = serial.PARITY_EVEN,    # parité paire
        stopbits = serial.STOPBITS_ONE, # un bit de stop
        xonxoff = False,                # pas de contrôle de flux
        timeout = 1
        )

[/Edit]

hi @olivierov
even if it is possible ( i dont know if the addon has access to all hardware)
then you would at least need some logging that would show your output.
at the moment all that your app does is log “RS_TIC” and then it connects the var ser to serial.Serial
does that give any errors in your log?

the least you should do is try to read from the serial, and close the serial when the app terminates.
to keep reading from the serial you would need to open a subthread that has a while loop.

i am afraid this is al way to complicated to start learning to work with appdaemon at all.
i advise you to start looking if there is a topic on the forum about what you try to do. maybe someone already did write an app for it.
if not i advise you to first start learning to work with appdaemon at all. read some tutorials, look at examples, and when you know your way around in appdaemon then start thinking about something like this.

1 Like

Hi @ReneTode,
Thank you for your answer.
I’m sorry, I forgot to mention that my python code is only the beginning of my program and of course the goal of it is to read the serial port incoming data. But as my program fails on the serial.Serial(), I didn’t want to confuse anyone with all my code.
This code does, for now, just open the serial port, read incoming data and close it:

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

#
# RS_TIC App
#
# Args:
#

class RS_TIC(hass.Hass):

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

    ser = serial.Serial(
        port = '/dev/ttyUSB0',
        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
        )
    
    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()

In the same meaning, I didn’t put all the log output, but here it is:

[s6-init] making user provided files available at /var/run/s6/etc...exited 0.
[s6-init] ensuring user provided files have correct perms...exited 0.
[fix-attrs.d] applying ownership & permissions fixes...
[fix-attrs.d] done.
[cont-init.d] executing container initialization scripts...
[cont-init.d] 00-banner.sh: executing... 
-----------------------------------------------------------
 Hass.io Add-on: AppDaemon 4
 Python Apps and Dashboard using AppDaemon 4.x for Home Assistant
-----------------------------------------------------------
 Add-on version: 0.1.2
 You are running the latest version of this add-on.
 System: HassOS 3.10  (armv7 / raspberrypi3)
 Home Assistant version: 0.105.4
 Supervisor version: 200
-----------------------------------------------------------
 Please, share the above information when looking for help
 or support in, e.g., GitHub, forums or the Discord chat.
-----------------------------------------------------------
[cont-init.d] 00-banner.sh: exited 0.
[cont-init.d] 01-log-level.sh: executing... 
[cont-init.d] 01-log-level.sh: exited 0.
[cont-init.d] appdaemon.sh: executing... 
Looking in links: https://wheels.hass.io/alpine-3.11/armv7/
Collecting pyserial
  Downloading https://files.pythonhosted.org/packages/0d/e4/2a744dd9e3be04a0c0907414e2a01a7c88bb3915cbe3c8cc06e209f59c30/pyserial-3.4-py2.py3-none-any.whl (193kB)
Installing collected packages: pyserial
Successfully installed pyserial-3.4
WARNING: You are using pip version 19.2.3, however version 20.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
[cont-init.d] appdaemon.sh: exited 0.
[cont-init.d] done.
[services.d] starting services
[services.d] done.
[16:18:08] INFO: Starting AppDaemon...
2020-02-16 16:18:12.286131 INFO AppDaemon: AppDaemon Version 4.0.1 starting
2020-02-16 16:18:12.287026 INFO AppDaemon: Python version is 3.8.1
2020-02-16 16:18:12.289747 INFO AppDaemon: Configuration read from: /config/appdaemon/appdaemon.yaml
2020-02-16 16:18:12.291129 INFO AppDaemon: Added log: AppDaemon
2020-02-16 16:18:12.292627 INFO AppDaemon: Added log: Error
2020-02-16 16:18:12.294069 INFO AppDaemon: Added log: Access
2020-02-16 16:18:12.295537 INFO AppDaemon: Added log: Diag
2020-02-16 16:18:12.364495 INFO AppDaemon: Loading Plugin HASS using class HassPlugin from module hassplugin
2020-02-16 16:18:12.468449 INFO HASS: HASS Plugin Initializing
2020-02-16 16:18:12.470376 INFO HASS: HASS Plugin initialization complete
2020-02-16 16:18:12.472845 INFO AppDaemon: Initializing HTTP
2020-02-16 16:18:12.475494 INFO AppDaemon: Using 'ws' for event stream
2020-02-16 16:18:12.522756 INFO AppDaemon: Starting API
2020-02-16 16:18:12.543982 INFO AppDaemon: Starting Admin Interface
2020-02-16 16:18:12.546547 INFO AppDaemon: Starting Dashboards
2020-02-16 16:18:12.589545 INFO HASS: Connected to Home Assistant 0.105.4
2020-02-16 16:18:12.625428 INFO AppDaemon: App 'hello_world' added
2020-02-16 16:18:12.627598 INFO AppDaemon: App 'RS_TIC' added
2020-02-16 16:18:12.631056 INFO AppDaemon: Found 2 total apps
2020-02-16 16:18:12.633555 INFO AppDaemon: Starting Apps with 2 workers and 2 pins
2020-02-16 16:18:12.638662 INFO AppDaemon: Running on port 5050
2020-02-16 16:18:12.722428 INFO HASS: Evaluating startup conditions
2020-02-16 16:18:12.753672 INFO AppDaemon: Got initial state from namespace default
2020-02-16 16:18:14.655105 INFO AppDaemon: Scheduler running in realtime
2020-02-16 16:18:14.669415 INFO AppDaemon: Adding /config/appdaemon/apps to module import path
2020-02-16 16:18:14.683809 INFO AppDaemon: Loading App Module: /config/appdaemon/apps/RS_TIC.py
2020-02-16 16:18:14.717736 INFO AppDaemon: Loading App Module: /config/appdaemon/apps/hello.py
2020-02-16 16:18:14.723074 INFO AppDaemon: Initializing app hello_world using class HelloWorld from module hello
2020-02-16 16:18:14.727920 INFO AppDaemon: Initializing app RS_TIC using class RS_TIC from module RS_TIC
2020-02-16 16:18:15.073192 INFO hello_world: Hello from AppDaemon
2020-02-16 16:18:15.078646 INFO hello_world: You are now ready to run Apps!
2020-02-16 16:18:15.090383 INFO RS_TIC: RS_TIC
2020-02-16 16:18:15.094659 WARNING RS_TIC: ------------------------------------------------------------
2020-02-16 16:18:15.095943 WARNING RS_TIC: Unexpected error running initialize() for RS_TIC
2020-02-16 16:18:15.097078 WARNING RS_TIC: ------------------------------------------------------------
2020-02-16 16:18:15.114836 WARNING RS_TIC: Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/serial/serialposix.py", line 265, in open
    self.fd = os.open(self.portstr, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
FileNotFoundError: [Errno 2] No such file or directory: '/dev/ttyUSB0'
During handling of the above exception, another exception occurred:
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/RS_TIC.py", line 15, in initialize
    ser = serial.Serial(
  File "/usr/lib/python3.8/site-packages/serial/serialutil.py", line 240, in __init__
    self.open()
  File "/usr/lib/python3.8/site-packages/serial/serialposix.py", line 268, in open
    raise SerialException(msg.errno, "could not open port {}: {}".format(self._port, msg))
serial.serialutil.SerialException: [Errno 2] could not open port /dev/ttyUSB0: [Errno 2] No such file or directory: '/dev/ttyUSB0'
2020-02-16 16:18:15.116039 WARNING RS_TIC: ------------------------------------------------------------
2020-02-16 16:18:15.120777 INFO AppDaemon: App initialization complete
2020-02-16 16:18:22.607342 INFO AppDaemon: New client Admin Browser connected

The log shows that ‘/dev/ttyUSB0’ doesn’t exist in the AppDaemon context while, either with an SSH session, either by examining ‘dmesg’ output or with the supervisor, ‘/dev/ttyUSB0’ really exists.
As I said, in an SSH session, I’m able to see the incoming data with a ‘cat /dev/ttyUSB0’ command. Of course, as the data bytes format is a little bit special, it’s human unreadable, but I can see a continuous flow of garbage data.

There is probably a way to ‘mount’ the serial port in the AppDaemon context… I spent a lot of time searching in the community forum or google but without success.

Finally, @ReneTode, I thank you for your advices for my 1st steps in writing programs, but I omit to mention that ‘I’m a total newbie… in AppDaemon and HA / HassIO’, not in programming which is my full time job since… 1986. That was funny and makes my day :wink:
Please don’t be upset, it’s my fault and I very appreciate that people like you takes time to answer to lost people and that you take care about the words you use with guys who want to begin programming in a such complex architecture like HA. That’s nice and not very common in some forums. I thank you very much for that :wave:.

im not upset at all.

i made assumptions based on the info you gave. off course when the info is little i make wrong assumptions more often. :wink:

I very appreciate that people like you takes time to answer to lost people and that you take care about the words you use with guys who want to begin programming in a such complex architecture like HA. That’s nice and not very common in some forums. I thank you very much for that :wave:.

i deal with beginning programmers almost every day, her or on our appdaemon discord server.
i know that when i start out to complicated then they get lost very quick. thx for noticing and appreciating what ido.

now back on the matter at hand:

the log shows that everything is working as expected and you cant connect to the port from AD.
and you made sure that the port is actually there.

i know that hassio uses docker, and i know you need to make the outside world known to a docker.
so there must be something changed in the docker settings to make the port available to AD.
I dont use hassio myself, so i have no clue to which possibilities you got to change the docker settings, except that you got a requirements.txt
and even then i know little, because i dont use docker either.

but i am sure that @frenck knows if it is possible and if so, how it is possible.
and if it isnt possible he is also the guy to make it possible.

im sorry that i cant help you more at this moment. if you got other questions about AD at some point (and you probably will because your a programmer, and AD works just a little different then just normal python, if you want to use it optimal) then you can always find me on our discord server.

i hope you get the answer soon.

1 Like

The serial ports aren’t exposed to the add-on at this time. I will add them to the next release of this add-on. :+1:

2 Likes

Thank you @frenck, nice to read that the serial ports will accessible in next release.

thx for the answer @frenck.

just a thought for the hassio dev: is it possible to make it that the serial ports are exposed to addons by default?

No, that is bad practice, this is a containerized environment.

Only requested / needed resources should be assigned. This improves general security.

somehow i got a feeling you would say that, allthough i think that there is no risk at all by opening serial ports.
what security risk would there be? that some unwanted person plugs an USB stick in the device running hassio and starts trying to get access? i guess when he is able to get to the device he already has access :wink:

Is it something that could be part of the configured options so always available to be enabled? Would that satisfy both sides?

1 Like

i have this issue:

20-02-17 23:12:21 ERROR (SyncWorker_10) [hassio.docker] Can’t start addon_a0d7b954_appdaemon: 500 Server Error: Internal Server Error (“driver failed programming external connectivity on endpoint addon_a0d7b954_appdaemon (2e5a8484e34628bf8c914557ab3e7bbc0bc09c4ba59c86090bbf9929489443f8): Error starting userland proxy: listen tcp 0.0.0.0:5050: bind: address already in use”)

and won’t start :frowning:

if i stop traccar, appdaemon start working.

so you have 2 addons that use the same port.
change 1 of the 2

on default AD uses port 5050

yes, and traccar use different port 8072. so why this problem?

there are 2 options:

  1. AD is running double somehow (ad3 and ad4 installed together for example, or a shutdown went wrong)

  2. you got another program blocking the port.

  3. would be helped with a restart in a lot of cases unless you got a double installation that both starts at restart.
    but 2 is the most logical conclusion, because when you close the other program AD starts to work.
    so closing traccar opens up the 5050 port.

so you need to find out why traccar blocks port 5050

if i start appdaemon before traccar, starting traccar i see:
2020-02-18 10:44:28 WARN: Port 5050 is disabled due to conflict

so, how i can change appdaemon port or disable this port in traccar?

https://appdaemon.readthedocs.io/en/latest/CONFIGURE.html#configuring-the-http-component

just change that port to another free port.

i dont know what traccar is and why it uses that port.

probably use this port for one of the compatible tracker, for example my tracker use the port: 5013