feat(deye): fixed total enegery export
All checks were successful
Build / build (push) Successful in 3m12s
All checks were successful
Build / build (push) Successful in 3m12s
This commit is contained in:
parent
c5e58816db
commit
107e7d0b5a
1 changed files with 44 additions and 8 deletions
|
@ -12,7 +12,7 @@ defmodule WeewxProxy.Modbus.Deye do
|
|||
@impl true
|
||||
def init(_opts) do
|
||||
_ = Logger.info("Initializing handler")
|
||||
{:ok, 0}
|
||||
{:ok, %{solar_energy_day: 0, solar_energy_total1: 0, solar_energy_total2: 0}}
|
||||
end
|
||||
|
||||
@impl true
|
||||
|
@ -61,7 +61,7 @@ defmodule WeewxProxy.Modbus.Deye do
|
|||
def handle_message(["bitshake", "tele", "smartmeter", topic], publish, state) do
|
||||
current_time = Time.utc_now()
|
||||
current_hour = current_time.hour
|
||||
new_state = if current_hour == 0, do: 0, else: state
|
||||
new_state = if current_hour == 0, do: %{state | solar_energy_day: 0}, else: state
|
||||
|
||||
if topic == "SENSOR" do
|
||||
{:ok, meter_data} = Jason.decode(publish)
|
||||
|
@ -106,7 +106,8 @@ defmodule WeewxProxy.Modbus.Deye do
|
|||
# Helper
|
||||
|
||||
@spec parse_message(String.t(), String.t()) :: float() | nil
|
||||
defp parse_message(topic, message) when topic in ["deye/day_energy", "deye/ac/active_power"] do
|
||||
defp parse_message(topic, message)
|
||||
when topic in ["deye/day_energy", "deye/ac/active_power", "deye/1/total_energy", "deye/2/total_energy"] do
|
||||
Utils.parse_float(message)
|
||||
end
|
||||
|
||||
|
@ -115,8 +116,8 @@ defmodule WeewxProxy.Modbus.Deye do
|
|||
@spec handle_reading(String.t(), float() | nil, {:day | :active, float()}) :: {:day | :active, float()} | nil
|
||||
defp handle_reading(_topic, nil, state), do: state
|
||||
|
||||
defp handle_reading("deye/day_energy", reading, old_reading) do
|
||||
if reading >= old_reading do
|
||||
defp handle_reading("deye/day_energy", reading, state) do
|
||||
if reading >= state.solar_energy_day do
|
||||
timestamp = DateTime.utc_now() |> DateTime.to_unix()
|
||||
|
||||
data = %{
|
||||
|
@ -127,10 +128,9 @@ defmodule WeewxProxy.Modbus.Deye do
|
|||
:ok = Publisher.publish("weewx/ingest_si", data)
|
||||
:ok = Publisher.publish_value_map("hadata/deye", data)
|
||||
|
||||
reading
|
||||
%{state | solar_energy_day: reading}
|
||||
else
|
||||
_ = Logger.warning("Reading unexpectedly decreased from #{old_reading} to #{reading}")
|
||||
old_reading
|
||||
state
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -147,4 +147,40 @@ defmodule WeewxProxy.Modbus.Deye do
|
|||
|
||||
state
|
||||
end
|
||||
|
||||
defp handle_reading("deye/1/total_energy", reading, state) do
|
||||
if reading > 0.1 and reading >= state.solar_energy_total1 do
|
||||
timestamp = DateTime.utc_now() |> DateTime.to_unix()
|
||||
|
||||
data = %{
|
||||
dateTime: timestamp,
|
||||
solarEnergyTotal1: reading
|
||||
}
|
||||
|
||||
# :ok = Publisher.publish("weewx/ingest_si", data)
|
||||
:ok = Publisher.publish_value_map("hadata/deye", data)
|
||||
|
||||
%{state | solar_energy_total1: reading}
|
||||
else
|
||||
state
|
||||
end
|
||||
end
|
||||
|
||||
defp handle_reading("deye/2/total_energy", reading, state) do
|
||||
if reading > 0.1 and reading >= state.solar_energy_total2 do
|
||||
timestamp = DateTime.utc_now() |> DateTime.to_unix()
|
||||
|
||||
data = %{
|
||||
dateTime: timestamp,
|
||||
solarEnergyTotal2: reading
|
||||
}
|
||||
|
||||
# :ok = Publisher.publish("weewx/ingest_si", data)
|
||||
:ok = Publisher.publish_value_map("hadata/deye", data)
|
||||
|
||||
%{state | solar_energy_total2: reading}
|
||||
else
|
||||
state
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue