Skip to content

Home Assistant - Split Sensor into Negative and Positive

This is a very quick post on how to take a sensor- such as sensor.grid_power and split it into sensor.grid_power_in and sensor.grid_power_out.

This can be handy if you need a separate sensor for energy/power in, and energy/power out.

For this short post, I will be splitting sensor.battery_power into sensor.battery_power_in and sensor.battery_power_out

How to do it?

First- you need to update your configuration.yaml/templates

I personally keep my templates in a dedicated file named template.yaml. This helps me keep everything organized.

How to store templates in a dedicated file

Simple, in your configuration.yaml, we will tell it to include sensors.yaml.

configuration.yaml
template: !include template.yaml

You can do this with sensors, binary_sensors, or any other section.

How to split sensor into negative/positive sensors

To do this, we will leverage home-assistant's template sensor.

template.yaml
- sensor:
  - name: battery_power_in
    unit_of_measurement: "W"
    state_class: measurement
    device_class: power
    state: >
        {% if states('sensor.battery_power')|float >= 0 %}
          {{ states('sensor.battery_power') }}
        {% else %}
          0
        {% endif %}
  - name: battery_power_out
    unit_of_measurement: "W"
    state_class: measurement
    device_class: power
    state: >
        {% if states('sensor.battery_power')|float < 0 %}
          {{ states('sensor.battery_power') | float  * -1  }}
        {% else %}
          0
        {% endif %}

state_class, and device_class are not required fields. However, if you wish to specify them, here is the documentation for acceptable values:

After making your changes, in home assistant, press C, and Reload Template Entities

If your new sensors do not show up, check your logs for errors.

Summary

This was intended to be a very simple guide on how to split a number into multiple sensors.

The use-case for needing to do this- is to create a dedicated sensor for power going INTO battery storage, or coming IN from the grid, and then, having a sensor for power LEAVING my house or battery.