Create new sensor based upon inversed energy meter

Hi all,

I have an old fashioned energy meter. I’ve used this great tutorial to get a ESP32 camera to take pictures every 5 minutes to ‘read’ the energy meter (Welcome - AI on the Edge Device) and deliver the numbers back through MQTT (sensor.electra_meter_value in the code below). That all works fine. Usually the meter increases, but on an sunny day my solar panels return power to the grid and the meter value decreases.

Now I’m trying to get a sensor which determines how much energy I give back to the grid. Basically I want to make a sensor that only calculates the amount the meter decreases to add to the energy dashboard.

I’ve used this forum link to make the two utility meters (How can I configure the energy dashboard to understand my net electric meter? - #24 by wildekek) and tried to add a new sensor that inverses the meter readings.

This is the code that I have in the configuration.yaml

platform: template
  sensors:
    grid_energy_inverted:
      value_template: "{{ 'sensor.electra_meter_value' | multiply(-1) | float }}"
      unique_id: grid_energy_inverted_calc

utility_meter:
  grid_energy_consumed:
    name: "Grid Energy Consumed"
    unique_id: grid_energy_consumed
    source: sensor.electra_meter_value
    delta_values: false
    net_consumption: false
    periodically_resetting: false

  grid_energy_produced:
    name: "Grid Energy Produced"
    unique_id: grid_energy_produced
    source: sensor.grid_energy_inverted
    delta_values: false
    net_consumption: false
    periodically_resetting: false

I’m getting two errors in the logs:

TemplateError('ValueError: Template error: multiply got invalid input 'sensor.electra_meter_value' when rendering template '{{ 'sensor.electra_meter_value' | multiply(-1) | float }}' but no default was specified') while processing template 'Template<template=({{ 'sensor.electra_meter_value' | multiply(-1) | float }}) renders=4>' for attribute '_attr_native_value' in entity 'sensor.grid_energy_inverted'`

And the other log error:

Logger: homeassistant.helpers.event
Source: helpers/template.py:587
First occurred: 13:42:43 (1 occurrences)
Last logged: 13:42:43

Error while processing template: Template<template=({{ 'sensor.electra_meter_value' | multiply(-1) | float }}) renders=2>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1880, in multiply
    return float(value) * amount
           ^^^^^^^^^^^^
ValueError: could not convert string to float: 'sensor.electra_meter_value'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 585, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)`
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^`
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2600, in _render_with_context
    return template.render(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1884, in multiply
    raise_no_default("multiply", value)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1849, in raise_no_default
    raise ValueError(
ValueError: Template error: multiply got invalid input 'sensor.electra_meter_value' when rendering template '{{ 'sensor.electra_meter_value' | multiply(-1) | float }}' but no default was specified

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 713, in async_render_to_info
    render_info._result = self.async_render(
                          ^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 587, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: Template error: multiply got invalid input 'sensor.electra_meter_value' when rendering template '{{ 'sensor.electra_meter_value' | multiply(-1) | float }}' but no default was specified`

What’s going wrong here? Much appreciated for your time!

That’s trying to multiply the string “sensor.electra_meter_value” by -1. You want:

      value_template: "{{ states('sensor.electra_meter_value')|multiply(-1) }}"

No need for the float: multiply does the conversion.

You could use:

      value_template: "{{ states('sensor.electra_meter_value')|float(0) * -1 }}"

Same result:

Worked like a charm! Thanks!