Pulse counter - from energy to power

Hi there,

I have a pulse counter connected to an rpi zero which sends its values via MQTT to home assistant running on another hardware component. The MQTT messages look like this:

{
    "fields": {
        "nstime": 1640676204429749500,
        "pulse": 3911667,
        "uptime": 11280557.66
    },
    "name": "s0meter",
    "tags": {
        "host": "s0pi",
        "path": "/home/pi/s0readings/sensorlog-current.txt"
    },
    "timestamp": 1640676204
}

The ‘nstime’ and ‘pulse’ fields are the most important ones:

  • pulse: the current pulse amount, my s0 meter gives 1000 pulses per kWh
  • nstime: the epoch at the time of the pulse

This gives me energy, not power. According to Pulse counter to current usage I should be able to use the derivative component (Derivative - Home Assistant) to calculate power.

However, I have the impression the derivative function will use the time that HA received the MQTT message. This is incorrect in my case: it should use the epoch mentioned in the MQTT message (nstime).

Is there a way to calculate the power manually, using e.g. a template? I guess it should be something like:
W = 1Wh / ( epoch(last_pulse) - epoch(previous_pulse) / 3600 )

Since I’m using telegraf to ship the values via MQTT, I have solved the problem there (at the source) using [[aggregators.derivative]] and [[processors.starlark]] to multiply the derivative with 3600.

This ‘power’ information is then published to a topic separate from the ‘energy’ information. This may even be cleaner than doing it in HA.