feat(deye): fixed total enegery export
All checks were successful
Build / build (push) Successful in 3m12s

This commit is contained in:
Daniel Kempkens 2024-08-10 12:25:13 +02:00
parent c5e58816db
commit 107e7d0b5a
Signed by: daniel
SSH key fingerprint: SHA256:Ks/MyhQYcPRQiwMKLAKquWCdCPe3JXlb1WttgnAoSeM

View file

@ -12,7 +12,7 @@ defmodule WeewxProxy.Modbus.Deye do
@impl true @impl true
def init(_opts) do def init(_opts) do
_ = Logger.info("Initializing handler") _ = Logger.info("Initializing handler")
{:ok, 0} {:ok, %{solar_energy_day: 0, solar_energy_total1: 0, solar_energy_total2: 0}}
end end
@impl true @impl true
@ -61,7 +61,7 @@ defmodule WeewxProxy.Modbus.Deye do
def handle_message(["bitshake", "tele", "smartmeter", topic], publish, state) do def handle_message(["bitshake", "tele", "smartmeter", topic], publish, state) do
current_time = Time.utc_now() current_time = Time.utc_now()
current_hour = current_time.hour 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 if topic == "SENSOR" do
{:ok, meter_data} = Jason.decode(publish) {:ok, meter_data} = Jason.decode(publish)
@ -106,7 +106,8 @@ defmodule WeewxProxy.Modbus.Deye do
# Helper # Helper
@spec parse_message(String.t(), String.t()) :: float() | nil @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) Utils.parse_float(message)
end end
@ -115,8 +116,8 @@ defmodule WeewxProxy.Modbus.Deye do
@spec handle_reading(String.t(), float() | nil, {:day | :active, float()}) :: {:day | :active, float()} | nil @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(_topic, nil, state), do: state
defp handle_reading("deye/day_energy", reading, old_reading) do defp handle_reading("deye/day_energy", reading, state) do
if reading >= old_reading do if reading >= state.solar_energy_day do
timestamp = DateTime.utc_now() |> DateTime.to_unix() timestamp = DateTime.utc_now() |> DateTime.to_unix()
data = %{ data = %{
@ -127,10 +128,9 @@ defmodule WeewxProxy.Modbus.Deye do
:ok = Publisher.publish("weewx/ingest_si", data) :ok = Publisher.publish("weewx/ingest_si", data)
:ok = Publisher.publish_value_map("hadata/deye", data) :ok = Publisher.publish_value_map("hadata/deye", data)
reading %{state | solar_energy_day: reading}
else else
_ = Logger.warning("Reading unexpectedly decreased from #{old_reading} to #{reading}") state
old_reading
end end
end end
@ -147,4 +147,40 @@ defmodule WeewxProxy.Modbus.Deye do
state state
end 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 end