Output from a template must always return a positive number

I have a sensor template to calculate power usage, the difference between what I generate with solar PV, and actual power in either direction from the grid, depending on generated level the result is either positive or negative. I want to always show as a positive integer
I have :-

  - platform: template
    sensors:
      actual_power_usage: #generated minus total grid power
        entity_id:
          - sensor.shellyem_109664_channel_1_power
          - sensor.shellyem_109664_channel_2_power
        value_template: >-
          {% set generated = states.sensor.shellyem_109664_channel_1_power.state|float %}
          {% set grid = states.sensor.shellyem_109664_channel_2_power.state| float %}
          {{ (generated - grid)|round(1) }}

would something like :-

{{ (INT(generated - grid))|round(1) }}

always return a positive number ??

{{ (generated - grid)|abs|round(1) }}

Thank you very much Petro

1 Like

Just a foot note
As the direction of current flow from the grid in my application will change sign -ve out and +ve in,
to avoid the sum being displayed and only the difference , subtracting a negative number will add, not subtract , I used the following

{{ ((grid | abs) - generated)|abs|round(1) }}