Add and Subtract States Sensor

This blueprint will help you create sensors which hold the value created by adding and/or subtracting the numeric states of one or more entities, either from the states of other entities or from a static base value.

This sensor will follow the equation A + B - C = D, where D is the output of this sensor.


Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.


blueprint:
  author: Didgeridrew
  homeassistant:
    min_version: 2025.1.0
  name: Add and Subtract State Values
  description: |
    Creates a sensor which holds the value created by adding and/or subtracting the state values of one or more entities, or from a static base value.
    This sensor will follow the equation A + B - C = D, where D is the output of this sensor.
  domain: template
  input:
    base_value:
      name: Base Value
      description: |
        A static value that selected state values will be added to or subtracted from (optional, defaults to 0).
        In the equation A + B - C = D, this input will define A.
      default: 0
      selector:
        number:
          mode: box
          min: -5000
          max: 5000
    sum_entities:
      name: Additive Entities
      description: |
        The states of all entities given in this input will be added to together, then added to the Base Value.
        In the equation A + B - C = D, the sum of these entities' states' will define B.
      default: "sensor.none"
      selector:
        entity:
          multiple: true
          filter:
            - domain: 
                - sensor
                - number
                - input_number
                - counter    
    diff_entities:
      name: Subtractive Entities
      description: |
        The states of all entities given in this input will be added to together, 
        then subtracted from the sum of the Base Value and Additive Entities.
        In the equation A + B - C = D, the sum of these entities' states' will define C.
      default: "sensor.none"
      selector:
        entity:
          multiple: true
          filter:
            - domain: 
                - sensor
                - number
                - input_number
                - counter
    availability_method:
      name: Availability Method
      description: |
        Controls how availability is handled. When using the "all" method, the sensor will be available only if all referenced entities 
        have valid state values. The "any" method will allow the sensor to return a value as long as 1 or more entities have valid state values.
        This input will default to "all" if no value is specified.
      default: all  
      selector:
        select:
          options:
            - all
            - any         
variables:
  base: !input base_value
  sum_ents: !input sum_entities
  diff_ents: !input diff_entities
  a_method: !input availability_method
  to_add: |
    {% if sum_ents is not none and sum_ents != 'sensor.none' %}
      {{ states(sum_ents)|float(0) if sum_ents is not list else sum_ents|select('has_value')|map('states')|map('float',0)|sum }}
    {% else %} 
      0 
    {%endif%}
  to_subtract: |
    {% if diff_ents is not none and diff_ents != 'sensor.none' %}
      {{states(diff_ents)|float(0) if diff_ents is not list else diff_ents|select('has_value')|map('states')|map('float',0)|sum }}
    {% else %} 
      0 
    {%endif%}
sensor:
  state: |
    {% set base = 0 if base is undefined else base %}
    {{ base + to_add - to_subtract }}
  attributes:
    base_value: "{{base}}"
    to_add: "{{to_add}}"
    to_subtract: "{{to_subtract}}"
  availability: |
    {% set u = union(diff_ents,sum_ents) | reject('eq', 'sensor.none') | list %}
    {% if a_method == 'all' %}
      {{ u|reject('has_value')|list|count == 0 }}
    {% else %}
      {{ u|select('has_value')|list|count > 0 }}
    {% endif %}

Template Blueprints are currently (2025.07) only available through YAML configuration. You can find more details about how to use them at: HA Docs - Templates - Using Blueprints

Once you have installed the blueprint, you can use the following configurations as examples to create working sensors in your configuration.yaml file:

template:
  - name: Subtract Counter from sum of Example 1 & 2 
    unique_id: bp_add_subtract_0001
    use_blueprint:
      path: Didgeridrew/add-and-subtract-states-sensor.yaml
      input:
        base_value: 0
        sum_entities: 
          - "sensor.example_1" 
          - "sensor.example_2"
        diff_entities: "counter.example_counter"
        availability_method: all

Hi, Import is not working, telling me unsupported Domain

I can’t reproduce your issue… I have deleted my previous import, restarted HA, and re-imported via the link 3 times without issue.

Hello Matthias Vanselow,

Is your Home Assistant at least that version or newer?

Can I use this blueprint to correct the deviation of my temperature sensor using a offset value? I am currently using a helper.

Hello Fred54,

Look at Compensation - Home Assistant