Split not working in appdaemon apps

I am trying to get info from an email body that is base64 coded. I am writing an appdaemon app. I am able to decode the email body, but when I try to split it, nothing happens (the code works in a straight python app however. Here is the code from that app:

#!/usr/bin/env python
import base64

mybody = base64.b64decode("QWxhcm06IE1vdGlvbiBEZXRlY3QgDQpBbGFybSBpbnB1dCBjaGFubmVsIE5vLjogMSAgDQpBbGFy")
print mybody 
print("going to split list")

mylist = mybody.split(":")
myalarm = mylist[2]
print(myalarm[1])
camera = myalarm[1]
#if camera  == "3":
#            turn_on.light.level_8       
print("light.level_7 on")

and here are the results…

pi@hassbian:/home/homeassistant/conf/apps $ python test.py
Alarm: Motion Detect
Alarm input channel No.: 1
Alar
going to split list
1
light.level_7 on
pi@hassbian:/home/homeassistant/conf/apps $

here is the code in the app that I am having problems with:

import appdaemon.plugins.hass.hassapi as hass
import base64


class camera_motion_detect(hass.Hass):
    def initialize(self):
        self.listen_state(self.turn_on_front_outside_lights,"sensor.backyard_motion", new="Alert") 

    def turn_on_front_outside_lights(self, entity, attribute, old, new, kwargs):
        email_body = self.entities.sensor.backyard_motion.attributes.body
        self.log(base64.b64decode(email_body))
        mybody = base64.b64decode(email_body)
        self.log("going to split list")
        mylist = mybody.split(':')
        for item in mylist :
            self.log(item)
        self.log("list is split, now printing the split list")
        self.log(mylist)
        self.log(mylist[1])
        myalarm = mylist[2]
        self.log(myalarm)
        camera = myalarm[1]
        self.log(camera)
        if camera == "3":
            self.turn_on.light.level_8       
            self.log("light.level_7 on")
        elif camera == "2":
            self.turn_on.group.outside_courtyard_lights
            self.log("outside_courtyard_lights on")
        elif camera == "1":
            self.turn_on.light.level_7
            self.log("light.level_7 on")
        else: self.log("problem in if then else")

here are the results from the appdaemon.log. It terminates during the split process, which was copied over to the app that ran the first split successfully.

2018-06-02 19:49:12.406080 INFO AppDaemon: Initializing app camera_motion_detect using class camera_motion_detect from module camera_motion_detect
2018-06-02 19:49:21.357483 INFO camera_motion_detect: b'Alarm: Motion Detect \r\nAlarm input channel No.: 1  \r\nAlar'
2018-06-02 19:49:21.363989 INFO camera_motion_detect: going to split list
2018-06-02 19:49:39.112624 INFO camera_motion_detect: b'Alarm: Motion Detect \r\nAlarm input channel No.: 3  \r\nAlarm start time(D/M/Y H:M:S): 2/6/2018 19:32:49\r\nAlarm device name: LNR6108\r\n'
2018-06-02 19:49:39.115540 INFO camera_motion_detect: going to split list
2018-06-02 19:57:55.531101 INFO camera_motion_detect: b'Alarm: Motion Detect \r\nAlarm input channel No.: 2  \r\nAlarm start time(D/M/Y H:M:S): 2/6/2018 19:58:13\r\nAlarm device name: LNR6108\r\n'
2018-06-02 19:57:55.533927 INFO camera_motion_detect: going to split list
2018-06-02 19:58:08.603146 INFO AppDaemon: Terminating camera_motion_detect

I am running under hassbian but the appdaemon instructions say to do this:

import appdaemon.plugins.hass.hassapi as hass

Is this the correct configuration for a hassbian installation?

AD is platform independant. so yeah you need to do it that way.

your trouble comes from this line

mybody = base64.b64decode(email_body)

when you log mybody after that, you see b’ before the text.
that means that it isnt a string but a byte
split is used to split strings.
so you need to turn it into a string first. i think that

mylist = str(mybody).split(':')

will do the trick.

Rene, thank you, you are awesome, that worked. Of course I had a few more mistakes down the line but worked them out! I have to give credit to you and your appdaemon tutorial for getting this far. I was able to base64 decode the email body, split the result, find the correct light to turn on, and turned it on.:sunglasses:

class camera_motion_detect(hass.Hass):
    def initialize(self):
        self.listen_state(self.turn_on_front_outside_lights,"sensor.backyard_motion", new="Alert") 
    def turn_on_front_outside_lights(self, entity, attribute, old, new, kwargs):
        email_body = self.entities.sensor.backyard_motion.attributes.body
        self.log(base64.b64decode(email_body))
        mybody = base64.b64decode(email_body)
        self.log("going to split list")
        mylist = str(mybody).split(':')
        self.log("list is split, now printing the split list")
        self.log(mylist)
        self.log(mylist[1])
        myalarm = mylist[2]
        self.log(myalarm)
        camera = myalarm[1]
        self.log(camera)
        if camera == "3":
            self.turn_on("light.level_8", brightness = "120")       
            self.log("light.level_8 on")
        elif camera == "2":
            self.turn_on("light.level_9", brightness = "120")
            self.log("light.level_9 on")
        elif camera == "1":
            self.turn_on("light.level_7", brightness = "120")
            self.log("light.level_7 on")
        else: self.log("problem in if then else")
2018-06-03 09:47:19.540629 INFO camera_motion_detect:  Motion Detect \r\nAlarm input channel No.
2018-06-03 09:47:19.543207 INFO camera_motion_detect:  3  \r\nAlarm start time(D/M/Y H
2018-06-03 09:47:19.545731 INFO camera_motion_detect: 3
2018-06-03 09:47:19.604421 INFO camera_motion_detect: light.level_8 on
1 Like

good to hear.

i love to see heavy use of logging, for sure when you are starting to build things.
it helps a lot to keep on track from what is happening.

2 small tips:

if you use it like this:

    # these lines just 1 time in the init
    self.debug = False    
    if "debug" in self.args:
        self.debug = self.args["debug"]

     # the next lines everywhere instead of just self.log
    if self.debug:
        self.log("anything you want to log","DEBUG")

you can shut down or let the logging reappear by setting debug: True in the yaml

or just remove them after you are satisfied with the app, because it will get annoying when you log to much unimportant details.

keep on going!

2 Likes

Do you mean

test_motion_sensor:
  module: test
  class: testing
  debug: true

yeah but i think it needs a capital t, so True

Thanks, I appreciate it!

1 Like