Sensor template only half of the template send value for IMAP gmail

Hi am trying to make home assistant reading my mail with IMAP and gmail, its working so far but only for the first sensor, the second return no value and i don’t know why?

the code:

- platform: imap_email_content
  server: imap.gmail.com
  port: 993
  username: myemail
  password: ---------
  name: energy_gmail
  folder: inbox
  senders: 
  - myemail
  value_template: '{{ body }}'
  
template:
- sensor:
  - name: "Energy"
    unit_of_measurement: "kWh"
    state: >
       {{ state_attr('sensor.energy_gmail','body')
         | regex_findall_index("energy: ([0-9]+) kWh") }}
  - name: "total"
    unit_of_measurement: "$"
    state: >
        {{ state_attr('sensor.energy_gmail','body')
          | regex_findall_index("Total:  ([0-9]+) $") }}

proof1

the mail:

proof4

As you can see the first sensor (sensor.energy) does return value but the second one (sensor.total) does not and should. the first sensor read all the mail info with the total of 23$ but yet the second sensor does not read it…

Thanks for the help!

Your regex contains two spaces after the colon while your data only seems to contain one. Also, dollar sign has special meaning in regex, so you need to escape it.

  - name: "total"
    unit_of_measurement: "$"
    state: >
        {{ state_attr('sensor.energy_gmail','body')
          | regex_findall_index("Total: ([0-9]+) \$") }}

You can test your templates in developer tools:
Open your Home Assistant instance and show your template developer tools.

{% set a = 'energy: 31 kWh
Total: 23 $' %}
{{ a | regex_findall_index("Total: ([0-9]+) \$") }}

Please post the states as plain text instead of screenshots, it helps people who are trying to help you.

1 Like

Oh it was the space! thank you! am trying to read my electricity provider mail by doing little test like this before, my provider give me dates and hours of electricity peak that i will use to cut my heat, but now the software can’t receive the mail data because of the character form in the mail. because of this b tag (bold text format) in the html code:

<p>
<b>&nbsp;3 janvier</b>
"&nbsp;de 6 h à&nbsp;9 h, en matin"
</p>

the mail is:

Un événement de pointe aura lieu le :

3 janvier de 6 h à 9 h, en matin (a peak will occur: january 3 from 6 h to 9 h, in the morning)

if i remove the date (3 janvier) home assistant will read the mail fine but because of the date not being in text format in the html code home assistant can’t read the whole mail… do you know a way to read the mail? My electricity provider send all those mail in this format making home assistant unable to read the whole mail :frowning:

I’m not sure what you mean by this. Is sensor.energy_gmail not having the correct state (body of the e-mail), or are you having problems with your template sensors?

sensor.energy_gmail don’t receive any data from the mail if the mail contain

3 janvier not being in the standard text format? if i remove this part in a new mail
sensor.energy_gmail will receive the mail

it seems it can’t read it because of the bold tag in the HTML code

Try removing value_template. You are using the body attribute (i.e. not the state of the sensor, but one of it’s attributes) in your template sensors anyway.

If specified this template will be used to render the state of the sensor. If a template is not supplied the message subject will be used for the sensor value.

thanks for the reply i’ll try this when i’ll link this template to my automation wich i don’t know if its possible to do… its more complex than i thought, i made a automation to let the user enter the hour to turn the system OFF and to turn the system ON again, so far its working

code;

  alias: 'Horaire 1 ON '
  description: ''
  trigger:
  - platform: time
    at: input_datetime.horaire_on
    id: input_datetime.horaire_on
  condition: []
  action:
  - service: switch.turn_on
    target:
      entity_id:
      - switch.eau
      - switch.plancher
  mode: single
- id: '1641587395939'
  alias: Horaire 1 Off
  description: ''
  trigger:
  - platform: time
    at: input_datetime.horaire_off
    id: input_datetime.horaire_off
  condition: []
  action:
  - service: switch.turn_off
    target:
      entity_id:
      - switch.eau
      - switch.plancher
  mode: single
- id: '1641587490579'
  alias: Horaire 1 ON switch
  description: ''
  trigger:
  - platform: state
    entity_id: input_boolean.activation
    from: 'off'
    to: 'on'
  condition: []
  action:
  - service: automation.turn_on
    target:
      entity_id:
      - automation.horaire_1_on
      - automation.horaire_off_1
  mode: single
- id: '1641587682135'
  alias: Horaire 1 Off switch
  description: ''
  trigger:
  - platform: state
    entity_id: input_boolean.activation
    from: 'on'
    to: 'off'
  condition: []
  action:
  - service: automation.turn_off
    target:
      entity_id:
      - automation.horaire_1_on
      - automation.horaire_off_1
  mode: single
- id: '1641588836184'
  alias: Horaire 2 ON
  description: ''
  trigger:
  - platform: time
    at: input_datetime.horaire_2_on
  condition: []
  action:
  - service: switch.turn_on
    target:
      entity_id:
      - switch.eau
      - switch.plancher
  mode: single
- id: '1641588881434'
  alias: Horaire 2 OFF
  description: ''
  trigger:
  - platform: time
    at: input_datetime.horaire_2_off
  condition: []
  action:
  - service: switch.turn_off
    target:
      entity_id:
      - switch.eau
      - switch.plancher
  mode: single
- id: '1641588937454'
  alias: Horaire 2 Switch ON
  description: ''
  trigger:
  - platform: state
    entity_id: input_boolean.activation_2
    from: 'off'
    to: 'on'
  condition: []
  action:
  - service: automation.turn_on
    target:
      entity_id:
      - automation.horaire_2_on
      - automation.horaire_2_off
  mode: single
- id: '1641588990528'
  alias: Horaire 2 Switch OFF
  description: ''
  trigger:
  - platform: state
    entity_id: input_boolean.activation_2
    from: 'on'
    to: 'off'
  condition: []
  action:
  - service: automation.turn_off
    target:
      entity_id:
      - automation.horaire_2_on
      - automation.horaire_2_off
  mode: single
- id: '1641709497154'

so this board:

tableau1

let the user set the hour to control those switch:

tableau2

and the objective is to make the template wich is reading the mail with hours data to use this data and change the hour of the first board…

template:

template:
- sensor:
  - name: "hour1_OFF"
    unit_of_measurement: "h"
    state: >
       {{ state_attr('sensor.energy_gmail','body')
         | regex_findall_index("lieu de: ([0-9]+) h") }}
  - name: "hour1_ON"
    unit_of_measurement: "h"
    state: >
        {{ state_attr('sensor.energy_gmail','body')
          | regex_findall_index("a ([0-9]+) h") }}

The “mail reader” read the value: 3 h for hour1_OFF and 5 h for hour1_ON and i don’t know what kind of template i could do to make it interact to take this value and set it up in the first board :thinking:

READER

state:

[sensor.hour1_OFF(Home Assistant)

hour1 3
unit_of_measurement: h friendly_name: hour1off

sensor.hour1_ON

hour2 5 unit_of_measurement: h friendly_name: hour1on

In the developper tool i tried some test but am stoll lost to make the link between the mail data to the board…

{% set time = {
  "hour1_ON": 5,
  "hour1_OFF": 2,
  
} %}

this month: : {{ now().month }} , to this day: {{now().day+1}} , the system turn off at {{ time.hour1_OFF }} the system turn on at {{ time.hour1_ON }}  

output:

this month: : 1 , to this day: 11 , the system turn off at 2 the system turn on at 5 h 

Am really sorry for all those questions… am kinda new to home assistant and am struggling learning template, am really gratefull for the time you are taking!