Fronius inverter integration

Could you also post a view of the history graph of the source sensors for those two utility sensors? That might give us a clue if there are some bad readings.

Yes, my inverter is setup for logging during night-time hours

##
## Energy Calculations
###################################################
utility_meter:
  ### daily data ###
  # calculate daily energy used
  house_energy_day:
    source: sensor.energy_used_hour
    cycle: daily

  # calculate daily energy consumed from grid
  grid_consumed_energy_day:
    source: sensor.fronius_smartmeter_energy_ac_consumed
    cycle: daily

  # calculate daily energy sold to grid
  grid_sold_energy_day:
    source: sensor.fronius_smartmeter_energy_ac_sold
    cycle: daily

  ### monthly data ###
  # calculate monthly energy used
  house_energy_month:
    source: sensor.energy_used_hour
    cycle: monthly

  # calculate monthly energy consumed from grid
  grid_consumed_energy_month:
    source: sensor.fronius_smartmeter_energy_ac_consumed
    cycle: monthly

  # calculate monthly energy sold to grid
  grid_sold_energy_month:
    source: sensor.fronius_smartmeter_energy_ac_sold
    cycle: monthly

  # calculate monthly panel production since that is not available from the inverter
  fronius_month_energy:
    source: sensor.energy_panel_hour
    cycle: monthly

  ### yearly data ###
  # calculate yearly energy used
  house_energy_year:
    source: sensor.energy_used_hour
    cycle: yearly

  # calculate yearly energy consumed from grid
  grid_consumed_energy_year:
    source: sensor.fronius_smartmeter_energy_ac_consumed
    cycle: yearly

  # calculate yearly energy sold to grid
  grid_sold_energy_year:
    source: sensor.fronius_smartmeter_energy_ac_sold
    cycle: yearly

image

image

image

image

It looks like you are getting a lot of zero readings. That might be what is tripping the utility meter.

This is how the two sensors for sold energy looks like for me (and the consumed energy looks similar):
image
image

On the other hand I get zero readings for the fronius_day_energy sensor. But since that value is used as-is and not fed into a utility sensor it doesn’t cause any issues.

image

I wonder why the inverter is returning bad data…I will see if I can figure out why.

Could you try to locate the following piece of code in sensor.py and comment out the two lines at the end that I have commented out? I’m curious if that makes a difference in this case because then the integration will return “None” and not “0” so the utility sensor might discard it and use the next value when it is a “valid” value.

        # convert and round the result
        if state is not None:
            if self._convert_units == "energy":
                _LOGGER.debug("Converting energy ({}) to {}".format(state, self._units))
                if self._units == "MWh":
                    self._state = round(state / 1000000, 2)
                elif self._units == "kWh":
                    self._state = round(state / 1000, 2)
                else:
                    self._state = round(state, 2)
            elif self._convert_units == "power":
                _LOGGER.debug("Converting power ({}) to {}".format(state, self._units))
                if self._units == "MW":
                    self._state = round(state / 1000000, 2)
                elif self._units == "kW":
                    self._state = round(state / 1000, 2)
                else:
                    self._state = round(state, 2)
            elif self._json_key == "DAY_ENERGY":
                # day energy always gets converted to kWh
                _LOGGER.debug("Converting day energy to kWh ({})".format(state))
                self._state = round(state / 1000, 2)
            else:
                _LOGGER.debug("Rounding ({}) to two decimals".format(state))
                self._state = round(state, 2)
# comment out these two lines
#        else:
#            self._state = 0
        _LOGGER.debug("State converted ({})".format(self._state))

Below is the extract of my sensor.py.

        # convert and round the result
        if state is not None:
            if self._convert_units == "energy":
                _LOGGER.debug("Converting energy ({}) to {}".format(state, self._units))
                if self._units == "MWh":
                    self._state = round(state / 1000000, 2)
                elif self._units == "kWh":
                    self._state = round(state / 1000, 2)
                else:
                    self._state = round(state, 2)
            elif self._convert_units == "power":
                _LOGGER.debug("Converting power ({}) to {}".format(state, self._units))
                if self._units == "MW":
                    self._state = round(state / 1000000, 2)
                elif self._units == "kW":
                    self._state = round(state / 1000, 2)
                else:
                    self._state = round(state, 2)
            else:
                _LOGGER.debug("Rounding ({}) to two decimals".format(state))
                self._state = round(state, 2)
        else:
            self._state = 0
        _LOGGER.debug("State converted ({})".format(self._state))

I noticed it is missing the below extract. will this be the problem?

            elif self._json_key == "DAY_ENERGY":
                # day energy always gets converted to kWh
                _LOGGER.debug("Converting day energy to kWh ({})".format(state))
                self._state = round(state / 1000, 2)

No, that part is only to correct the conversion of day energy so that it is always converted to kWh regardless of what you have specified in “units”.

But you should have that part too…but I think he released a new version a few hours ago after I submitted a pull-request to add this. So update to the latest version first and then try to comment out those lines.

OK. I’ve just updated the file to the latest version.

Just back to the graphs.

They are not really zero values

image

image

image

The “dips” in the graphs that goes to zero are zero. Otherwise the graphs would never have “spikes” that goes down to zero. I have also noticed on my day energy that lovelace displays zero sometimes which confirms that it actually is zero. But since that value is never used for any “calculation/summary” in any other sensor it doesn’t matter.

I have no knowledge of the internals of the utility meter. But I can imagine that it will have issues if the source sensor sometimes returns zero.
That is all I can come up with that could cause the behavior you see.

There is another thread I have been checking that involves similar issues…and the issues I mentioned above.

Is it possible that the reading shows zero when the inverter’s connection drops out from the wireless connection?

Could be. Although I have a wired connection to mine so there should be no connection issues for me. And afaik the integration should log an error if it cannot connect to the inverter or if it returns no data. But I have not been able to see any such issues in my log.

So something is causing the inverter to return “bad” data sometimes. Or this integration has some issues parsing out the data correctly. We just need to figure out why it happens :stuck_out_tongue:

OK. My inverter does have a weak wireless reception. I will relocate my wireless AP closer to the inverter for a better signal strength and see how it goes.

It’s just doing my head in having to see the correct figures in the morning thinking everything works fine and in the arvo it plays up again.

BTW commenting out the two lines did not make a difference so far.

Yup, it one of those errors that really annoys you :expressionless:

Try leaving it this way until tomorrow. It probably needs a “reset” of the utility meter, which happens on daily basis, before we can see if it still acts weird.

What you could check until then is to see how the history graph looks like.

Alright. I will keep you posted again tomorrow. Appreciate your help so far. Thanks!

1 Like

Do you know how can I get ?

  • Daily Energy Sent to Newtork (KWh)
  • Daily Energy Get from the Network (KWh)
  • Daily consumed Energy (KWh)

I think we might have hit the jackpot! :slightly_smiling_face: Been monitoring it all day and the figures seems correct now.

image
image
image
image
image

Also, I’ve been trying to setup a monthly and yearly stats but i don’t think the figures are populating correctly.

Below are extract of my cards.

cards:
  - entity: sensor.house_energy_month
    scale: 22px
    style: |
      ha-card {
        background: {% if states('ensor.house_energy_month') | float < states('sensor.grid_sold_energy_month') | float %} green {% else %} darkred {% endif %};
      }
    title: Total Consumption
    type: 'custom:bignumber-card'
  - entity: sensor.fronius_month_energy
    scale: 22px
    style: |
      ha-card {
        background: {% if states('sensor.fronius_month_energy') | float > states('sensor.house_energy_month') | float %} green {% else %} darkred {% endif %};
      }
    title: Panel Generation
    type: 'custom:bignumber-card'
  - entity: sensor.grid_consumed_energy_month
    scale: 22px
    style: |
      ha-card {
        background: {% if states('sensor.fronius_grid_usage') | float < 0 %} green {% else %} darkred {% endif %};
      }
    title: Energy Bought
    type: 'custom:bignumber-card'
  - entity: sensor.grid_sold_energy_month
    scale: 22px
    style: |
      ha-card {
        background: {% if states('sensor.grid_sold_energy_month') | float > states('sensor.grid_consumed_energy_month') | float %} green {% else %} darkred {% endif %};
      }
    title: Energy Sold
    type: 'custom:bignumber-card'
title: Monthly Stats
type: horizontal-stack
cards:
  - entity: sensor.house_energy_year
    scale: 22px
    style: |
      ha-card {
        background: {% if states('sensor.house_energy_year') | float < states('sensor.grid_sold_energy_year') | float %} green {% else %} darkred {% endif %};
      }
    title: Total Consumption
    type: 'custom:bignumber-card'
  - entity: sensor.fronius_year_energy
    scale: 22px
    style: |
      ha-card {
        background: {% if states('sensor.fronius_year_energy') | float > states('sensor.house_energy_year') | float %} green {% else %} darkred {% endif %};
      }
    title: Panel Generation
    type: 'custom:bignumber-card'
  - entity: sensor.grid_consumed_energy_year
    scale: 22px
    style: |
      ha-card {
        background: {% if states('sensor.fronius_grid_usage') | float < 0 %} green {% else %} darkred {% endif %};
      }
    title: Energy Bought
    type: 'custom:bignumber-card'
  - entity: sensor.grid_sold_energy_year
    scale: 22px
    style: |
      ha-card {
        background: {% if states('sensor.grid_sold_energy_year') | float > states('sensor.grid_consumed_energy_year') | float %} green {% else %} darkred {% endif %};
      }
    title: Energy Sold
    type: 'custom:bignumber-card'
title: Yearly Stats
type: horizontal-stack

I suspected that this was the reason. The utility meter thinks that since one reading was 0 and the next one “normal” it will apply that diff, which is why your readings started going sky high. But if the state is “None” it will just discard the value and use the next one that comes in.

The change to returning 0 instead on None was done because in the case when logging is disabled at night (which is the default), or when you restart HA when sun is down, there will be sensors with unknown state until sun comes back up again. The Fronius API returns Null value, or omits some readings, when the inverter is not producing power. So it’s tricky to know which way is the right one to choose.

Ideally I would like to understand when/why we get Null/Zero values for some sensors when they really should have a “valid” value. It might have something to do with how the requests are sent towards the inverter. Or it is the inverter that sometimes actually returns Null. I started running my integration with some debug logs but so far I have not found anything. Yet I can see that I am getting 0 sometimes for some sensors when I look at the history graph.

So if you could continue running your HA with the current setting and also try to observe how some sensors looks like when sun is down that would be good feedback.

I think you are a victim of the same problem here…that the utility sensors have gone crazy due to the 0 inputs…but now you are stuck with it until the end of the month/year when they are reset. If you check the history for the sensors with crazy numbers you will probably see big jumps instead of a smooth curve.

To fix that you should be able to change the “restore state” for those sensors. But it’s a bit of a hack and it’s been a while so i’m not sure I can give you instructions for how to fix it.
But in pinciple you need to stop HA and then go into the .storage folder inside your config and locate the file core.restore_state. Inside that file is every state that HA will restore upon startup. So if you search for the utility sensors you need to correct you can change their values. But i’m not sure if it will work if you set them to 0.

Ideally we should be able to just tell the utility sensor to reset it’s value. And there is a service call to do so. But unfortunately it only works if the utility sensor has a “tariff”, which these do not have. But you could also try to add a “default” tariff to them, restart HA, and then try to call the reset service for those utility sensors that have bad values. This might be a better/easier way to try to fix those sensors than using the restore state.

Do you have a smart meter? If so you can look at the package file I posted recently. It contains sensor setups for getting those values.

I have a smart meter and I already configured sensors with integration/utility_meter cycles daily/monthly/yearly…

I was wondering if there’s a way to get those info from the fronius apis…

Doing an integral of the power to get the energy and sum it up daily can be different from the values shown in the website solarweb… moreover I see that the daily unity meter doesn’t consider the day from 0 to 24h, but seems to consider another hour as cutoff hour…

No, it is not possible to get those values directly from the API.

You can only get accumulated energy values for to/from grid. So if you use them with the utility sensor it will be correct because it will sum up the difference between each call to the API. There is no need to do any calculations based on power readings so you can skip the integration sensors for that.

For house load you cannot get a direct energy reading from the API. But you can use the energy produced by the panels and the energy to/from grid to calculate how much energy the house has used. So again, there is no need to do calculations using power as input, which will always be a “guess” compared to getting the energy values from the inverter/meter itself.