Template Blueprint
Filter_positive_or_negative_value_changes_in_sensor_entity.yaml
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