How to calculate the difference of two sensors (high and low) in percentage

Hi Community,

Hope someone can help me. I have created a simple automation for my battery to load when energy prices are low. I used this for that. The configuration sets the time on a helper and I use that helper to trigger the automation.

In the automation I used the entso_lowest_energy_price_today and entso_highest_energy_price_today to check if the price is between a range and if that is true then it triggers.

The problem with this approach is that like today the price is 17.8cents on lowest and 29.3cents on highest so my automation would not trigger but I do want to load the batterij.

Question:

How do I calculate the difference between the entso_lowest_energy_price_today and entso_highest_energy_price_today in percentage? Lets say, if the price between highest and lowest is more then 10% then load.

alias: "[AUTOMATION-PRICE] Notify en laad sessy op het goedkoopste moment"
description: ""
trigger:
  - platform: time
    at: input_datetime.device_start_time
condition:
  - condition: and
    conditions:
      - condition: numeric_state
        entity_id: sensor.entso_lowest_energy_price_today
        below: 0.2
      - condition: numeric_state
        entity_id: sensor.entso_highest_energy_price_today
        above: 0.35
  - condition: or
    conditions: []
action:
  - device_id: 1de5f9b1fcc07ee3d69bb0a4f1d42da6
    domain: select
    entity_id: e9d4a0f2ffda06052db2b6db0ab64125
    type: select_option
    option: api
  - device_id: 1de5f9b1fcc07ee3d69bb0a4f1d42da6
    domain: number
    entity_id: 4cc39646960bc0825a2fa8abe2bf06c4
    type: set_value
    value: -1900
  - service: notify.whatsapp
    data:
      message: >-
        HA meldt: Energie is nu de aankomende 3 uur het goedkoopst. De prijs van
        gas is tot 06:00 {{ states('sensor.zonneplan_current_gas_tariff') }}
        EUR/m³ en de kWh prijs is nu {{
        states('sensor.zonneplan_current_electricity_tariff') }} Tip! {{
        states('sensor.zonneplan_status_tip') }} Sessy gaat nu laden.
mode: single

Solved it by creating a helper with the following code;

{% set lowest = states.sensor.entso_lowest_electricity_market_price.state|float %}
          {% set highest = states.sensor.entso_highest_energy_price_today.state| float %}
          {{ (highest - lowest)|round(2) }}