Some help with custom sensors required

Hi,

I am using the SolarEdge Integration for getting information about the Solar Production “this year” and “this month”.

Since these values will be reset after the current month / current year, I want to use them somehow for creating a history…
How much energy was produced in the year 2021 how much energy was produced in each month of 2021…
It would be great, if the values could be stored in the database, for example:

April 2021: 1.500.000 kWh
May 2021: 1.550.000 kWh…


December 2021: 300 kWh

2021: 9.503.003 kWh

Same for 2022 and so on.

Also, I have created some custom sensors, for calculating the kWh per kWp of my installation.
This is the current configuration:

# kWh / kWp calculation
    solaredge_yearly_kwh_kwp:
      state_class: measurement
      device_class: energy
      friendly_name: "Yearly kWh/kWp"
      unit_of_measurement: "kWh/kWp"
      value_template: >-
        {{ (states('sensor.solaredge_energy_this_year_kwh')  | float / '13.26' | float) | float(default=0.0)|round(2) }}

    solaredge_monthly_kwh_kwp:
      state_class: measurement
      device_class: energy
      friendly_name: "Monthly kWh/kWp"
      unit_of_measurement: "kWh/kWp"
      value_template: >-
        {{ (states('sensor.solaredge_energy_this_month_kwh')  | float / '13.26' | float) | float(default=0.0)|round(2) }}

    solaredge_daily_kwh_kwp:
      state_class: measurement
      device_class: energy
      friendly_name: "Daily kWh/kWp"
      unit_of_measurement: "kWh/kWp"
      value_template: >-
        {{ (states('sensor.solaredge_energy_today_kwh') | float / '13.26' | float) | float(default=0.0)|round(2) }}

How can I make them “persistant” so that I can also get a history?

EDIT:
I’ve added this as Utility meter… but then I am getting an error…

utility_meter:
  yearly_energy_per_kwp:
    - source: sensor.solaredge_yearly_kwh_kwp
      name: Yearly kWh per kWp
      cycle: yearly 
  monthly_energy_per_kwp:
    - source: solaredge_monthly_kwh_kwp
      name: Monthly kWh per kWp
      cycle: monthly  
  yearly_energy: 
    - source: solaredge_energy_this_year_kwh
      name: Yearly kWh
      cycle: yearly
  monthly_energy:
    - source: solaredge_energy_this_month_kwh
      name: Monthly kWh
      cycle: monthly

the error:

nvalid config for [utility_meter]: expected a dictionary for dictionary value @ data['utility_meter']['monthly_energy']. Got [OrderedDict([('source', 'solaredge_energy_this_month_kwh'), ('name', 'Monthly kWh'), ('cycle', 'monthly')])]
expected a dictionary for dictionary value @ data['utility_meter']['monthly_energy_per_kwp']. Got [OrderedDict([('source', 'solaredge_monthly_kwh_kwp'), ('name', 'Monthly kWh per kWp'), ('cycle', 'monthly')])]
expected a dictionary for dictionary value @ data['utility_meter']['yearly_energy']. Got [OrderedDict([('source', 'solaredge_energy_this_year_kwh'), ('name', 'Yearly kWh'), ('cycle', 'yearly')])]
expected a dictionary for dictionary value @ data['utility_meter']['yearly_energy_per_kwp']. Got [OrderedDict([('source', 'sensor.solaredge_yearly_kwh_kwp'), ('name', 'Yearly kWh per kWp'), ('cycle', 'yearly')])]. (See /config/configuration.yaml, line 20). 
``

you forgot to add sensor. to your entities.

utility_meter:
  yearly_energy_per_kwp:
    - source: sensor.solaredge_yearly_kwh_kwp
      name: Yearly kWh per kWp
      cycle: yearly 
  monthly_energy_per_kwp:
    - source: sensor.solaredge_monthly_kwh_kwp
      name: Monthly kWh per kWp
      cycle: monthly  
  yearly_energy: 
    - source: sensor.solaredge_energy_this_year_kwh
      name: Yearly kWh
      cycle: yearly
  monthly_energy:
    - source: sensor.solaredge_energy_this_month_kwh
      name: Monthly kWh
      cycle: monthly
1 Like

thanks, that was stupid…
but … unfortunately, adding the sensor. doesn’t seem to solve the issue ?!

utility_meter:
  yearly_energy_per_kwp:
    - source: sensor.solaredge_yearly_kwh_kwp
      name: Yearly kWh per kWp
      cycle: yearly 
  monthly_energy_per_kwp:
    - source: sensor.solaredge_monthly_kwh_kwp
      name: Monthly kWh per kWp
      cycle: monthly  
  yearly_energy: 
    - source: sensor.solaredge_energy_this_year_kwh
      name: Yearly kWh
      cycle: yearly
  monthly_energy:
    - source: sensor.solaredge_energy_this_month_kwh
      name: Monthly kWh
      cycle: monthly
Invalid config for [utility_meter]: expected a dictionary for dictionary value @ data['utility_meter']['monthly_energy']. Got [OrderedDict([('source', 'sensor.solaredge_energy_this_month_kwh'), ('name', 'Monthly kWh'), ('cycle', 'monthly')])]
expected a dictionary for dictionary value @ data['utility_meter']['monthly_energy_per_kwp']. Got [OrderedDict([('source', 'sensor.solaredge_monthly_kwh_kwp'), ('name', 'Monthly kWh per kWp'), ('cycle', 'monthly')])]
expected a dictionary for dictionary value @ data['utility_meter']['yearly_energy']. Got [OrderedDict([('source', 'sensor.solaredge_energy_this_year_kwh'), ('name', 'Yearly kWh'), ('cycle', 'yearly')])]
expected a dictionary for dictionary value @ data['utility_meter']['yearly_energy_per_kwp']. Got [OrderedDict([('source', 'sensor.solaredge_yearly_kwh_kwp'), ('name', 'Yearly kWh per kWp'), ('cycle', 'yearly')])]. (See /config/configuration.yaml, line 20). 

ah sorry, i was too quick and overlooked something, the error should be about the hyphen.

utility_meter:
  yearly_energy_per_kwp:
    source: sensor.solaredge_yearly_kwh_kwp
    name: Yearly kWh per kWp
    cycle: yearly 
  monthly_energy_per_kwp:
    source: sensor.solaredge_monthly_kwh_kwp
    name: Monthly kWh per kWp
    cycle: monthly  
  yearly_energy: 
    source: sensor.solaredge_energy_this_year_kwh
    name: Yearly kWh
    cycle: yearly
  monthly_energy:
    source: sensor.solaredge_energy_this_month_kwh
    name: Monthly kWh
    cycle: monthly
1 Like

ah thanks :slight_smile:
That’s sometimes a bit unclear to me when a hyphen should be used - and when not… :frowning:

hm… ok thanks for your help :slight_smile:

The sensors are now available in the database, but it seems that they don’t get an update… o.O

do I need the state_class: measurement and device_type: energy on the sensors?
When I added this, it also throw an error…

Invalid config for [sensor.template]: [state_class] is an invalid option for [sensor.template]. Check: sensor.template->sensors->solaredge_yearly_kwh_kwp->state_class. (See ?, line ?). 

OK, probably - I’ve found the issue …

here’s the complete configuration:

utility_meter:
  yearly_energy_per_kwp:
    source: sensor.yearly_kwh_per_kwp
    name: Yearly kWh per kWp
    cycle: yearly 
  monthly_energy_per_kwp:
    source: sensor.monthly_kwh_per_kwp
    name: Monthly kWh per kWp
    cycle: monthly  
  yearly_energy: 
    source: sensor.yearly_kwh_production
    name: Yearly kWh
    cycle: yearly
  monthly_energy:
    source: sensor.monthly_kwh_production
    name: Monthly kWh
    cycle: monthly

template:
    sensors:
      - name: 'Yearly kWh per kWp'
        unique_id: 'yearly_kwh_per_kwp'
        state_class: 'measurement'
        device_class: 'energy'
        state: '{{sensor.solaredge_yearly_kwh_kwp}}'
        attributes:
          last_reset: '1970-01-01T00:00:00+00:00'
      
      - name: 'Monthly kWh per kWp'
        unique_id: 'monthly_kwh_per_kwp'
        state_class: 'measurement'
        device_class: 'energy'
        state: '{{sensor.solaredge_monthly_kwh_kwp}}'
        attributes:
          last_reset: '1970-01-01T00:00:00+00:00'
      
      - name: 'Yearly kwh production'
        unique_id: 'yearly_kwh_production'
        state_class: 'measurement'
        device_class: 'energy'
        state: '{{sensor.solaredge_energy_this_year_kwh}}'
        attributes:
          last_reset: '1970-01-01T00:00:00+00:00'

      - name: 'Monthly kwh production'
        unique_id: 'monthly_kwh_production'
        state_class: 'measurement'
        device_class: 'energy'
        state: '{{sensor.solaredge_energy_this_month_kwh}}'
        attributes:
          last_reset: '1970-01-01T00:00:00+00:00'

and additionally in a seperate file:

#############
# TEMPLATES #
#############
- platform: template
  sensors:

# kWh / kWp calculation
    solaredge_yearly_kwh_kwp:
      friendly_name: "Yearly kWh/kWp"
      unit_of_measurement: "kWh"
      value_template: >-
        {{ (states('sensor.solaredge_energy_this_year_kwh')  | float / '13.26' | float) | float(default=0.0)|round(2) }}

    solaredge_monthly_kwh_kwp:
      friendly_name: "Monthly kWh/kWp"
      unit_of_measurement: "kWh"
      value_template: >-
        {{ (states('sensor.solaredge_energy_this_month_kwh')  | float / '13.26' | float) | float(default=0.0)|round(2) }}
# This months energy in kWh
    solaredge_energy_this_month_kwh:
      friendly_name: solaredge (Energy this month) [kWh]
      unit_of_measurement: "kWh"
      value_template: "{{ states('sensor.solaredge_api_energy_this_month') | float(default=0) / 1000 | round(2) }}"
      icon_template: mdi:solar-power

# This years energy in kWh
    solaredge_energy_this_year_kwh:
      friendly_name: solaredge (Energy this year) [kWh]
      unit_of_measurement: "kWh"
      value_template: "{{ states('sensor.solaredge_api_energy_this_year') | float(default=0) / 1000 | round(2) }}"
      icon_template: mdi:solar-power

ok, one last thing…

shouldn’t be the state = the current state of the sensor - and SUM the sum the difference between the current and the last state?

this is taken from the statistics_short_term for the sensor: yearly_kwh (due to which reason ever, the system has not taken the name: ‘yearly_kwh_production’
grafik