Filter positive or negative value changes in a sensor entity

Template Blueprint

Filter_positive_or_negative_value_changes_in_sensor_entity.yaml

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

This template blueprint will add either the positive or negative value changes (increase/decrease) in a source entity to a destination entity. The destination entity will always increase.

Use it to split balancing entities like into import/export, charge/discharge, load/unload entities which always count up. Implement one template blueprint for positive changes and one for negative changes as needed.

Background

For example, energy import and export is often measured in a net entity that will increase when importing and decrease when exporting. The energy dashboard expects import and export in separate entities.

An example is the HA core Enphase_Envoy integration that provides data for the IQ Gateway a.k.a Envoy. In some setups only a net sensor is available for grid import/export or battery energy is only available in energy content changing with charge and discharge.

Prerequisite

This blueprint requires the time in seconds since home assistant started running. This can be calculated by configuring the Uptime integration in Home Assistant and using a template sensor. This uptime sensor is used to detect Home Assistant restarts and must be specified as input.

Unavailable/unknown states

This blueprint depends on change from a previous value. To handle HA outages or entity unavailable/unknown status, it caches the last good value prior to these states as an attribute of the destination entity.

It will however not differentiate if both positive and negative changes occur during those situations, only the net change over the outage is determined using the cached last good value prior and new good value received post the situation.

Blueprint Inputs

The blueprint accepts below inputs for configuration:

source_entity

Source sensor entity to monitor for value changes.
Required input

changes

Specify if positive or negative value changes (increase/decrease) should be added to the destination sensor.
Optional input, default positive changes if not specified.
Options: positive or negative.

uom

Unit of Measurement to use for the destination entity.
Required input.

uptime

Specify an a template sensor providing time in seconds since HA start.
Required input.
Device_class: duration sensor entity (see below in example)

uptime_delay

Delay time in seconds for the uptime trigger to fire after HA startup.
Optional input, default 10 seconds if not specified.

correction_value

One-time correction of the destination entity value.
Optional input, default 0 if not specified.

Can be used to set an initial value or apply a correction later. The correction is only applied when this setting is changed from a previous specified correction value.

For example when value 100 was specified in the template, 100 is added 1 time to the destination entity value. If later another correction of 2 is needed, just change the input to 2 and reload the template entities.

decimals

Number of decimal places to round destination value to.
Optional input, default 6 if not specified.

Templates

Filter positive value change

To filter positive or negative value changes into sensor entities add templates like below to your template configuration yaml file.

- use_blueprint:
    path: catsmanac/Filter_positive_or_negative_value_changes_in_sensor_entity.yaml
    input:
      source_entity: sensor.net_grid_energy
      uptime: sensor.ha_uptime
      changes: positive
      uom: kWh
  name: grid energy import

- use_blueprint:
    path: catsmanac/Filter_positive_or_negative_value_changes_in_sensor_entity.yaml
    input:
      source_entity: sensor.net_grid_energy
      uptime: sensor.ha_uptime
      changes: negative
      uom: kWh
  name: grid energy export

Uptime entity

To create an Uptime sensor entity add a template like below to your template configuration yaml file.

- sensor:
  - name: HA Uptime
    unique_id: ha_uptime
    state: >
      {{ (as_timestamp(now()) - as_timestamp(states('sensor.uptime')))|int }}
    unit_of_measurement: 's'
    device_class: duration
    state_class: measurement

Edits:

  • June 24, 2026
    • Update wording for uptime sensor
    • Fix blueprint path name in template example, actual blueprint name starts with capital F:
      • catsmanac/Filter_positive_or_negative_value_changes_in_sensor_entity.yaml
1 Like

FYI you can get the previous state without storing it like you do. Using this.state

I’d also recommend adding restrictions to your uptime sensor as most uptime sensors are datetime objects not counting up numbers.

Thanks for the feedback @petro.

FYI you can get the previous state without storing it like you do. Using this.state

If I’m not mistaken, this only refers to the target entity and I need the previous state of the source entity before it became unavailable or unknown

I’d also recommend adding restrictions to your uptime sensor as most uptime sensors are datetime objects not counting up numbers.

How would I do that? I’m pretty new on template blueprints.

But you have this convoluted way to get the attribute

{% set saved_previous_state = state_attr(this.entity_id, ‘previous_state’)| float(0) %}

this.attributes.previous_state and it will already be a float.

You can add filters to entity selectors.

Ah, I see. Going by the Warning in the Templating - Home Assistant sensor section It is strongly advised to use the states(), is_state(), state_attr() and is_state_attr() as much as possible, …. Is that kind of obsolete these days?

EDIT: The construct like it is, protects from the warning has no attribute 'previous_state' showing in the log file at very first run of a newly added template sensor. The attribute this.attributes.previous_state only exists after first execution.

You can add filters to entity selectors.

Thanks for that pointer. Think that would make it:

    uptime:
      name: Home Assistant uptime
      description: Specify an Uptime integration sensor to use for HA startup detection
      selector:
        entity:
          domain: sensor
          device_class: duration

hi nice code, the Problem for enphase is that the value from the sensor are not always really good… :confused:

From time to time, when consumption changes rapidly,we can observe negative and positive spikes.

Enphase Envoy Battery reporting - #26 by Esmax555

You would not use this for power entity like in your link, but for energy entities.

Hello ! Is there any reason to build your own uptime sensor vs the native integration?

Any uptime sensor that provides seconds since start will work. The blueprint triggers 10 seconds (configurable) after HA start and needs an integer value, not a data-time object as was mention in one of the replies.

The sensor itself is build with the native uptime integration, if that is what you are asking?

Ok I think I got what you said. The fact that the description of the uptime input is asking for an instance of the uptime integration, which is unique, confused me.

I see what you mean, the wording does not represent what actually is going on.

Would this be more clear for description:

This blueprint requires the time in seconds since home assistant started running. This can be calculated by configuring the Uptime integration in Home Assistant and using a template sensor. This uptime sensor is used to detect Home Assistant restarts and must be specified as input.

And for:

uptime

Specify an a template sensor providing time in seconds since HA start.
Required input.
Device_class: duration sensor entity (see below in example)

Does the import blueprint link work for anyone else? I just tried another random one from the forums and it worked, but I cannot get this one to import.

I see it only loads in the yaml and doesn't show in the UI. I tried adding a templates section in the configuration yaml but it keeps failing to find it.

From the directory that has the configuration.yaml file there is blueprints/template/catsmanac/Filter_positive_or_negative_value_changes_in_sensor_entity.yaml.

$ ll blueprints/template/catsmanac/Filter_positive_or_negative_value_changes_in_sensor_entity.yaml
-rw-r--r--. 1 mosquitto 4.8K Jun 23 12:39 blueprints/template/catsmanac/Filter_positive_or_negative_value_changes_in_sensor_entity.yaml

configuration.yaml snippets:

template:
  - use_blueprint:
      path: blueprints/template/catsmanac/Filter_positive_or_negative_value_changes_in_sensor_entity.yaml
      input:
        source_entity: sensor.envoy_482544054638_lifetime_balanced_net_energy_consumption
        uptime: sensor.ha_uptime
        changes: positive
        uom: kWh
    name: grid energy import

  - use_blueprint:
      path: blueprints/template/catsmanac/Filter_positive_or_negative_value_changes_in_sensor_entity.yaml
      input:
        source_entity: sensor.envoy_482544054638_lifetime_balanced_net_energy_consumption
        uptime: sensor.ha_uptime
        changes: negative
        uom: kWh
    name: grid energy export


... omitted lines ...

sensor:
  - name: HA Uptime
    unique_id: ha_uptime
    state: >
      {{ (as_timestamp(now()) - as_timestamp(states('sensor.uptime')))|int }}
    unit_of_measurement: 's'
    device_class: duration
    state_class: measurement

Not sure why the blueprint doesn't show up in the UI and not sure what needs to change from the examples to make this work in the yaml. Can someone please help? I'm guessing I'm missing something very basic here. Not sure why the HA integration for enphase doesn't provide the necessary input to fill out the energy dashboard vs having to go through all of this.

It likely imported but templates do not have the ability to be setup through the UI

I don't think it did. I see in the logs the following error

2026-06-23 13:05:31.236 ERROR (MainThread) [homeassistant.config] Invalid config for 'template' at configuration.yaml, line 146: Failed to load blueprint: Unable to find blueprints/template/catsmanac/Filter_positive_or_negative_value_changes_in_sensor_entity.yaml, please check the docs at Template - Home Assistant

And in the UI I see the same thing. I keep trying different paths like full path from same dir of config file, from blueprints dir, from templates dir, etc.

Open up your config folder and browse to the locations you're trying. This is a normal file system. Once you know the location, use that path.

Got them working now. I think originally the problem was that the file name has Filter not filter. Capital F. And I went down a rabbit hole of thinking the pathing was wrong in the examples.

I'm guessing I'm using the wrong sensor here, but I went with the one mentioned in the docs that lead to here.

template:
  - use_blueprint:
      path: catsmanac/Filter_positive_or_negative_value_changes_in_sensor_entity.yaml
      input:
        source_entity: sensor.envoy_482544054638_lifetime_balanced_net_energy_consumption
        uptime: sensor.ha_uptime
        changes: positive
        uom: kWh
    name: grid energy import

  - use_blueprint:
      path: catsmanac/Filter_positive_or_negative_value_changes_in_sensor_entity.yaml
      input:
        source_entity: sensor.envoy_482544054638_lifetime_balanced_net_energy_consumption
        uptime: sensor.ha_uptime
        changes: negative
        uom: kWh
    name: grid energy export

After updating the energy dashboard's electricity grid section to use these import/export values, the dashboard doesn't show any export.

Guessing I'm missing something basic again.

I've updated the blueprint description for the capital F issue, my mistake, sorry for the confusion.

As for using the blueprint en getting actual values. Can you confirm that you have a TOTAL-CONSUMPTION CT (Current transformer) installed? This will show in the HA device info page for the Envoy , or download the diagnostics file for details on what is configured.

I assume you verified there is actual data variations in sensor.envoy_482544054638_lifetime_balanced_net_energy_consumption

I do and there is actual data variations in the lifetime balanced net energy consumption. I think my last problem was just impatience :sweat_smile:

This pic after the rest of the day went on shows what I'm assuming are correct values, but I'll need to wait another day to have a full day's worth of info setup this way and then check the maths myself.

Thank you for following up and thanks to petro as well for helping me through this. Would be neat if HA was able to do this math automatically via the energy dashboard without the extra steps.