Skip to content

Commit

Permalink
Use absolute values to determine charge /discharge power from battery…
Browse files Browse the repository at this point in the history
… power raw to also support newer firmware (where the battery power raw is signed)
  • Loading branch information
mkaiser committed Jan 3, 2025
1 parent f1e01ed commit d6d79c4
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions modbus_sungrow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1277,14 +1277,17 @@ modbus:
scale: 0.1
scan_interval: 10

# always positive battery power
# old firmware ( before october 2024): always positive battery power
# use binary_sensor.battery_charging | discharging to retrieve the direction of the energy flow
# new firmware: positive if charging and negative if discharging
# with new firmware installed we could use this raw value directly as signed battery power
# to support both firmwares we use the template sensor "signed battery power"
- name: Battery power raw
unique_id: sg_battery_power_raw
device_address: !secret sungrow_modbus_slave
address: 13021 # reg 13022
input_type: input
data_type: int16
data_type: int16 #updated to signed int, see issue #406
precision: 0
unit_of_measurement: W
device_class: power
Expand Down Expand Up @@ -1750,7 +1753,7 @@ template:
{# EMS forced mode #}
{% if (states('sensor.battery_forced_charge_discharge_cmd') == "Forced charge") %}
{# in mode Forced charge #}
{% if (states('sensor.battery_power_raw')|int > 0 ) %}
{% if (states('sensor.battery_power_raw')|int > 0 ) %}
{# power flow from/to battery #}
on
{% else %}
Expand Down Expand Up @@ -1825,7 +1828,7 @@ template:
{% endif %}
{% else %}
{# not in EMS forced mode, assuming self consumption mode #}
{% if ( ( states('sensor.total_dc_power')|int < states('sensor.load_power')|int ) ) and states('sensor.battery_power_raw')|int > 0 %}
{% if ( ( states('sensor.total_dc_power')|int < states('sensor.load_power')|int ) ) and states('sensor.battery_power_raw')|int > 0 %}
{# more power consumed than generated and some battery power --> assuming battery discharging #}
on
{% else %}
Expand Down Expand Up @@ -2222,7 +2225,8 @@ template:
Unknown - should not see me! code: {{ (states('sensor.ems_mode_selection_raw') |int) }}
{% endif %}
- name: Signed battery power # positive if charging and negative if discharging
# positive if charging and negative if discharging
- name: Signed battery power
unique_id: sg_signed_battery_power
unit_of_measurement: W
device_class: power
Expand All @@ -2235,14 +2239,15 @@ template:
}}
state: >-
{% if is_state('binary_sensor.battery_charging', 'on') %}
{{ (states('sensor.battery_power_raw') | float)}}
{{ (states('sensor.battery_power_raw') |float |abs)}}
{% elif is_state('binary_sensor.battery_discharging', 'on') %}
{{ (states('sensor.battery_power_raw') | float * -1)}}
{{ (states('sensor.battery_power_raw') |float |abs * -1)}}
{% else %}
0
{% endif %}
- name: Battery charging power # positive if charging else zero
# positive if charging else zero
- name: Battery charging power
unique_id: sg_battery_charging_power
unit_of_measurement: W
device_class: power
Expand All @@ -2254,7 +2259,7 @@ template:
}}
state: >-
{% if is_state('binary_sensor.battery_charging', 'on') %}
{{ states('sensor.battery_power_raw') }}
{{ states('sensor.battery_power_raw')|int |abs }}
{% else %}
0
{% endif %}
Expand All @@ -2272,7 +2277,7 @@ template:
}}
state: >-
{% if is_state('binary_sensor.battery_discharging', 'on') %}
{{ states('sensor.battery_power_raw') }}
{{ states('sensor.battery_power_raw')|int |abs }}
{% else %}
0
{% endif %}
Expand Down

0 comments on commit d6d79c4

Please sign in to comment.