feat(deye): ignore invalid counter increments
All checks were successful
Build / build (push) Successful in 3m10s

This commit is contained in:
Daniel Kempkens 2024-08-09 20:14:49 +02:00
parent 42edf668a4
commit c5e58816db
Signed by: daniel
SSH key fingerprint: SHA256:Ks/MyhQYcPRQiwMKLAKquWCdCPe3JXlb1WttgnAoSeM

View file

@ -12,7 +12,7 @@ defmodule WeewxProxy.Modbus.Deye do
@impl true
def init(_opts) do
_ = Logger.info("Initializing handler")
{:ok, nil}
{:ok, 0}
end
@impl true
@ -59,6 +59,10 @@ defmodule WeewxProxy.Modbus.Deye do
@impl true
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
if topic == "SENSOR" do
{:ok, meter_data} = Jason.decode(publish)
@ -77,9 +81,9 @@ defmodule WeewxProxy.Modbus.Deye do
:ok = Publisher.publish("weewx/ingest_si", mqtt_data)
:ok = Publisher.publish_value_map("hadata/bitShake", mqtt_data)
{:ok, state}
{:ok, new_state}
else
{:ok, state}
{:ok, new_state}
end
end
@ -111,41 +115,36 @@ 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, {:active, active}) do
defp handle_reading("deye/day_energy", reading, old_reading) do
if reading >= old_reading do
timestamp = DateTime.utc_now() |> DateTime.to_unix()
data = %{
dateTime: timestamp,
solarEnergyDay: reading * 1000.0
}
:ok = Publisher.publish("weewx/ingest_si", data)
:ok = Publisher.publish_value_map("hadata/deye", data)
reading
else
_ = Logger.warning("Reading unexpectedly decreased from #{old_reading} to #{reading}")
old_reading
end
end
defp handle_reading("deye/ac/active_power", reading, state) do
timestamp = DateTime.utc_now() |> DateTime.to_unix()
data = %{
dateTime: timestamp,
solarEnergyDay: reading * 1000.0,
solarEnergyActive: active
}
:ok = Publisher.publish("weewx/ingest_si", data)
:ok = Publisher.publish_value_map("hadata/deye", data)
nil
end
defp handle_reading("deye/day_energy", reading, _other) do
{:day, reading}
end
defp handle_reading("deye/ac/active_power", reading, {:day, day}) do
timestamp = DateTime.utc_now() |> DateTime.to_unix()
data = %{
dateTime: timestamp,
solarEnergyDay: day * 1000.0,
solarEnergyActive: reading
}
:ok = Publisher.publish("weewx/ingest_si", data)
:ok = Publisher.publish_value_map("hadata/deye", data)
nil
end
defp handle_reading("deye/ac/active_power", reading, _other) do
{:active, reading}
state
end
end