Automation IMAP Trigger

Hi,

i’m new to this forum and of course new to “Home Assistant”.

I’ve got problems with my automation:

  - alias: E-Mails from blabla
    trigger:
      platform: Emails
      entity_id: sensor.emails
      above: 0
    action:
      service: light.turn_on
      entity_id: light.kitchen

The sensor worked, but there is no enlightenment.

 - platform: imap
    server: blabla
    port: 993
    name: Emails
    username: xxxxxxxxxxxxx
    password: xxxxxxxxxxxxx

I’ve found an error! instead of “platform: Emails” it should be “platform: numeric_state”.
But if the number increases, there is still no action. The Automation itself turns on the light. It seems that there is no connection between the sensor and the automation.

There are two imap sensors; one that does unread and one that does content. What does your sensor setup look like?

I’ve the first one. Only unread. The sensor works. Both, the sensor and the automation work. But if there is an unread mail (the sensor shows 1) nothing happens.

So when you go to the dev panel and look at the entity, you’re getting a numeric value of the unread mails?

I’m asking because I don’t use the IMAP sensor myself.

Yes. For instance: Entity: sensor.emails; State:1

Could you use a value template for this?

value_template: "{{ states('sensor.emails') | int > 0 }}"

Perfect that works!

   automation:

    - alias: E-Mails from blabla
      hide_entity: False
      trigger:
        platform: template
        value_template: "{{ states('sensor.emails') | int > 0 }}"
      action:
        service: light.turn_on
        entity_id: light.kitchen

Thanks a lot Robert!

Awesome! Can you mark it as solved so that others can find the solution? Thanks!

Does this fire if the value increases from 1 to 2, 2 to 3, etc…, or will it only fire once, when the value goes above 0?

Here is my configuration that runs an action whenever new mail is received (but not when unread mail is being read):

sensor:
  - platform: imap
    name: mailcount
    server: !secret mail_server
    username: !secret mail_username
    password: !secret mail_password

binary_sensor:
  - platform: trend
    sensors:
      mailcount_increase:
        entity_id: sensor.mailcount

automation:
  - alias: "New mail"
    trigger:
      - platform: state
        entity_id: binary_sensor.mailcount_increase
    condition:
      - condition: state
        entity_id: binary_sensor.mailcount_increase
        state: 'on'
    action:
      service: light.lifx_effect_pulse
      entity_id: light.office
      data:
        mode: breathe
        brightness_pct: 80
        color_name: cyan

Can’t seem to get this to work, I also noticed the binary sensor doesn’t turn ‘on’ when new email arrives, but the count increases, any ideas?

Sensor=

- platform: imap
  server: imap.gmail.com
  port: 993
  name: Email Count
  username: my_email_address
  password: password
  folder: inbox

Binary Sensor=

- platform: trend
  sensors:
    email_count_increase:
      entity_id: sensor.email_count

Automation=

- alias: "New Email"
  trigger:
  - platform: state
    entity_id: binary_sensor.email_count_increase
    state: 'on'
  condition:
      - condition: state
        entity_id: 'device_tracker.my_phone'
        state: 'home'  
  action:
  - service: media_player.sonos_snapshot
    data_template:
      entity_id: media_player.living_room
      with_group: true
  - service: media_player.sonos_unjoin
    data_template:
      entity_id: media_player.living_room
  - service: media_player.volume_set
    data_template:
      entity_id: media_player.living_room
      volume_level: 0
  - service: media_player.volume_set
    data_template:
      entity_id: media_player.living_room
      volume_level: 0.6
  - service: tts.amazon_polly_say
    data_template:
      entity_id: media_player.living_room
      message: You have new email.
  - delay:
      seconds: 1
  - delay: "{% set duration = states.media_player.living_room.attributes.media_duration\
      \ %} {% if duration > 0 %}\n  {% set duration = duration - 1 %}\n{% endif %}\
      \ {% set seconds = duration % 60 %} {% set minutes = (duration / 60)|int % 60\
      \ %} {% set hours = (duration / 3600)|int %} {{ \"%02i:%02i:%02i\"|format(hours,\
      \ minutes, seconds)}}"
  - service: media_player.sonos_restore
    data_template:
      entity_id: media_player.living_room
      with_group: true

Your automation is invalid, keep the trigger and condition like I had it (you can of course add more conditions).

The binary sensor should work, which attributes does it show (in the Developer Tools)?

riendly_name: email_count_increase
entity_id: sensor.email_count
invert: false
sample_duration: 0
sample_count: 1
min_gradient: 0
gradient: null

The state doesn’t change.

That’s the initial state, the sample_count should change to 2 when getting the first mail.

Sorry, I do not see why it isn’t working.

I’m getting the following Error if it applies?

2017-12-14 17:46:18 ERROR (MainThread) [homeassistant.helpers.entity] Update for binary_sensor.email_count_increase fails
Traceback (most recent call last):
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/entity.py”, line 199, in async_update_ha_state
yield from self.async_device_update()
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/entity.py”, line 304, in async_device_update
yield from self.async_update()
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/binary_sensor/trend.py”, line 179, in async_update
yield from self.hass.async_add_job(self._calculate_gradient)
File “/usr/lib/python3.5/asyncio/futures.py”, line 380, in iter
yield self # This tells Task to wait for completion.
File “/usr/lib/python3.5/asyncio/tasks.py”, line 304, in _wakeup
future.result()
File “/usr/lib/python3.5/asyncio/futures.py”, line 293, in result
raise self._exception
File “/usr/lib/python3.5/concurrent/futures/thread.py”, line 55, in run
result = self.fn(*self.args, **self.kwargs)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/binary_sensor/trend.py”, line 195, in _calculate_gradient
import numpy as np
File “/srv/homeassistant/lib/python3.5/site-packages/numpy/init.py”, line 142, in
from . import add_newdocs
File “/srv/homeassistant/lib/python3.5/site-packages/numpy/add_newdocs.py”, line 13, in
from numpy.lib import add_newdoc
File “/srv/homeassistant/lib/python3.5/site-packages/numpy/lib/init.py”, line 19, in
from .polynomial import *
File “/srv/homeassistant/lib/python3.5/site-packages/numpy/lib/polynomial.py”, line 13, in
import numpy.core.numeric as NX
AttributeError: module ‘numpy’ has no attribute ‘core’

Yes, that is the problem. It seems you have a dirty install.

dirty install?

the sensor, or HASS?

The problem is that HASS cannot load the numpy module. Unfortunately I do not know what the solution is, besides installing HASS from scratch in a fresh virtualenv.

Great, thanks for all the help.