Add jinja |subtract filter

Since we now need to default our floats in template sensors things have become a bit more contrived but in a fine discussion on Discord I was made aware I could write a template like:

state: >
  {{(states('sensor.grid_energy_teller_1')|float(0)+
     states('sensor.grid_energy_teller_2')|float(0))|round(2,none)}}

which would need an extra availability template on both entities, or some other complex way to guard against none,

as

{{expand('sensor.grid_energy_teller_1','sensor.grid_energy_teller_2')
   |map(attribute='state')|rejectattr('state','in',['unknown','unavailable'])
   |map('float',0)|sum|round(2,none)}}

which is great.

However, many sensors also use other operations at least subtractions, which lead to a current config:

      - unique_id: calculated_totaal_dag_levering_t1
        name: Totaal dag levering T1
        <<: *energy
        icon: mdi:counter
        state: >
         {{(states('sensor.teller_1_terug')|float(0) -
            states('sensor.teller_1_terug_daystart')|float(0))|round(2,none)}}
        availability: >
          {% set x = ['unavailable','unknown'] %}
          {{states('sensor.teller_1_terug') not in x and
            states('sensor.teller_1_terug_daystart') not in x}}

or even worse:

      - unique_id: calculated_totaal_dag_levering
        name: Totaal dag levering
        <<: *energy
        icon: mdi:counter
        state: >
         {{(states('sensor.teller_1_terug')|float(0)
           + states('sensor.teller_2_terug')|float(0) -

           states('sensor.teller_1_terug_daystart')|float(0)
           - states('sensor.teller_2_terug_daystart')|float(0))|round(2,none)}}
        availability: >
          {% set x = ['unavailable','unknown'] %}
          {{states('sensor.teller_1_terug') not in x and
            states('sensor.teller_2_terug') not in x and
            states('sensor.teller_1_terug_daystart') not in x and
            states('sensor.teller_2_terug_daystart') not in x}}

it would really be very nice if we could use the technique of the top template (thank you @petro) with an additional |subtract filter (and maybe some more operations of course… :wink: )

hope anyone will consider and is able to write it…

it is Hacktober after all

thanks!