Appdaemon + Python import - not working

Hi everyone! I’m quite new to home assistant and I’m struggeling with getting further into appdaemon.
My current system is: HA and Appdaemon both the lastest version, under seperate users in virtual environments on a rasperryPi 3B+. Python version is 3.7

The basic appdaemon apps are working so far - the hello.py, also some interaction with HA.
I am now trying to run a python script with imports when an event is triggered inside HA. The script is supposed to show a rainbow on the MATRIX Creator module mounted on the Pi. When running by itself it works. So far I’ve tried two ways.

  1. I copied the code into the working example of reacting to an event
from matrix_lite import led
from time import sleep
from math import pi, sin
import appdaemon.plugins.hass.hassapi as hass

class Rainbow(hass.Hass):
        def initialize(self):
                event="rhasspy_Party"
                self.listen_event(self.fewSecEverloop, event)

        def fewSecEverloop(self, event_id, payload_event, *args):
                everloop = ['black'] * led.length
                ledAdjust = 0.51 # MATRIX Creator
                frequency = 0.375
                counter = 0.0
                tick = len(everloop) - 1
                while True:
                        # Create rainbow
.......

this gives me the error
No module named 'matrix_lite'
However, when i run the .py (without hass part) by itself there is no such issue. I’ve already tried adding the module to the requirements.txt, which did’nt make a differnce.

  1. My second attempt was calling the python script as a subsystem:
import os
import appdaemon.plugins.hass.hassapi as hass
import subprocess

class Rainbow(hass.Hass):
        def initialize(self):
                event="rhasspy_Party"
                self.listen_event(self.fewSecEverloop, event)

        def fewSecEverloop(self, event_id, payload_event, *args):
                #execfile('rainbow.py')
                subprocess.call(['python3', 'rainbow.py'])
                #os.system("python3 rainbow.py")

This does nothing, when the event is triggered, not even gives an error.

I’ve added the everloopapp.py to the apps.yaml like this:

party:
  module: everloopapp
  class: Rainbow

Please let me know, if there is anything missing in order to figure out what might be wrong.
I’m grateful for any advice, hints, ect.

Long time that I used AppDaemon in a venv. Is there a reason why you prefer installing it in a venv instead of something else?
Could you not just install the package in the venv with pip, like this:

python3 -m pip install --user matrix-lite

I was not able to get appdaemon working through the suggested installation, and it worked so far with the venv…
I’ve already done that. the installation give me Requirement already satisfied

I suspect that you didn’t install it in the virtual environment.
Here’s a guide on how to install packages in a venv, in case you need it.

I hope that’s not the case, otherwise I’d have to relearn how to work with venvs :smiley:

pi@raspi:/home/appdaemon/.appdaemon $ sudo su -s /bin/bash appdaemon
appdaemon@raspi:~/.appdaemon$ python3 -m pip install --user matrix_lite
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: matrix_lite in /home/appdaemon/.local/lib/python3.7/site-packages (0.0.8)
Requirement already satisfied: pybind11>=2.4 in /home/appdaemon/.local/lib/python3.7/site-packages (from matrix_lite) (2.4.3)

Hmm strange. I don’t like to tag people but maybe @eifinger may help out here.

@gloria so you start appdaemon with the appdaemon user and the venv activated?

Hi, thanks for joining the topic!
I have appdaemon running as a service, starting it with the following service file:

[Unit]
Description=AppDaemon
[email protected]
[email protected]

[Service]
Type=simple
User=%i
ExecStart=/srv/appdaemon/bin/appdaemon -c “/home/%i/.appdaemon/conf/”

[Install]
WantedBy=multi-user.target

During the setup and for installing the packages i did it as user, with venv

Where does the path /srv/appdaemon/bin/appdaemon come from?

1 Like

Nice, that was the question I needed!
I did’nt properly activate the venv before, because I wasn’t in the correct destination. I works perfectly now :slightly_smiling_face:
These steps worked, when executed in the right place

pi@raspi:/srv/appdaemon $ sudo -u appdaemon -H -s
appdaemon@raspi:/srv/appdaemon$ source bin/activate
(appdaemon) appdaemon@raspi:/srv/appdaemon$ python3 -m pip install matrix_lite
2 Likes