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.
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 %}