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 home assistant Uptime integration to be installed with a template sensor for it configured. 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 Uptime integration sensor to use for HA startup detection.
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
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.