Imap_email_content + value_template number in the body of the email

Hallo. I get a emails from ebanking that shows changes in a bank account.

I want to set up an imap sensor to track the email, but I want it to use the values in the email for sensor information so I can see it on a graph or card in the front end.
How would I go about that?
My current “half working” email sensor template come from tutorial example

but is parsing random numbers :slight_smile:

IMAP sensor config works:

# Example configuration.yaml entry
sensor:
  - platform: imap_email_content
    server: mail.myemail.cz
    port: 993
    username: [email protected]
    password: somepassword
    folder: Inbox
    senders:
      - [email protected]

Relevant part of email message look like

Date: Mon, 8 Jun 2020 08:18:04 +0200 (CEST)
From: [email protected]
To: [email protected]
Message-ID: [email protected]
Subject: Fio banka - prijem na konte
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
X-UMS-Id: 186437230

P=C5=99=C3=ADjem na kont=C4=9B: 80155454(1903540123)
=C4=8C=C3=A1stka: 190,00
VS: 877
Zpr=C3=A1va p=C5=99=C3=ADjemci: Pivo
Aktu=C3=A1ln=C3=AD z=C5=AFstatek: 4 386,86
Proti=C3=BA=C4=8Det: 3874103271/0100
SS:=20
KS: 0

Part of line what I’m traying parse to sensor is number after : in line

Aktu=C3=A1ln=C3=AD z=C5=AFstatek: 4 386,86

that mean

4 386,86

In my case “half working” template part look like:

  - platform: template
    sensors:
      fio_recent_min:
        friendly_name: Fio Min
        unit_of_measurement: Kč
        value_template: >
         {{ state_attr('sensor.info_ebank_cz','body') | regex_findall_index(find='(\d+\,?\d+)', index=6, ignorecase=False) }}
      fio_recent_max:
        friendly_name: Fio Max
        unit_of_measurement: Kč
        value_template: >
          {{ state_attr('sensor.info_ebank_cz','body') | regex_findall_index(find='(\d+\,?\d+)', index=7, ignorecase=False) }}

or tested template part solo

      fio_recent_difference:
        friendly_name: Fio Difference
        unit_of_measurement: Kč
        value_template: >
          {{ state_attr('sensor.info_ebank_cz','body') | regex_findall_index(find='(-?\d+\, ?\d+)', index=0, ignorecase=False) }}

without success :confused:

Thank you for help with value template

try change syntax
value_template: > must be value_template: >-

woow… after change it by your suggestion, it start parse numbers after white space to coma, that mean 4 386,86

I change it (add \s\ ) to

  - platform: template
    sensors:
      fio_recent_status:
        friendly_name: Fio Last
        unit_of_measurement: Kč
        value_template: >-
          {{ state_attr('sensor.info_fio_cz','body') | regex_findall_index(find='(\d+\s\,?\d+)', index=0, ignorecase=False) }}

and after this change + adding your suggestion >- it parse number with space but without number after coma 4 386,86 but it is enough for me :slight_smile: Thank you.

Btw it is possible to add more examples to the Home Assistant Wiki / Manual? HomeAssistant is great but full usable only for peoples with programming languages basics knowledge… :confused:

I would suggest something like this:

        value_template: >
          {{ state_attr('sensor.info_fio_cz', 'body')
             |regex_findall_index('statek: ([\d\s,]+)') }}
1 Like

It works well + more: your code parse all numbers (cents after comma too). For “non-coders” offers the opportunity to customize the template for their own purpose by changing the easily searchable part of the code. I hope gtranslate works too and you understand me :slight_smile:

1 Like

↑ + This is working solution for unavailable, unknown, none states ↑