I am requesting that a small change be made to the imap email sensor to decode base64 for the body of emails. You could have a parameter available to the user to indicate “yes” decode or “no” do not decode. I believe this change will save a lot of grief for new users to the system. Once the decoding is done, the user will be able to parse or use the body any way they would like.’
PS, the subject, email addressee and date are not encoded.
Thank You!
Jim (been fooling around with this for 3 days with no success) Pridemore
I think I have found a solution by using appdaemon apps.
Hi, I’m having the same problem. It would be great if you could give me more details on how you solved this with appdeamon. I know it’s an old thread but more information on this will be much appreciated.
Here is the snippet of code…
mystate = self.get_state("sensor.backyard_motion")
email_body = self.entities.sensor.backyard_motion.attributes.body
email_date = self.entities.sensor.backyard_motion.attributes.date
#self.notify(email_body)
#self.log('email body '+email_body)
#self.log(email_date)
mybody = base64.b64decode(email_body)
#self.log('mybody :'+str(mybody))
x = 'Alarm: Motion Detect' + 'Alarm input channel No.: 3' + 'Alarm device name: LNR6108'
x = str(mybody)
y = x.find(str("No.: "))
#self.log('found string: '+str(y))
#self.log(x[y+5])
#detected_by_camera = x[y+5]
#self.log("camera "+detected_by_camera)
mylist = str(mybody).split(':')
myalarm = mylist[2]
detected_by_camera = myalarm[1]
Here is the entry in my configuration.yaml that sets up the “sensor.backyard.motion”
sensor:
- platform: version
- platform: imap_email_content
name: 'Backyard Motion'
server: imap.gmail.com
port: 993
username: [email protected]
password: !secret gmail_password
senders:
- [email protected]
As I recall, I had to do a bit of experimentation to find all the info and configure the coding correctly, hence, you can see all my #self.log entries.
Good luck!
Here is all of the code…I have 3 cameras and I handle each of them a little differently…
import appdaemon.plugins.hass.hassapi as hass
import base64
import datetime
import os, time
class motion_detect(hass.Hass):
def initialize(self):
self.camera_1_handle = None
self.camera_2_handle = None
self.camera_3_handle = None
detected_by_camera = ""
self.log("initalization camera_motion_detect")
#self.notify("initalization camera_motion_detect")
mystate = self.get_state("sensor.backyard_motion")
self.listen_state(self.motion_detected,"sensor.backyard_motion", new="Alert")
def motion_detected(self, entity, attribute, old, new, kwargs):
mystate = self.get_state("sensor.backyard_motion")
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
time_now = time.strftime('%X %x %Z')
self.log ("motion detected ")
#self.log (self.get_state("sensor.backyard_motion"))
#if self.sun_up():
if self.sun_down():
mystate = self.get_state("sensor.backyard_motion")
email_body = self.entities.sensor.backyard_motion.attributes.body
email_date = self.entities.sensor.backyard_motion.attributes.date
#self.notify(email_body)
#self.log('email body '+email_body)
#self.log(email_date)
mybody = base64.b64decode(email_body)
#self.log('mybody :'+str(mybody))
x = 'Alarm: Motion Detect' + 'Alarm input channel No.: 3' + 'Alarm device name: LNR6108'
x = str(mybody)
y = x.find(str("No.: "))
#self.log('found string: '+str(y))
#self.log(x[y+5])
#detected_by_camera = x[y+5]
#self.log("camera "+detected_by_camera)
mylist = str(mybody).split(':')
myalarm = mylist[2]
detected_by_camera = myalarm[1]
#self.notify("detected by camera "+detected_by_camera+" "+time_now)
#self.log("detected by camera "+detected_by_camera)
#backyard porch
# if detected_by_camera == "3":
# self.set_state("sensor.backyard_motion", new = "off")
# self.turn_on("light.linear_lb60z_1_dimmable_led_light_bulb_level_12", brightness = "120")
# self.cancel_timer(self.camera_3_handle)
# self.camera_3_handle = self.run_in(self.timer_handler, 300, myentity = "light.linear_lb60z_1_dimmable_led_light_bulb_level_12")
# self.log(self.get_state("light.linear_lb60z_1_dimmable_led_light_bulb_level_12", attribute='friendly_name') + ", turned on due to motion - line 37 "+time_now)
# front gate light
if detected_by_camera == "2":
if self.now_is_between("21:00:01", "sunrise"):
self.set_state("sensor.backyard_motion", new = "off")
self.turn_on("light.linear_lb60z_1_dimmable_led_light_bulb_level_17", brightness = "120")
self.cancel_timer(self.camera_2_handle)
self.camera_2_handle = self.run_in(self.timer_handler, 300, myentity = "light.linear_lb60z_1_dimmable_led_light_bulb_level_17")
self.log(self.get_state("light.linear_lb60z_1_dimmable_led_light_bulb_level_17", attribute='friendly_name') + ", turned on due to motion - line 46 "+time_now)
# middle garage
elif detected_by_camera == "1":
self.set_state("sensor.backyard_motion", new = "off")
self.turn_on("light.linear_lb60z_1_dimmable_led_light_bulb_level_15", brightness = "120")
self.cancel_timer(self.camera_1_handle)
self.camera_1_handle = self.run_in(self.timer_handler, 300, myentity = "light.linear_lb60z_1_dimmable_led_light_bulb_level_15")
self.log(self.get_state("light.linear_lb60z_1_dimmable_led_light_bulb_level_15", attribute='friendly_name') + ", turned on due to motion - line 53 "+time_now)
# else: self.log("Old email dated "+email_date+" ignored")
self.set_state("sensor.backyard_motion", new = "off")
#self.notify("Motion detected from camera " + detected_by_camera)
detected_by_camera = ''
# else: self.log("Sun is not down")
def timer_handler(self, kwargs):
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
self.turn_off(kwargs['myentity'])
self.log(self.get_state(kwargs['myentity'], attribute='friendly_name') + ", is off ")
#self.notify(self.get_state(kwargs['myentity'], attribute='friendly_name') + ", is off " )
1 Like
Wow, thank you so much for your lightning fast reply. That will definitely get me started!