# Calculate Solar Panel ROI - accumulating sensor(?)

I am trying to bend my head around how to calculate the return of investment for our solar panels.

Everything is working nicely with regards for the net export via the Energy dashboard, but I fail with the self consumed energy.

I have a total increasing template for the hourly kWh, the challenge is that I also have a hourly cost, so at the end of the hour I have to capture the consumption and multiply that with the current cost in turn to be added to the total increasing monitary sensor.
What is the best approach to extract the last hour of consumption, and when to calculate the cost, given that the tarif changes on the hour? At hh:55 or something like it?

sensor.solar_selfconsumed_power calculates the difference between produced power and power submitted to the grid [W]
sensor.solar_selfconsumed_energy uses the Riemann sum integral (left) to convert to kWh

``````  - platform: template
sensors:
solar_selfconsumed_power:
friendly_name: Solpanel EgenfĂ¶rbrukad Kraft
value_template: "{{ (states('sensor.sma_solpaneler_grid_power')|float(0)) - ((states('sensor.power_produced')|float(0))*1000) }}"
unit_of_measurement: "W"

- platform: integration
name: Solpanel EgenfĂ¶rbrukad Energi
unique_id: solar_selfconsumed_energy
unit_prefix: k
unit_time: h
method: left
source: sensor.solar_selfconsumed_power
``````

sensor.elkostnad is the hourly rate + fixed transfer cost from the grid, updated on the hour
sensor.solcell_besparing has two shortcomings, I need a delta sensor rather than the total_increasing, and it will be updated at the same time as the cost sensor is updated, giving an unpredicable outcome.

``````  - sensor:
state: "{{ (states('sensor.nordpool_kwh_se3')|float(0)) + (states('sensor.elkostnad_natagare')|float(0)) }}"
availability: "{{ states('sensor.nordpool_kwh_se3') not in ['unavailable', 'unknown', 'none'] }}"
unit_of_measurement: "SEK/kWh"
device_class: monetary
- sensor:
- name: "Solcell Besparing"
state: "{{ states('sensor.solar_selfconsumed_energy')|float(0) * states('sensor.elkostnad')|float(0) }}"
availability: "{{ states('sensor.nordpool_kwh_se3') not in ['unavailable', 'unknown', 'none'] }}"
unit_of_measurement: "SEK"
device_class: monetary
state_class: total_increasing
``````

Hej.

Did you ever find a good solution for this problem? Looking for something like this as well.

I got something working, but I must confess that I do not recall the exact nature

config file:

``````  - sensor:
- name: "Solcell ErsĂ¤ttning Produktion LeverantĂ¶r"
state: "{{ states('sensor.nordpool_kwh_se3')|float(0)+0.05 }}"
availability: "{{ states('sensor.nordpool_kwh_se3') not in ['unavailable', 'unknown', 'none'] }}"
unit_of_measurement: "SEK/kWh"
device_class: monetary

- sensor:
- name: "Solcell ErsĂ¤ttning Produktion NĂ¤tĂ¤gare"
state: >
{% if states('input_boolean.el_laglast') is true %}
{{ 0.0675  }}
{% else %}
{{ 0.0775 }}
{% endif %}
availability: "{{ states('sensor.energy_produced') not in ['unavailable', 'unknown', 'none'] }}"
unit_of_measurement: "SEK/kWh"
device_class: monetary

- sensor:
- name: "Solcell ErsĂ¤ttning Produktion"
state: "{{ states('sensor.solcell_ersattning_produktion_natagare')|float(0) + states('sensor.solcell_ersattning_produktion_leverantor')|float(0)}}"
unit_of_measurement: "SEK/kWh"
device_class: monetary

- sensor:
- name: "Solcell Besparing"
unique_id: solcell_besparing
state: "{{ states('input_number.solpanel_besparing')|float(2) }}"
availability: "{{ states('sensor.nordpool_kwh_se3') not in ['unavailable', 'unknown', 'none'] }}"
unit_of_measurement: "SEK"
device_class: monetary
state_class: total_increasing
``````

helpers:

end result:

lovelace:

``````type: custom:bar-card
entities:
- entity: sensor.solcell_besparing
decimal: '2'
max: '280000'
severity:
- from: '160000'
to: '180000'
color: green
icon: mdi:cash-multiple
target: '180000'
``````
1 Like