Add up time of an alert

Basically I’m trying to find the run-time of the generator.

So I have some SNMP alerts for voltage at a remote site and an autostart generator.

Is there any way of querying LibreNMS to find out how long alerts for a given sensor have been active between two dates?

It’s unclear what you want here.

If you want to know how long an alert had been active once it clears that’s easy to do in the alert templates.

For example as part of most of my alert templates I have

@if ($alert->state == 0)
Time elapsed: {{ $alert->elapsed }}
@endif
Timestamp: {{ $alert->timestamp }}

If $alert->state == 0 the alert has cleared so the Time Elapsed line only appears in recovery alerts and will list the total amount of time the alert was active, for example that a device was down for 2 days, 5 hours and 15 minutes…

The Timestamp will appear in both fault and recovery alerts - this is the timestamp of when the alert was generated. This is useful if the fault can delay delivery of alerts such as a network or server outage because alerts may arrive late or out of order, (so email times etc can’t be relied on) but the Timestamp will always be correct and be when the alert was triggered or cleared.