From 89ce8b46ea961c2ad42927c3a123e03939f7aec1 Mon Sep 17 00:00:00 2001 From: Marius Date: Tue, 17 Mar 2026 13:53:10 +0200 Subject: [PATCH] fix: read NordPool price from HA entity instead of scoped service to fix cross-app price isolation Made-with: Cursor --- HomeAssistant.Server/appsettings.json | 2 +- .../PowerMonitor.Manager.csproj | 1 - PowerMonitor.Manager/PowerMonitorInit.cs | 29 +++++++++++++++---- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/HomeAssistant.Server/appsettings.json b/HomeAssistant.Server/appsettings.json index 52f52b8..bf591d5 100644 --- a/HomeAssistant.Server/appsettings.json +++ b/HomeAssistant.Server/appsettings.json @@ -28,7 +28,7 @@ }, "PowerMonitor": { "MainMeterEntityId": "sensor.sonoff_100267d63f_power", - "PollIntervalSeconds": 5, + "PollIntervalSeconds": 1, "SmoothingWindowSize": 10, "AnomalyThresholdWatts": 500, "ExcludedSensorPatterns": [ diff --git a/PowerMonitor.Manager/PowerMonitor.Manager.csproj b/PowerMonitor.Manager/PowerMonitor.Manager.csproj index 33e2a8f..8c4e2bb 100644 --- a/PowerMonitor.Manager/PowerMonitor.Manager.csproj +++ b/PowerMonitor.Manager/PowerMonitor.Manager.csproj @@ -16,7 +16,6 @@ - diff --git a/PowerMonitor.Manager/PowerMonitorInit.cs b/PowerMonitor.Manager/PowerMonitorInit.cs index 2f48cdf..6c0a492 100644 --- a/PowerMonitor.Manager/PowerMonitorInit.cs +++ b/PowerMonitor.Manager/PowerMonitorInit.cs @@ -1,3 +1,4 @@ +using System.Globalization; using System.Reactive.Concurrency; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; @@ -5,7 +6,6 @@ using NetDaemon.AppModel; using NetDaemon.Extensions.Scheduler; using NetDaemon.HassModel; using NetDaemon.HassModel.Entities; -using NordPoolManager.Interface; using PowerMonitorManager.Interface; using PowerMonitorManager.Models; using PowerMonitorManager.Models.Consts; @@ -23,7 +23,8 @@ public sealed class PowerMonitorInit : IPowerMonitorInit private readonly IOtherPowerCalculationService _calculationService; private readonly IPowerCostCalculationService _costService; private readonly IPowerMonitorEntityService _entityService; - private readonly INordPoolPriceService _priceService; + + private const string NordPoolCurrentPriceEntityId = "sensor.nordpool_current_price"; private int _mainMeterConsecutiveFailures; private DateTime? _anomalyStartTime; @@ -36,8 +37,7 @@ public sealed class PowerMonitorInit : IPowerMonitorInit IPowerSensorDiscoveryService discoveryService, IOtherPowerCalculationService calculationService, IPowerCostCalculationService costService, - IPowerMonitorEntityService entityService, - INordPoolPriceService priceService) + IPowerMonitorEntityService entityService) { _haContext = haContext; _logger = logger; @@ -47,7 +47,6 @@ public sealed class PowerMonitorInit : IPowerMonitorInit _calculationService = calculationService; _costService = costService; _entityService = entityService; - _priceService = priceService; _ = InitializeAndScheduleAsync(); } @@ -189,7 +188,7 @@ public sealed class PowerMonitorInit : IPowerMonitorInit var calculation = _calculationService.Calculate(totalPower.Value, deviceReadings); - var nordPoolPrice = _priceService.CurrentPrice; + var nordPoolPrice = ReadNordPoolPrice(); var priceSource = nordPoolPrice > 0 ? "live" : _costService.PriceSource; var effectivePrice = nordPoolPrice > 0 ? nordPoolPrice : _costService.LastKnownPrice; @@ -245,6 +244,24 @@ public sealed class PowerMonitorInit : IPowerMonitorInit } } + private double ReadNordPoolPrice() + { + var state = _haContext.GetState(NordPoolCurrentPriceEntityId); + if (state?.State is null || state.State == "unavailable" || state.State == "unknown") + { + _logger.LogDebug("NordPool price entity unavailable, using last known price"); + return 0; + } + + if (double.TryParse(state.State, CultureInfo.InvariantCulture, out var price) && price > 0) + { + return price; + } + + _logger.LogDebug("NordPool price entity returned non-positive value: {State}", state.State); + return 0; + } + private double? ReadMainMeterPower() { var state = _haContext.GetState(_options.MainMeterEntityId);