Imap content sensor line break

Hello! I use a Imap content sensor to track the loss of power on the UPS. The UPS sends the following email messages:

image
image

I used the following configuration:

  - platform: imap_email_content
    server: imap.gmail.com
    name: imap_content_APC
    port: 993
    username: 
    password: 
    senders:
      - 
    value_template: >-
      {% if 'On Battery' in body %}
        On Battery
      {% elif 'No Longer On Battery' in body %}
        Main power
      {% endif %}

But it didn’t work as required, due to the fact that when power was restored, the message also contains the “On Battery”

How to write a sensor correctly so that it takes into account text wrapping on a new line?

Thanks in advance!

The first wins.
Something nice to play in Devtools/Templates

{% set text = "No Longer On Battery" %}
{% if 'No Longer On Battery' in text %}
  Main power
{% elif 'On Battery' in text %}
  On Battery
{% endif %}

Returns ‘Main power’.

{% set text = "On Battery" %}
{% if 'No Longer On Battery' in text %}
  Main power
{% elif 'On Battery' in text %}
  On Battery
{% endif %}

Returns ‘On Battery’

{% set text = "No Longer On Battery" %} # or "On Battery"
{% if 'On Battery' in text %}
  On Battery
{% elif 'No Longer On Battery' in text %}
  Main power
{% endif %}

Returns always ‘On Battery’

1 Like

Thank you!

An alternative that actually looks for the line break — so a bit more of a “general” solution:

{% if '\nOn Battery' in body %}
  On Battery
{% else %}
  Mains power
{% endif %}