Skip to content

Fix: Total Import Energy zero-value safeguard for Home Assistant#338

Open
Xoliul wants to merge 1 commit intodingo35:masterfrom
Xoliul:feature/energy-zero-value-safeguard
Open

Fix: Total Import Energy zero-value safeguard for Home Assistant#338
Xoliul wants to merge 1 commit intodingo35:masterfrom
Xoliul:feature/energy-zero-value-safeguard

Conversation

@Xoliul
Copy link

@Xoliul Xoliul commented Mar 7, 2026

Prevents publishing 0 for EV/Mains Import Active Energy over MQTT and REST when meter hasn't been read yet or on rare comm glitches. A single 0 corrupts HA statistics (state_class: total_increasing).

  • Add lastKnownEVImportWh/lastKnownMainsImportWh to SettingsCache (NVS)
  • Add energyValueToPublish() helper - never send less than last known good
  • Update mqttPublishData, mqttSmartEVSEPublishData, REST GET/POST, MQTT Set/EVMeter

Prevents publishing 0 for EV/Mains Import Active Energy over MQTT and REST
when meter hasn't been read yet or on rare comm glitches. A single 0 corrupts
HA statistics (state_class: total_increasing).

- Add lastKnownEVImportWh/lastKnownMainsImportWh to SettingsCache (NVS)
- Add energyValueToPublish() helper - never send less than last known good
- Update mqttPublishData, mqttSmartEVSEPublishData, REST GET/POST, MQTT Set/EVMeter

Made-with: Cursor
@dingo35
Copy link
Owner

dingo35 commented Mar 7, 2026

Ok so you never found the cause for the 0 value and you built this workaround....

I would prefer to get rid of the origin of the problem.
If you cant find that, isnt it much easier to test for 0 value and prevent it from being sent?

@dingo35
Copy link
Owner

dingo35 commented Mar 7, 2026

That would only go wrong the first Wh of the energy meter, which for imported power would soon solve itself. But possible trouble for exported power for meters where no power delivery (solar) are present, not sure how that would behave.

Perhaps have some logger running on telnet, with timestamps, would show what causes the bug?

@Xoliul
Copy link
Author

Xoliul commented Mar 7, 2026

Ok so you never found the cause for the 0 value and you built this workaround....

I would prefer to get rid of the origin of the problem. If you cant find that, isnt it much easier to test for 0 value and prevent it from being sent?

You can't prevent 0 values from being saved in HA unless you wrap it in a complex template sensor. That's not something anybody does from the start. If you do it afterwards, you lose all previous statistics, as they can not be transferred.

It has happened twice for me in one year, both at random moments where nothing much seemed to be happening. For me it is nearly impossible to reproduce.

Safeguarding from sending 0, or a lower value seems like an acceptable solution.

@mstegen
Copy link
Collaborator

mstegen commented Mar 9, 2026

Isn't this just overcomplicating things?

What if you would only send kWh data to HA that is > 0 ? Or have you seen anything else then a zero in your HA statistical data ?
This would have effect on brand new meters, as they would not send any data to HA for this sensor until there is a positive number. But i don't think that is a major problem as the resolution of these meters is quite high. They would reach a positive measurement quite quickly.

@dingo35
Copy link
Owner

dingo35 commented Mar 9, 2026

I agree; the only problem would be on the "exported energy" value of the EV meter, which in general is 0 and will stay that way...

@Xoliul
Copy link
Author

Xoliul commented Mar 9, 2026

Sure, it can be made simpler.
I don't see the problem with EV export? This is basically an unused value, and you can skip any value checks on it.

@dingo35
Copy link
Owner

dingo35 commented Mar 9, 2026

I would say treat all Meters the same, so also but this safety on the export value for all meters.

@dingo35
Copy link
Owner

dingo35 commented Mar 17, 2026

Ok the cause of the problem is that after a reboot, using modbus kWh meters, we might be giving out a zero value if the mqtt publish and or the REST publish is done before the answer on the modbus request of the energy value is received.

This version guards giving out 0 values for import- and export energy values:
fa3ff31.zip

Since I have no testing capabilities here, please test !!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants