Skip to content

Commit

Permalink
Merge pull request #25 from nilrog/master
Browse files Browse the repository at this point in the history
Fixed handling of json data.
  • Loading branch information
safepay committed Apr 19, 2020
2 parents c13f580 + ac5ab9e commit 9ced175
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@


# Fronius Sensor for Home Assistant
This component simplifies the integration of a Fronius inverter and optional PowerFlow and SmartMeter:
* creates up to 24 individual sensors for easy display or use in automations
This component simplifies the integration of a Fronius inverter and optional PowerFlow/SmartMeter:
* creates up to 22 individual sensors for easy display or use in automations
* converts Wh to kWh
* rounds values to 2 decimal places
* converts daily, yearly and total energy data to kWh or MWh (user-configurable)
* optionally connects to PowerFlow devices for 3 additional sensors
* optionally connects to SmartMeter devices for 10 additional sensors
* optionally converts PowerFlow units to W, kW or MW
* optionally sums values if you have more than one inverter

If you have a SmartMeter installed this component:
* optionally connects to PowerFlow API for 5 additional sensors
* optionally connects to SmartMeter API for 8 additional sensors
* optionally converts PowerFlow units to W, kW or MW

* compatible with the custom [Power Wheel Card](https://github.com/gurbyz/power-wheel-card/tree/master) if using PowerFlow

### URL's Utilised
Expand Down Expand Up @@ -63,7 +66,7 @@ sensor:
```

```yaml
# Example configuration.yaml entry where you have a PowerFlow device:
# Example configuration.yaml entry where you have a SmartMeter device and add PowerFlow sensors:
sensor:
- platform: fronius_inverter
ip_address: LOCAL_IP_FOR_FRONIUS
Expand All @@ -72,7 +75,7 @@ sensor:
```

```yaml
# Example configuration.yaml entry where you have a SmartMeter device:
# Example configuration.yaml entry where you have a SmartMeter device and add SmartMeter sensors:
sensor:
- platform: fronius_inverter
ip_address: LOCAL_IP_FOR_FRONIUS
Expand All @@ -85,15 +88,14 @@ variable | required | type | default | description
-------- | -------- | ---- | ------- | -----------
``ip_address`` | yes | string | | The local IP address of your Fronius Inverter.
``name`` | no | string | ``Fronius`` | The preferred name of your Fronius Inverter.
``powerflow`` | no | boolean | ``False`` | Set to ``True`` if you have a PowerFlow meter (SmartMeter) to add ``grid_usage``, ``house_load`` and ``panel_status`` sensors.
``smartmeter`` | no | boolean | ``False`` | Set to ``True`` if you have a SmartMeter to add sensors for grid AC current/voltage and total energy sold/consumed.
``powerflow`` | no | boolean | ``False`` | Set to ``True`` if you have a PowerFlow meter (SmartMeter) to add ``grid_usage``, ``house_load``, ``panel_status``, ``rel_autonomy`` and ``rel_selfconsumption`` sensors.
``smartmeter`` | no | boolean | ``False`` | Set to ``True`` if you have a SmartMeter to add ``smartmeter_current_ac_phase_one``, ``smartmeter_current_ac_phase_two``, ``smartmeter_current_ac_phase_three``, ``smartmeter_voltage_ac_phase_one``, ``smartmeter_voltage_ac_phase_two``, ``smartmeter_voltage_ac_phase_three``, ``smartmeter_energy_ac_consumed`` and ``smartmeter_energy_ac_sold`` sensors.
``smartmeter_device_id`` | no | string | ``0`` | The Device ID of your Fronius SmartMeter.
``units`` | no | string | ``MWh`` | The preferred units for Year and Total Energy from ``Wh, kWh, MWh``.
``power_units`` | no | string | ``W`` | The preferred PowerFlow units from ``W, kW, MW``.
``device_id`` | no | string | ``1`` | The Device ID of your Fronius Inverter.
``scope`` | no | string | ``Device`` | Set to ``System`` if you have multiple inverters. This will return ``ac_power, daily_energy, year_energy`` and, ``total_energy`` only. Case-sensitive.
``scan_interval`` | no | integer | ``60`` | Minimum configurable number of seconds between polls.
``monitored_conditions`` | no | list | All | List of monitored conditions from: ``ac_power``, ``ac_current``, ``ac_voltage``, ``ac_frequency``, ``dc_current``, ``dc_energy``, ``daily_energy``, ``year_energy``, ``total_energy``
``monitored_conditions`` | no | list | All | List of monitored conditions from: ``ac_power``, ``ac_current``, ``ac_voltage``, ``ac_frequency``, ``dc_current``, ``dc_energy``, ``daily_energy``, ``year_energy``, ``total_energy``, ``grid_usage``, ``house_load``, ``panel_status``, ``rel_autonomy``, ``rel_selfconsumption``, ``smartmeter_current_ac_phase_one``, ``smartmeter_current_ac_phase_two``, ``smartmeter_current_ac_phase_three``, ``smartmeter_voltage_ac_phase_one``, ``smartmeter_voltage_ac_phase_two``, ``smartmeter_voltage_ac_phase_three``, ``smartmeter_energy_ac_consumed``, ``smartmeter_energy_ac_sold``


### Custom Power Wheel Card (if using a Powerflow)
Expand Down
31 changes: 20 additions & 11 deletions custom_components/fronius_inverter/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,24 +233,30 @@ async def async_update(self, utcnow=None):
if self._data.latest_data and (self._json_key in self._data.latest_data):
_LOGGER.debug("Device: {}".format(self._device))
if self._device == 'inverter':
# Read data, if a value is 'null' convert it to 0
if self._scope == 'Device':
# Read data
state = self._data.latest_data[self._json_key]['Value']
if state is None:
_LOGGER.debug(">>>>> Converting {} from null to 0".format(self._json_key))
state = 0
elif self._scope == 'System':
for item in self._data.latest_data[self._json_key]['Values']:
state = state + self._data.latest_data[self._json_key]['Values'][item]
elif self._device == 'powerflow':
# Read data
if self._data.latest_data[self._json_key]:
state = self._data.latest_data[self._json_key]
elif self._device == 'smartmeter':
# Read data
if self._data.latest_data[self._json_key]:
state = self._data.latest_data[self._json_key]
value = self._data.latest_data[self._json_key]['Values'][item]
if value is None:
_LOGGER.debug(">>>>> Converting {} from null to 0".format(self._json_key))
value = 0
state = state + value
elif self._device == 'powerflow' or self._device == 'smartmeter':
# Read data directly, if it is 'null' convert it to 0
state = self._data.latest_data[self._json_key]
if state is None:
_LOGGER.debug(">>>>> Converting {} from null to 0".format(self._json_key))
state = 0
_LOGGER.debug("State: {}".format(state))

# convert and round the result
if state is not None:
_LOGGER.debug("Sensor: {}".format(self._json_key))
if self._convert_units == "energy":
_LOGGER.debug("Converting energy ({}) to {}".format(state, self._units))
if self._units == "MWh":
Expand All @@ -275,7 +281,8 @@ async def async_update(self, utcnow=None):
_LOGGER.debug("Rounding ({}) to two decimals".format(state))
self._state = round(state, 2)
else:
self._state = 0
_LOGGER.debug(">>>>> State is None for {} <<<<<".format(self._json_key))
_LOGGER.debug("Latest data: {}".format(self._data.latest_data))
_LOGGER.debug("State converted ({})".format(self._state))

def find_start_time(self, now):
Expand Down Expand Up @@ -313,6 +320,7 @@ def latest_data(self):
@Throttle(MIN_TIME_BETWEEN_UPDATES)
async def async_update(self):
"""Get the latest data from inverter."""
_LOGGER.debug("Requesting inverter data")
try:
result = requests.get(self._build_url(), timeout=10).json()
self._data = result['Body']['Data']
Expand Down Expand Up @@ -343,6 +351,7 @@ def latest_data(self):
@Throttle(MIN_TIME_BETWEEN_UPDATES)
async def async_update(self):
"""Get the latest data from inverter."""
_LOGGER.debug("Requesting powerflow data")
try:
result = requests.get(self._build_url(), timeout=10).json()
self._data = result['Body']['Data']['Site']
Expand Down

0 comments on commit 9ced175

Please sign in to comment.