Subtract two sensor values from each other

I try to subtract two sensor values from each other I use this

  - sensor:             
      - name: "Power Remaining Home Generator AC Current"
        unique_id: power_remaining_home_generator_ac_current
        device_class: current
        unit_of_measurement: 'A'
        state_class: measurement
        icon: mdi:alpha-a
        state: >
          {% set home_generator = states('input_number.power_home_generator_ac_current') | float %}
          {% set total_ac_current = states('sensor.power_total_ac_current') | float %}
          {{ ((home_generator - total_ac_current) ) | round(1, default=0) }}
                     
        availability: >
          {{ 
            states('input_number.power_home_generator_ac_current') | float(none) != none and
            states('sensor.power_total_ac_current') | float(none) != none
          }}                    

get this error

Logger: homeassistant.helpers.template
Source: helpers/template.py:1292
First occurred: 17:48:53 (1 occurrences)
Last logged: 17:48:53

Template warning: 'float' got invalid input 'unavailable' when rendering template '{% set home_generator = states('input_number.power_home_generator_ac_current') | float %} {% set total_ac_current = states('sensor.power_total_ac_current') | float %} {{ ((home_generator - total_ac_current) ) | round(1, default=0) }}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2022.1

thanks

You are getting that warning because you have not assigned defaults to your float filters. The requirement for defaults explained in the link below have not been fully implemented yet but they could be at any time. You need to update you templates or they will stop working when that happens.

If you want your example template to keep working as they have been, replace all instances of float with float(0).

{% set home_generator = states('input_number.power_home_generator_ac_current') | float(0) %}
          {% set total_ac_current = states('sensor.power_total_ac_current') | float(0) %}
          {{ ((home_generator - total_ac_current) ) | round(1, default=0) }}

THANK YOU :smiley:

I Hope this is the right place to ask this question:

But why is it that, this template does not round to 2 numbers when subtracting and does round to 2 numbers when adding?

   - platform: template
     sensors:
      from_grid_high:
        friendly_name: From Grid High
        unit_of_measurement: 'KWh'
        value_template: '{{ states("sensor.t2") | float | round(2) - states("sensor.T2_daystart") | float | round(2)}}'       
        device_class: energy 

result —> 0.970000000000255

  - platform: template
    sensors:
      from_grid_high_plus:
        friendly_name: From Grid High plus
        unit_of_measurement: 'KWh'
        value_template: '{{ states("sensor.t2") | float | round(2) + states("sensor.T2_daystart") | float | round(2)}}'       
        device_class: energy 

result —> 12784.85

Not novice but not savvy either, especially not in templating, so please be lenient :wink:

try this:

  - platform: template
     sensors:
      from_grid_high:
        friendly_name: From Grid High
        unit_of_measurement: 'KWh'
        value_template: '{{ (states("sensor.t2") | float | round(2) - states("sensor.T2_daystart") | float | round(2)) | round(2) }}'       
        device_class: energy

apply round at the last stage.

nope sadly didn’t work.

BTW
the value of t2 = 6392.906
value of t2_daystart = 6391.942

Was wondering if there was a solution to the above? I have tried something similiar. Works fine in template editor as well as configurations, but when I check the state on Developer Tools I get hit with unknown. Is there something super simple here I am missing? Thanks!

 - platform: template
   sensors:
      all_other_office_kitchen:
        friendly_name: Other Office & Kitchen
        unit_of_measurement: 'KWh'
        value_template: '{{ (states("sensor.plugs_office_kitchen_2_1d") | float | round(2) - states("sensor.main_desk_today_s_consumption") | float | round(2)) | round(2) }}}}'       
        device_class: energy
        icon_template: mdi:lightning-bolt-outline

Seems a trigger is missing, error message:
“This template does not listen for any events and will not update automatically.”
How do I have to trigger that?
I would like to update it every 30 seconds.

You don’t give any details of what you are talking about. I assume you’ve created a template in the Editor?

What are you trying to do?

Templates like the ones in the prior posts will be updated whenever either of the referenced sensors changes. You don’t need to specify “every 30 seconds”: it just happens. The message suggests your template has issues. Post it here in full, correctly formatted with the </> button.

Since the posts above were written, HA has evolved and you can now create template sensors in the UI, under Helpers:

I am newbee and I did completely wrong

I created/tested it under Developper tools->Templates (where it also says )
my code, pasted then to UI

# Calculate Remaining Power
  - platform: template
    sensors:
      remainingpower:
        value_template: "{{ ( ('sensor.smappee_1107003070_local_total_production_active_power') | int(0)) - ( ('sensor.smappee_1107003070_local_total_production_active_power') | int(0)) }}"
        unit_of_measurement: 'W'
        friendly_name: Remaining Power
        default: 0

Now it is working, I missed the word “states” and removed all other parameters
Pasted only:

{{ (states ('sensor.smappee_1107003070_local_total_production_active_power') | int(200)) 
 - (states('sensor.smappee_1107003070_local_total_consumption_active_power') | int (100)) }}

Thank you

You had confused the YAML configuration for the entire sensor (in “legacy” format because it’s an old topic) with the Jinja template that sets the state. You are not the first person to make that mistake.

I’m glad it’s working for you now :).