Appdaemon first steps

Took some time to configure appdaemon. I believe completed all the steps corectly and then tried to add the program from here

https://home-assistant.io/cookbook/automation_kodi_dynamic_input_select/

I believe i’m still messing up what should go to the apps section in appdaemon.yaml
I put it like this (as i put into the code into kodi.py). Do i need - in fron of program name?

# Apps
hello_world:
  module: hello
  class: HelloWorld

kodi select:
  module: kodi
  class: DynamicKodiInputSelect

But get the following error

2017-08-18 17:26:12.074126 WARNING ------------------------------------------------------------
2017-08-18 17:27:54.045849 WARNING ------------------------------------------------------------
2017-08-18 17:27:54.046409 WARNING Unexpected error during loading of kodi.py:
2017-08-18 17:27:54.046731 WARNING ------------------------------------------------------------
2017-08-18 17:27:54.048340 WARNING Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/appdaemon/appdaemon.py", line 911, in read_app
    conf.modules[module_name] = importlib.import_module(module_name)
  File "/usr/lib/python3.4/importlib/__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/home/homeassistant/.homeassistant/appdaemon/apps/kodi.py", line 2, in <module>
    from homeassistant.components.media_player.kodi import (
ImportError: No module named 'homeassistant'

Cool - I didn’t know AppDaemon Apps had made it into the docs :slight_smile:

It looks like your config is fine.

I’m not the author of that App but it looks like it is making a couple of assumptions. It;s making a call into the homeassistant libraries which means:

  1. You need to be running AppDaemon on the same machine as AppDaemon
  2. You need to be running in the same virtual environment as Home Assistant.

If that isn’t the case the home assistant modules won;t be found and you will get this error.

1 Like

I believe you naile it with 2nd one.
I’m using the all-in one isntaller. So i installed appdaemon outside the VE and then activated it in VE running pip3 install appdaemon in both cases…
But i’m launching it outside the VE - so back to reading the docs

1 Like

Well like i said you nailed it.

Running

#sudo su -s /bin/bash homeassistant
#source /srv/homeassistant/homeassistant_venv/bin/activate
#appdaemon -c /home/homeassistant/.homeassistant/appdaemon

Got everything in order.

For the noobs like myself below is a corrected systemd script for launching appdaemon in VE

[Unit]
Description=AppDaemon
[email protected]

[Service]
Type=simple
User=homeassistant
ExecStart=/srv/homeassistant/homeassistant_venv/bin/appdaemon -c "/home/homeassistant/.homeassistant/appdaemon"

[Install]
WantedBy=multi-user.target
3 Likes

I’m getting what I think is a similar error. I reinstalled appdaemon in the same venv that I am running home assistant in, but am getting an error that:

  File "/home/homeassistant/.homeassistant/appdaemon/apps/flasher.py", line 1, in <module>
	import homeassistant.appapi as appapi
ImportError: No module named 'homeassistant'

Here’s the full output when I try to run appdaemon from the command line:

(homeassistant_venv) homeassistant@raspberrypi:~ $ appdaemon -c /home/homeassistant/.homeassistant/appdaemon
/usr/local/lib/python3.4/dist-packages/requests/__init__.py:80: RequestsDependencyWarning: urllib3 (1.22) or chardet (2.3.0) doesn't match a supported version!
  RequestsDependencyWarning)
2017-11-21 20:12:40.816165 INFO AppDaemon Version 2.1.12 starting
2017-11-21 20:12:40.816682 INFO Configuration read from: /home/homeassistant/.homeassistant/appdaemon/appdaemon.yaml
2017-11-21 20:12:41.209024 INFO Starting Apps
2017-11-21 20:12:41.460185 INFO Got initial state
2017-11-21 20:12:41.461709 INFO Loading Module: /home/homeassistant/.homeassistant/appdaemon/apps/hello.py
2017-11-21 20:12:41.466013 INFO Loading Object hello_world using class HelloWorld from module hello
2017-11-21 20:12:41.609494 INFO hello_world: Hello from AppDaemon
2017-11-21 20:12:41.612255 INFO hello_world: You are now ready to run Apps!
2017-11-21 20:12:41.612692 INFO Loading Module: /home/homeassistant/.homeassistant/appdaemon/apps/flasher.py
2017-11-21 20:12:41.615963 WARNING ------------------------------------------------------------
2017-11-21 20:12:41.616429 WARNING Unexpected error during loading of flasher.py:
2017-11-21 20:12:41.616740 WARNING ------------------------------------------------------------
2017-11-21 20:12:41.620814 WARNING Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/appdaemon/appdaemon.py", line 901, in read_app
	conf.modules[module_name] = importlib.import_module(module_name)
  File "/usr/lib/python3.4/importlib/__init__.py", line 109, in import_module
	return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/home/homeassistant/.homeassistant/appdaemon/apps/flasher.py", line 1, in <module>
	import homeassistant.appapi as appapi
ImportError: No module named 'homeassistant'

2017-11-21 20:12:41.621304 WARNING ------------------------------------------------------------
2017-11-21 20:12:41.621698 INFO App initialization complete
2017-11-21 20:12:41.622364 INFO Dashboards are disabled
2017-11-21 20:12:41.622652 INFO API is disabled
2017-11-21 20:12:41.756016 INFO Connected to Home Assistant 0.56.2

Here’s the full content of flasher.py

import homeassistant.appapi as appapi

class MotionLights(appapi.AppDaemon):

  def initialize(self):
	self.listen_state(self.motion, "input_boolean.test_boolean", new = "on")

  def motion(self, entity, attribute, old, new, kwargs):
	if self.self.sun_down():
	  self.turn_on("light.map_level")
	  self.run_in(self.light_off, 60)
	  self.flashcount = 0
	  self.run_in(self.flash_warning, 1)

  def light_off(self, kwargs):
	self.turn_off("light.map_level")

  def flash_warning(self, kwargs):
	self.toggle("light.table_lamp_level")
	self.flashcount += 1
	if self.flashcount < 10:
	  self.run_in(self.flash_warning, 1)

Any idea what gives? Thank you very much!

your first line should be:

import appdaemon.appapi as appapi

there is no module homeassistant like the error says.

1 Like

That was it, thank you! I’ve now successfully ran the app. :grinning:

Now on to my next challenge of autostart… Baby steps!

2 Likes

Related to this, I’ve created pull requests to update the documentation lest anyone else fall into this trap!

2 Likes

Thanks - but this will be changed again in AppDaemon 3.0! The first typo was from a very early version of the docs before 1.0 was released that somehow escaped. It has been as above now for a long time, but will be changing in 3.0 - I’ll document it when the time comes. Wanted to add this comment for posterity if future people read this thread after 3.0.

1 Like

Thank you very much for this! I can run Appdaemon from the command line in the same way you do (same venv path and everything), but I can’t get the service to run with systemd. When I try that, I get this error:

 Active: failed (Result: exit-code) since Tue 2017-12-05 20:26:00 EST; 7s ago
  Process: 1067 ExecStart=/srv/homeassistant/homeassistant_venv/bin/appdaemon -c /home/homeassistant/.homeassistant/appdaemon (code=exited, status=203/EXEC)
 Main PID: 1067 (code=exited, status=203/EXEC)

Do you have any idea what’s wrong? I’ve been banging my head for a while!

if i google on the error i find this:

The issue is fixed. After my restart things are working fine. I think I missed to add #!/bin/sh in the beginning of the script file.

could that be it?
other problem people often encounter here is that you are running your service as another uer tht as no filerights to the appdaemon directory.

I think I found a similar post, and when I tried that I got a similar error. :frowning:

Thank you for the recommendation!

pi@raspberrypi:/etc/systemd/system $ systemctl status [email protected][email protected] - AppDaemon
   Loaded: loaded (/etc/systemd/system/[email protected]; enabled)
   Active: failed (Result: exit-code) since Wed 2017-12-06 07:15:36 EST; 3s ago
  Process: 6001 ExecStart=/srv/homeassistant/homeassistant_venv/bin/appdaemon -c /home/homeassistant/.homeassistant/appdaemon (code=exited, status=203/EXEC)
 Main PID: 6001 (code=exited, status=203/EXEC)

i think its a user problem, what is in your service file?

Did you install appdaemon in the venv?

Here’s my service file:

#!/bin/sh
[Unit]
Description=AppDaemon
[email protected]
[Service]
Type=simple
User=homeassistant
ExecStart=/srv/homeassistant/homeassistant_venv/bin/appdaemon -c "/home/homeassistant/.homeassistant/appdaemon"
[Install]
WantedBy=multi-user.target

I have it after [email protected] because when I run systemctl | grep running that’s the service I see. Not sure if that implies that the user pi is running home assistant… which would perhaps be not so good.

@keithh666 I am pretty sure I installed it in the venv. I originally didn’t, but then uninstalled / re-installed (I say probably because I first got appdaemon running about two weeks ago and my memory is crap).

Thank you both so much!

It would probably be a good idea to find out where appdaemon is installed…

ls /srv/homeassistant/homeassistant_venv/bin if it isn’t there then you need to determine where it is :stuck_out_tongue:

I ran sudo find / -type d -name 'appdaemon' and the only result it found was /usr/local/lib/python3.4/dist-packages/appdaemon

Does that indicate where it is installed? Does that make any sense?

In case it helps (you help me :slight_smile: ), if I activate my venv as user homeassistant and enter appdaemon -c /home/homeassistant/.homeassistant/appdaemon , appdaemon runs fine (just not as an auto-started service of course).

I ran into similar errors when I started integrating appdaemon to my HA.

Read this thread, it may help in your case.

This suggests you didn’t install appdaemon in the virtual environment, but by using sudo pip install appdaemon.

ExecStart=/srv/homeassistant/homeassistant_venv/bin/appdaemon -c "/home/homeassistant/.homeassistant/appdaemon"

This line from your service file is trying to start the appdaemon in the virtual environment.

I suggest you keep your service file, remove the appdaemon you have installed, and install one in the virtual environment

As pi user

sudo pip uninstall appdaemon

As homeassistant user with the HA virtual environment enabled do

pip install appdaemon

And you should be fine.

Edit:
If you have run appdaemon as pi user. you may run into permission problems, but cross that bridge if we come to it.

1 Like

Boom! Installing it in the venv fixed it. I was unable to uninstall it outside of the venv, which is a little alarming… but I chose to ignore that fact and install in the venv anyways. Thank you all so much!

2 Likes