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

**URL:** <https://community.openhab.org/t/how-to-calculate-average-dates-of-when-an-item-has-been-switched-on/53503>\
**Category:** Scripts & Rules\
**Created:** [October 10, 2018, 9:07pm UTC](https://community.openhab.org/t/how-to-calculate-average-dates-of-when-an-item-has-been-switched-on/53503 "2018-10-10T21:07:40Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [October 22, 2018, 4:59pm UTC](https://community.openhab.org/t/how-to-calculate-average-dates-of-when-an-item-has-been-switched-on/53503/9 "2018-10-22T16:59:18Z")

</div>

> [@lolu](#):
>
> As far as I understood from [documentation](https://www.openhab.org/docs/configuration/persistence.html#persistence-extensions-in-scripts-and-rules), `historicState` will return the state of the item at the specified point of time, which is `now.minusDays(1)` in your suggestion.

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](https://www.eclipse.org/smarthome/documentation/javadoc/index.html)

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.

> [@lolu](#):
>
> It needs to become more generic I think (should be able to handle boolean, string ,…, from `.getState` , …) and to handle exceptions (what to do when there is no data to get state from? …).

boolean is not a valid Item state. See [Items | openHAB](https://www.openhab.org/docs/configuration/items.html#type) 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.

> [@lolu](#):
>
> As I will have to use this code several times in a rule, does anyone know how to convert it into a lambda (or a script)?

- [Reusable Functions: A simple lambda example with copious notes](https://community.openhab.org/t/reusable-functions-a-simple-lambda-example-with-copious-notes/15888)

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

> [@lolu](#):
>
> Also, out of curiosity, if I call twice the same lambda/script in a rule, will two instances of the lambda/script be called and will they run in parallel?

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.

---

_[View the full topic](https://community.openhab.org/t/how-to-calculate-average-dates-of-when-an-item-has-been-switched-on/53503)._
