Problems with imap_email_content sensor in automation (solved)

Sorry, long post follows… I hope someone can help and also added the snippets so others might re-use it in their installation. Might be me, but it took some time to understand how value_template could be used in the imap sensor…

I am trying to integrate my alarm system in HA. My alarm is not compatible with HA as such, but it does have an option to email changes, so I made a free gmail account and have my alarm system send the emails to that address when it is turned on or off. In the email’s subject line the value can be found, as well as the name of the person who turned off the alarm.

In the configuration.yaml I have this entry:

  • platform: imap_email_content
    name: IncomingAlarm
    port: 993
    username: [email protected]
    password: secretpassword
    • [email protected]
      value_template: >-
      {% set list1 = subject.split(’ ') %}
      {% if list1[0] == “Arming” %}
      {% elif list1[0] == “Disarming” %}
      ‘Disarmed {{list1[3]}}’
      {% else %}
      {% endif %}

This works perfectly and the value of the alarm switches between “Armed” and “Disarmed - Person” as expected.

Second part of the installation is a magnetic sensor to know if my front gate it open or not. This is just an RF magnetic sensor which I read with a sonoff bridge over MQTT. This part also looks nice and works.>

  - platform: mqtt
    name: "Gate state"
    state_topic: "tele/RFbridge1/RESULT"
    value_template: '{{value_json.RfReceived.Data}}'
    payload_on: "D08C0A"
    payload_off: "D08C0E"
    device_class: Door
    optimistic: false
    qos: 1
    retain: false

I have an automation which send me a notification if the alarm changes state.
What I do want though, is to only get a notification when I put the alarm on AND the gate it still open.

I tried to have the automation with an included condition, but that doesn’t seem to work.
As an intermediate I added a template binary_sensor, but that sensor seems to not change state when the alarm sensor does. I am not sure whether this will be needed, but a fix of this pseudo-sensor might also be welcome…

  - platform: template
        value_template: >-
          {{ is_state('sensor.IncomingAlarm', 'Armed') }}

The automation I tried is this - it does include some variables just to check their state…
binary_sensor.hekken_status actually does show the right value, but I don’t seem to be able to use it in the condition. When I remove the condition, I always get the notification. When I try to change the trigger with an added ‘to: “Armed”’, it does no longer trigger, even though that value is returned in the notification if I remove that line.

- id: '1543006669055'
  alias: Alarm on Gate open
  - entity_id: sensor.incomingalarm
    platform: state
  - condition: state
    entity_id: binary_sensor.hekken_status
    state: "on"
      title: "Alarm turned on"
      message: "But the gate it still open!\nAlarm: {{ states.sensor.incomingalarm.state }}\nGate (on=open; off=closed): {{ states.binary_sensor.hekken_status.state }}"
    service: notify.iosapp_thomass_iphone

Who can magically mix the required ingredients and help me with my setup?

Thanks a lot in advance!

After searching for weeks and mere hours after posting it here, I found the error in my code…

The value_template in the imap_email_content was wrong, causing the values to have bad content (including single quotes), which of course were not what I needed.
This is the correct code, for those who might be interested:

- platform: imap_email_content
  name: IncomingAlarm
  port: 993
  username: [email protected]
  password: secretpassword
    - [email protected]
  value_template: >-
    {% set list1 = subject.split(’ ') %}
    {% if list1[0] == "Arming" %}
    {% elif list1[0] == "Disarming" %}
      Disarmed {{list1[3]}}
    {% else %}
    {% endif %}

Now I get the exact notification I wanted (when gate is open and I turn the alarm on; no notification when the gate is already closed) and the tempate binary sensor also switches as expected - though I don’t really need it anymore.