Hi, as part of my energy monitoring, I’m calculating the current watt via measuring the rotation time of the disk of our ferraris style power meter (Electricity meter - Wikipedia), detected via an IR detector to detect the red marker on the disc. (overall consumption in kWh is just counting the pulses in a separate logic, no time measuring needed for that)
The logic behind it is super straightforward, with setting a “lastpulse” global with the current milliseconds when the ESP starts and initially syncs the time with HA
time:
- platform: homeassistant_time
on_time_sync:
then:
- id(lastpulse) = millis();
and then calculating the delta everytime the next pulse comes in:
id(rotationtime) = (millis() - id(lastpulse)) / 1000
id(lastpulse) = millis();
This is running smooth for 2 months now, but I occasionally get measurings of ultrashort rotation times (e.g. 0.3 seconds) which result in massive unrealistic watt peaks (30000-50000 watts).
Digging into Time — ESPHome I found the line " With the homeassistant time platform, the native API connection to Home Assistant will be used to periodically synchronize the current time.", which explains why the on_time_sync doesnt only get triggered once on boot/connection. I can code around this by checking if the lastpulse is not zero during the on_time_sync, or I can ignore any rotationtime values under a certain threshold where things get unrealistic.
But I was still wondering: as I am also using the millis() continuously with every pulse, and I assume that the time sync might result in a shift of 1-2 seconds from time to time: is there a way to “predict” the time of resync happening? Is that a fixed frequency? I dug into github, but couldnt find a fixed timer myself.
Thanks in advance