SOLVED - Platform template : convert EUR exchange rate

Hi ,

I am using the “platform: openexchangesrates” to get hourly rate of the EUR USD , for example 0,84 ( 1 USD = 0,84 EUR)

But I would like to get the rate in the other sense ( 1 EUR = 1 / 0,84 = 1,19 ) so I I put these lines in the sensor.yaml file ( sensor: !include sensor.yaml) :

#------------------------------------
- platform: openexchangerates
  api_key: xxxxx
  quote: EUR

- platform: template
  sensors:
    exchange_rate_sensor:
      value_template: 
        "{% set eur_usd_rate = '1'%}"
        "{{ sensor.exchange_rate_sensor | (eur_usd_rate / sensor.exchange_rate_sensor) }}"
#------------------------------------

I could not make this work after different code configurations.

Error loading /config/configuration.yaml: while parsing a block mapping
  in "/config/sensor.yaml", line 24, column 7
expected <block end>, but found '<scalar>'
  in "/config/sensor.yaml", line 26, column 9

Please can you advise ?
Thank you

Read here about building templates and YAML multiline strings.

Thanks @VDRainer to have pointed out the missing multiline “>”

Now there is no more configuration errors.
Let’s see then if it works

#------------------------------------
- platform: openexchangerates
  api_key: xxxxxxx
  quote: EUR

- platform: template
  sensors:
    exchange_rate_sensor:
      value_template: >-
        "{% set eur_usd_rate = 1%}"
        "{{ sensor.exchange_rate_sensor | int }}"
        "{{ eur_usd_rate / sensor.exchange_rate_sensor }}"

The issue is that 2 sensors are created :

  • 1 by the “platform openexchangesrates” : Exchange rate sensor ( default one as per … https://www.home-assistant.io/integrations/openexchangerates/ which is working as initially whithout the platform template (1 USD = 0,84 EUR))

  • 1 by the “platform template” : exchange_rate_sensor which is not working ( unavailable) , expecting 1 EUR = 1 / 0,84 USD = 1,19

Sreenshot enclosed:

Like i said in my first post, read the docs.

  1. Multiline templates have no doublequotes around them.

  2. If your openexchangerates sensor has the entity_id sensor.exchange_rate_sensor, you can’t use that name for the template sensor.

  3. To get the state of a sensor you need.

states.sensor.exchange_rate_sensor.state

or (better)

states('sensor.exchange_rate_sensor')

You can test your templates in Dev Tools/templates.

Thanks for the help , this is my first “templating”

The updated code

- platform: openexchangerates
  api_key: !secret openexchangesrates_api_key
  quote: EUR

- platform: template
  sensors:
    exchange_eurusd_rate_sensor:
      value_template: >
        {% set eur_usd_rate = 1%}
        {{ states('sensor.exchange_rate_sensor') | float }}
        {{ eur_usd_rate / ( states('sensor.exchange_rate_sensor') | float ) }}

The result

So still some work to do as the result is 0,818 1,22 instead of the expected 1.22

Why the second line in your template? Remove it.

     value_template: >
        {% set eur_usd_rate = 1 %}
        {{ (eur_usd_rate / (states('sensor.exchange_rate_sensor') | float)) | round(2) }}

Thank a lot , it works perfectly