Energy Management in Home Assistant

Just picked up your reply to my post. Hope you got it sorted but this is what I did. I created five integration sensors as below and then used them in the energy integration. You unfortunately have to accept that the input form your generator is “Grid” as that is not currently configurable. The energy distribution screen grab is from a not very sunny day in December!

- platform: integration
  source: sensor.solar_to_batteries
  name: Energy To Batteries
  unit_prefix: k
  unit_time: h
  round: 1

- platform: integration
  source: sensor.solar_from_batteries
  name: Energy From Batteries
  unit_prefix: k
  unit_time: h
  round: 1

- platform: integration
  source: sensor.solar_direct_use
  name: Energy Direct Use
  unit_prefix: k
  unit_time: h
  round: 1

- platform: integration
  source: sensor.solar_generator
  name: Energy From Generator
  unit_prefix: k
  unit_time: h
  round: 1

- platform: integration
  source: sensor.solar_panels
  name: Energy From Panels
  unit_prefix: k
  unit_time: h
  round: 1

image

hi, I’m also having some trouble getting the correct energy readings in the dashboard.

@LArivee15 I’ve also followed some of your updates, but still not working.

I have an inverter from GoodWe which I integrated in HA and added some custom sensors based on integration attributes:

sensor:
  - platform: template
    sensors:
      pv_temperature:
        value_template: '{{ states.sensor.inverter_goodwe.attributes.tempperature }}'
        unit_of_measurement: 'C'
        friendly_name: "PV Temperature"
      pv_eday:
        value_template: '{{ states.sensor.inverter_goodwe.attributes.eday }}'
        unit_of_measurement: 'kWh'
        friendly_name: "PV energy day"
        device_class: energy
      pv_etotal:
        value_template: '{{ states.sensor.inverter_goodwe.attributes.etotal }}'
        unit_of_measurement: 'kWh'
        friendly_name: "PV energy total"
        device_class: energy
      pv_iday:
        value_template: '{{ states.sensor.inverter_goodwe.attributes.iday }}'
        unit_of_measurement: '€'
        friendly_name: "PV income day"
      pv_itotal:
        value_template: '{{ states.sensor.inverter_goodwe.attributes.itotal }}'
        unit_of_measurement: '€'
        friendly_name: "PV income total"
      pv_excess:
        value_template: '{{ states.sensor.inverter_goodwe.attributes.pmeter }}'
        unit_of_measurement: 'W'
        friendly_name: "PV spare"
      # battery soc
      pv_soc:
        value_template: '{{ states.sensor.inverter_goodwe.attributes.soc }}'
        unit_of_measurement: '%'
        friendly_name: "Battery power"
  - platform: integration
    source: sensor.pv_eday
    name: "Solar Generated per day"
    unit_prefix: k
    unit_time: h
    round: 2

I’ve added a custom integration template to use one of the newly created sensors, but I can’t see this in the energy dashboard configuration (for adding solar production).

Any idea what I’m doing wrong in here?

Cheers,
Alexandru

Please post the attributes for the sensor which is not showing. (From developer tools)

You don’t need the integration integration. According to your unit of measurements, they already have the correct units. Also, you should use the new template format instead of the legacy format. Then you won’t have to use customize.yaml. Lastly, you’re using templates that are prone to startup errors.

Keep in mind, this configuration does NOT go in the sensor section. It goes in configuration.yaml.

template:
  - sensors:
    - unique_id: pv_temperature
      name: PV Temperature
      state: "{{ state_attr('inverter_goodwe', 'tempperature') }}"
      unit_of_measurement: ºC
      
    - unique_id: pv_eday
      name: PV energy day
      state: "{{ state_attr('inverter_goodwe', 'eday') }}"
      unit_of_measurement: kWh
      device_class: energy
      state_class: total_increasing
      
    - unique_id: pv_etotal
      state: "{{ state_attr('inverter_goodwe', 'etotal') }}"
      name: PV energy total
      unit_of_measurement: kWh
      device_class: energy
      
    - unique_id: pv_iday
      name: PV income day
      state: "{{ state_attr('inverter_goodwe', 'iday') }}"
      unit_of_measurement: €
      
    - unique_id: pv_itotal
      name: PV income total
      state: "{{ state_attr('inverter_goodwe', 'itotal') }}"
      unit_of_measurement: €
      
    - unique_id: pv_excess
      name: PV spare
      state: "{{ state_attr('inverter_goodwe', 'pmeter') }}"
      unit_of_measurement: W
      
    - unique_id: pv_soc
      name: Battery power
      state: "{{ state_attr('inverter_goodwe', 'soc') }}"
      unit_of_measurement: %

Hi petro,

I’ve updated the configuration for the sensors to use the new format (as suggested in documentation):

template:
    sensors:
      pv_temperature:
        value_template: '{{ states.sensor.inverter_goodwe.attributes.tempperature }}'
        unit_of_measurement: 'C'
        friendly_name: "PV Temperature"
      pv_eday:
        value_template: '{{ states.sensor.inverter_goodwe.attributes.eday }}'
        unit_of_measurement: 'kWh'
        friendly_name: "PV energy day"
      pv_etotal:
        value_template: '{{ states.sensor.inverter_goodwe.attributes.etotal }}'
        unit_of_measurement: 'kWh'
        friendly_name: "PV energy total"
      pv_iday:
        value_template: '{{ states.sensor.inverter_goodwe.attributes.iday }}'
        unit_of_measurement: '€'
        friendly_name: "PV income day"
      pv_itotal:
        value_template: '{{ states.sensor.inverter_goodwe.attributes.itotal }}'
        unit_of_measurement: '€'
        friendly_name: "PV income total"
      pv_excess:
        value_template: '{{ states.sensor.inverter_goodwe.attributes.pmeter }}'
        unit_of_measurement: 'W'
        friendly_name: "PV spare"
      # battery soc
      pv_soc:
        value_template: '{{ states.sensor.inverter_goodwe.attributes.soc }}'
        unit_of_measurement: '%'
        friendly_name: "Battery power"

I can see the new entities with the correct values in dev tools:

But again, from the energy dashboard configuration, if I try to add the new entity sensor.pv_eday I can’t find it

These entities are all I can see when I search for the new created entity

You didn’t add state_class or device_class from my example, both are needed.

As I’m not allowed to more than 1 embedded item per post, here are the entities that I can see it in the energy dashboard configuration when I try to add the entity sensor.pv_eday as solar production:

Screenshot 2022-02-02 at 15.14.53

The energy produced sensors, are coming from the slimmelezer integration

Yes, see my last response. You didn’t add the necessary items. Look at my post again and ensure you include state_class and device_class for each power entitythat you want to show up in the energy panel.

You also didn’t change to the new template configuration… What exactly did you change?

Currently I have this in configuration.yaml. It says that it’s a valid configuration, but after start I get an error

template:
  - sensors:
    - unique_id: pv_temperature
      name: PV Temperature
      state: "{{ state_attr('inverter_goodwe', 'tempperature') }}"
      unit_of_measurement: ºC
      
    - unique_id: pv_eday
      name: PV energy day
      state: "{{ state_attr('inverter_goodwe', 'eday') }}"
      unit_of_measurement: kWh
      device_class: energy
      state_class: total_increasing
      
    - unique_id: pv_etotal
      state: "{{ state_attr('inverter_goodwe', 'etotal') }}"
      name: PV energy total
      unit_of_measurement: kWh
      device_class: energy
      
    - unique_id: pv_iday
      name: PV income day
      state: "{{ state_attr('inverter_goodwe', 'iday') }}"
      unit_of_measurement: €
      
    - unique_id: pv_itotal
      name: PV income total
      state: "{{ state_attr('inverter_goodwe', 'itotal') }}"
      unit_of_measurement: €
      
    - unique_id: pv_excess
      name: PV spare
      state: "{{ state_attr('inverter_goodwe', 'pmeter') }}"
      unit_of_measurement: W
      
    - unique_id: pv_soc
      name: Battery power
      state: "{{ state_attr('inverter_goodwe', 'soc') }}"
      unit_of_measurement: %

And I get this error:

Invalid config for [template]: expected dictionary for dictionary value @ data['sensors']. Got [OrderedDict([('unique_id', 'pv_temperature'), ('name', 'PV Temperature'), ('state', "{{ state_attr('inverter_elcasadelpapelinverter', 'tempperature') }}"), ('unit_of_measurement', 'ºC')]), OrderedDict([('unique_id', 'pv_eday'), ('name', 'PV energy day'), ('state', "{{ state_attr('inverter_elcasadelpapelinverter', 'eday') }}"), ('unit_of_measurement', 'kWh'), ('device_class', 'energy'), ('state_class', 'total_increasing')]), OrderedDict([('unique_id', 'pv_etotal'), ('state', "{{ state_attr('i.... (See /config/configuration.yaml, line 18).

Typo on my part

template:
  - sensor:
    - unique_id: pv_temperature
      name: PV Temperature
      state: "{{ state_attr('inverter_goodwe', 'tempperature') }}"
      unit_of_measurement: ºC
      
    - unique_id: pv_eday
      name: PV energy day
      state: "{{ state_attr('inverter_goodwe', 'eday') }}"
      unit_of_measurement: kWh
      device_class: energy
      state_class: total_increasing
      
    - unique_id: pv_etotal
      state: "{{ state_attr('inverter_goodwe', 'etotal') }}"
      name: PV energy total
      unit_of_measurement: kWh
      device_class: energy
      
    - unique_id: pv_iday
      name: PV income day
      state: "{{ state_attr('inverter_goodwe', 'iday') }}"
      unit_of_measurement: €
      
    - unique_id: pv_itotal
      name: PV income total
      state: "{{ state_attr('inverter_goodwe', 'itotal') }}"
      unit_of_measurement: €
      
    - unique_id: pv_excess
      name: PV spare
      state: "{{ state_attr('inverter_goodwe', 'pmeter') }}"
      unit_of_measurement: W
      
    - unique_id: pv_soc
      name: Battery power
      state: "{{ state_attr('inverter_goodwe', 'soc') }}"
      unit_of_measurement: %

That works! Thanks

2022-02-03 12:16:56 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('TypeError: 'int' object is not iterable') while processing template 'Template("{{ states.sensor.shelly_em_c45bbe77e8ff_meter_power_0.state | float(0) | min(0) | multiply(-1) }}")' for attribute '_attr_native_value' in entity 'sensor.power_export'

Got this error since i’ve updated to 2022.2

What is going on with the state of sensor.shelly_em_c45bbe77e8ff_meter_power_0 ?

Just updating as normally


Was it just a one off error on start or are you seeing lots?

Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 407, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1814, in _render_with_context
return template.render(**kwargs)
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 1291, in render
self.environment.handle_exception()
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 925, in handle_exception
raise rewrite_traceback_stack(source=source)
File "<template>", line 1, in top-level template code
File "/usr/local/lib/python3.9/site-packages/jinja2/filters.py", line 525, in do_max
return _min_or_max(environment, value, max, case_sensitive, attribute)
File "/usr/local/lib/python3.9/site-packages/jinja2/filters.py", line 475, in _min_or_max
it = iter(value)
TypeError: 'int' object is not iterable
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 523, in async_render_to_info
render_info._result = self.async_render(variables, strict=strict, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 409, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: TypeError: 'int' object is not iterable

Yeah I can reproduce this. Something has changed with the min function and it is no longer happy that it has a list to iterate over. I am not sure that I have a solution immediately to hand. Someone who is better at templating than I might have a solution but I will keep googling for a result…

Ahh sh*t, there goes my energy dashboard, haha.

Try this:

{{ [states.sensor.shelly_em_c45bbe77e8ff_meter_power_0.state | float(0) | multiply(-1), 0] | min }}