Difference between 2 sensors

Good day everyone… I’m try to do something similar, but i have always error report from HA

Can you please check this code for me?

# Calculated Value Sensor : Battery_Garage_KWH
  - platform: template
    sensors:
      Battery_Garage_KWH2:
        entity_id:
        - sensor.energy_meter_1_energia 
        - sensor.inv_gar_out_energia
        value_template: =
          {set BatGarIn2 = states.sensor.energy_meter_1_energia.state| float }
          {set BatGarOut2 = states.sensor.inv_gar_out_energia.state| float }
          {{ (BatGarIn2 - BatGarOut2)|round(1) }}

What error?

(I can probably guess, looking at that template, but it really helps people help you if you actually share these things)

1 Like

The bracket starts a Jinja2 line.
The characters following the bracket decide how the line should be treated.
{% is an internal handled line
{{ is a line that returns a result

YAML is key-value pairs.
The format is key: value, like platform: template

1 Like

Where exactly did you get all that template code?

I’m guessing some sort of hallucinogenic AI?

1 Like

So far, for help all others having this issue:
Goal: measure the kWh left Garage Battery by an Ingoing and Outgoing kWh sensor of an inverter.
I firstly have created 2 sensor counter to keep trace of daily increment of power Ingoing and Outgoing (avoiding finding them offline by the subtraction template).



Then i create a template helper to do subtraction from the two sensor created like that: {{ (( states('sensor.inv_gar_in_fisso') | float (0) + 0 ) | float (0) - ( states('sensor.inv_gar_out_fisso') | float (0) + 10 ) ) /1 }} and everithing worked well.

Now one last problem:
The “autoconsumption” (well known costant) of the inverter between “in sensor” and “out sensor”.
Can anyone suggest how to costantly or daily increment the “out sensor” of x kWh?

Sorry for my explenation if anyone understand and want to explain better is welcome.