Imap_email_content help required

Hi mplawner,

thanks a lot for looking back in you script, appreciate it!
I tried as you suggested - using “parseemail.sh TEST-BACKUP FAIL” manually works find and the sensor switches to “Failed”.
So - the automation rule it is…

I looked into the mails, “TEST-Backup FAIL” for example is placed in the mail body…but the trigger won’t act. :confounded:

So the issue seems to be the missing value from the automation to the script - i understood.

Tried {{ states(‘sensor.emails’) }} too, sadly with the same result.

- alias: Email State Change - Parse
  trigger:
    - platform: state
      entity_id: sensor.emails
  action:
    - service: shell_command.parseemail
      data_template:
        #value: "{{ state('sensor.emails') }}"
        value: "{{ trigger.to_state.state }}"

So close :sweat:

The script is case sensitive, so if you have “TEST-Backup FAIL” in the body, but TEST-BACKUP FAIL in the script, those two values won’t be equal.

Have you tried sending emails manually?

Thanks mplawner! Sorry for the delay, had to sort out some private stuff.

And guess what, with a fresh view, there is my ‘Ah Ha’ moment :wink:

Incoming mails are processed via imap_email_content, the content is shown in sensor.emails and your wonderful script parses the content according to the rules and feeds mqtt. Thanks again for sharing your work!

There seems to be a difference between “value: “{{ states(‘sensor.emails’) }}”” and “value: “{{ state(‘sensor.emails’) }}””, that did it for me.

shell_command.parseemail
parseemail: bash /home/homeassistant/.homeassistant/parseemail.sh {{value}}

parseemail.sh

#!/bin/sh

echo "****"  >> /home/homeassistant/.homeassistant/parseemail.log
echo $1 >> /home/homeassistant/.homeassistant/parseemail.log

if [ $# -ne 1 ]; then
  echo "Unknown"
else

# Backup Status Sycloud TEST 3
  mosquitto_pub -h 127.0.0.1 -u <User> -P <PW> -r -t RaspHA/emails/`echo $1 | awk '/SyCloud/ {print "Backup_TEST3"}'` -m "SyCloud"
  mosquitto_pub -h 127.0.0.1 -u <User> -P <PW> -r -t RaspHA/emails/`echo $1 | awk '/SxCloud/ {print "Backup_TEST3"}'` -m "SxCloud"

fi
################################################################

Automation - email_parse.yaml

- alias: Email State Change - Parse
  trigger:
    - platform: state
      entity_id: sensor.emails
  action:
    - service: shell_command.parseemail
      data_template:
        value: "{{ states('sensor.emails') }}"

Sensor MQTT

- platform: mqtt
  name: "MQTT Status Backup TEST3"
  state_topic: "RaspHA/emails/Backup_TEST3"

Whew! Glad you got there @Vayain and all is working for you!

I’m still uncertain why

value: "{{ states('sensor.emails') }}"

works for you and

value: "{{ trigger.to_state.state }}"

doesn’t. They should be the same.

Nonetheless, glad you’re up and running!

Thanks to you :wink:
Giving it another try with “trigger.to_state.state”, the result is the same…I’m wondering, where my first error was…

Now i recognized one last thing…seems like the parsing uses only the first word:

parseemail.log

TEST-BACKUP
****
Backup_SyCloud-2-WDextern
****
Backup_SyCloud-2-SGextern
****
TEST-BACKUP

For example the last entry should be “TEST-BACKUP FAILED”, the state of “sensor.email” is complete, seems like something got lost somewhere :sweat_smile:

Odd. My script does appear to be parsing the full message.

For example, from my log:

****
4/19 5:03 PM
 Garage Door (Zone 12) changed to Open.
 garagedooropen
****
4/19 5:04 PM
 Garage Door (Zone 12) changed to Closed.
 garagedoorclosed

Those entries were accurately captured by the following lines from the script:

  # Garage Door
  mosquitto_pub -h 127.0.0.1 -r -t home-assistant/emails/`echo $1 | awk '/garagedooropen/ {print "garagedoor"}'` -m "open"
  mosquitto_pub -h 127.0.0.1 -r -t home-assistant/emails/`echo $1 | awk '/garagedoorclosed/ {print "garagedoor"}'` -m "closed"

I confirmed my current script is as described above.

I should also mention, I’m running Home Assistant 0.42.4 (current) on a Raspberry PI 3, pip install (no docker).