ioBroker.history icon indicating copy to clipboard operation
ioBroker.history copied to clipboard

Cannot get earliest stored value (limit 1)

Open klein0r opened this issue 2 years ago • 6 comments

Describe the bug

I want to use sendTo to get the first value (with the lowest timestamp) in history database. But that is not possible:

To Reproduce

Idea:

  • Select all data since timestamp 0
  • Ordered by date ascending
  • Limit result to 1 value
sendTo('history.0', 'getHistory', {
    id: 'yr.0.forecastHourly.0h.air_temperature',
    options: {
        start: 0,
        end: Date.now(),
        aggregate: 'none',
        returnNewestEntries: false,
        limit: 1,
    }
}, async (historyData) => {
    if (historyData.result.length > 0) {
        let startTs = historyData.result[0].ts;
        console.log(`Earliest vlaue -> ${formatDate(startTs, 'DD.MM.YYYY hh:mm:ss')}`);
    }
});

The first value in my DB is from 1636560079478 -> 10.11.2021 (~ 1 year old). But I receive: Earliest vlaue -> 09.11.2022 11:25:28 (today).

When I set the limit to 2000, the result is Earliest vlaue -> 13.08.2022 18:25:29.

Seems like the limit is applied to "the wrong side" of the result.

Versions:

  • Adapter version: 2.2.0

klein0r avatar Nov 09 '22 10:11 klein0r

Same behaviour with beta version 2.2.4

klein0r avatar Nov 09 '22 10:11 klein0r

Yes this is because of the 0 as start ... this is handled as "not defined" ... try 1 (or use 1.1.2000 becausthe adapter was not exosting before that date at all)

Apollon77 avatar Nov 09 '22 11:11 Apollon77

Just checked the code start: 0 doesn't survive the option checks. start: 1 does the trick! Maybe you should check if options.start is a number.

https://github.com/ioBroker/ioBroker.history/blob/24cb6c5f3fa40dc3fc435fd45eda801e79656739/main.js#L1224-L1226

klein0r avatar Nov 09 '22 11:11 klein0r

No idea if this would be breaking for some visus or such ... no idea if someone sends start=0 to actually trigger this behaviour as it is now ... :-(

Apollon77 avatar Nov 09 '22 11:11 Apollon77

Ok, got it :) Feel free to close this issue

klein0r avatar Nov 09 '22 11:11 klein0r

I leave open, maybe a README note might be an idea

Apollon77 avatar Nov 09 '22 11:11 Apollon77