How to calculate average dates of when an item has been switched on?

Technically, it returns a HistoricItem that has a number of methods, one of which is to get the state of the Item at that time. But we are using the getTimestamp method here to get the timestamp that is stored with the Item in the database. So if you are using a changed strategy in persistence, it will be the time that the Item changed to that state prior to now.minusDays(1).

See Archived Projects | The Eclipse Foundation

It makes no sense for getTimestamp to return the same date/time you passed to it to create it in the first place. If that is indeed what is happening then I’d argue it is a bug and an issue should be filed.

boolean is not a valid Item state. See Items | openHAB for a list of all the Items and the types the Items store.

historicState will return null if there is no data at that time.

You can’t pass arguments nor get a return value from a Rules DSL Script.

Lambdas are Objects. If you create the lambda as a global and you call twice or more from Rules running at the same time, they will be sharing the same lambda and depending on timing and shared resources they will stomp on each other.

This is one of the reasons I frequently recommend against the use of lambdas.

However, this is a case where a lambda can be quite useful. So what I would recommend, since you only need to call it from the same Rule, is to create the lambda within the Rule. This will give each instance of the Rule running its own copy of the lambda Object to work with.