Power Entity - Customise negative value to show grid export

Hi everyone!

I am very new to HA so bear with me. I have an Octopus Energy Mini that I have working very well with HA. I have a very basic numerical display of current grid consumption.

I have solar, but have not been able to configure anything yet to provide generation data. When I am exporting, the grid consumption figure changes to a negative value. Is it possible with a bit of custom code to show this as a positive figure, and change the icon to show that power is being exported to the grid?

Long story short, my meter doesn’t show export and I don’t get paid for export, but I would like to show my export data, and have an indication on my dashboard that I am exporting.

Many thanks in advance,
Bret

Yes it is, like this:

configuration.yaml

template:
  - sensor:
      - name: Import Power # positive grid power values
        unique_id: grid_import_power
        device_class: power
        state_class: measurement
        unit_of_measurement: W
        state: "{{ [0, states('sensor.your_grid_power_sensor_here')|float(0)]|max }}"
        availability: "{{ has_value('sensor.your_grid_power_sensor_here') }}"

      - name: Export Power # negative grid power values returned as a positive number
        unique_id: grid_export_power
        device_class: power
        state_class: measurement
        unit_of_measurement: W
        state: >
          {% if states('sensor.your_grid_power_sensor_here')|float(0) < 0 %}
            {{ states('sensor.your_grid_power_sensor_here')|float(0)|abs }}
          {% else %}
            0
          {% endif %}
        availability: "{{ has_value('sensor.your_grid_power_sensor_here') }}"

You will need to restart Home Assistant if this is the first time you have used the template integration. Otherwise a template reload will do.

It will create two positive value sensors:

sensor.import_power

and

sensor.export_power

Hi Tom,

Thanks very much for this. Sorry for the late reply, it’s the first chance I’ve had to get on the computer again! I’m going to try this now.

Bret

Me again! So, that works exactly as you’ve described Tom, thank you. I have attached a screenshot of what I now have. What I would like to achieve if possible, is have only one sensor showing, but change the icon and it’s colour to show import or export, I have the two different icons but not sure if this is possible to do with the code you have kindly provided?

Energy

Many thanks for all of your help.
Bret

I managed to do this with conditional cards. The Grid Consumption card will show for any figure above zero and the Solar Export card shows for anything below zero. Probably not the best way to do it but it works.