I have a template that is able to look at the email body. Each time I have motion in my security camera system I send myself an email to an email account that was set up solely to receive these emails. I have an imap email content sensor. I use a template to parse the body for the camera number where motion occurred. Unfortunately, the body is encoded in Base64. I want to use python to decode this information. However, I see that templates cannot import the Base64 library nor can a Python Script. I have been able to get the decode to work by copying the body data from my sensor and pasting into a python program I wrote (that imports Base64). I cannot determine a way I can access that program from a template or a Home assistant python script. I have a background in javascript and jquery so I know my way around that language. I know very little python and am learning that Homeassistant has a lot of power built in but it takes forever to determine the correct approach. I have seen several topics on this subject but none that I have seen helps me resolve this issue.
Here is my template:
- id: test parse email body
alias: test parse email body using template
trigger:
entity_id: sensor.backyard_motion
platform: state
to: Alert
# condition:
# condition: sun
# after: sunset
action:
service: light.turn_on
data_template:
entity_id: >
{% if is_state("sensor.backyard_motion", "Alert")%}
{% set test = states.sensor.backyard_motion.attributes.body %}
{% set mylist = test.split(":") %}
{% set myalarm = mylist[2] %}
{% if myalarm[1] == "3" %}
light.level_8
{% elif myalarm[1] == "2" %}
group.outside_courtyard_lights
{% elif myalarm[1] == "1" %}
light.level_7
{% endif %}
{% endif %}
brightness: 120
Here is my python code to decode the body:
#!/usr/bin/python
import base64
decodedstr = base64.b64decode(email_body)
# I cut and paste the email body from the imap sensor into "email_body"
# I don't know how to access the email body
Here is the info in the email body I need, all I need is the Alarm Input channel No:
Alarm: Motion Detect
Alarm input channel No.: 1
Alarm start time(D/M/Y H:M:S): 1/6/2018 17:44:44
Alarm device name: LNR6108
After 3 days, I am brain dead and cannot determine how to get the template and python code to work together in HA.
I have worked several days on this and cannot find a solution. Can anyone help me with this? Thanks in advance.