You mean you want the transition? Well you have to look at the previous entries in the array to see what it may have changed from. Note that rrd4j saves a value every minute in addition to changes so if the switch is ON for more than a minute, the previous state will likely be ON too. So you’ll have to keep track of the states as you go to find those entries that represent a change.
That code makes no sense. Look at the section of the docs that I posted above. There is no “itemName” property of a PersistedItem
. And you should already know the itemName as all the results are only for the one Item (which I guess is confusingly called “itemName”?).
That’s also not how a filter
works. A filter is going to reduce the Array down to only those elements that match a criteria. state.itemName
isn’t a criteria. The function passed to the filter
needs to return a boolean (i.e. true
or false
). When the function returns true
the element of the Array is included in the result. When false
it’s excluded from the result.
This code really makes no sense and it’s totally unclear what you are trying to accomplish with it.
You should always look up every operation you are using in the JS Scripting docs and in generic JS docs and tutorials until you get more firm on JS. Don’t guess or try stuff at random.
Of course not because your filter doesn’t make sense so you’ve filtered the list of PersistedItems
down to nothing.
And if you are using ChatGPT or one of the other chatbots for this, stop. Thay all produce garbage when it comes to OH rules.
If I understand what you are asking, you want to reduce the Array down to only those entries that represent a change in the state.
var item = items.getItem("MyItem"); // get the Item
var today = time.toZDT(); // Get now
var yesterday = today.minusDays(1); // get yesterday at this time
var allStates = item.persistence.getAllStatesBetween(yesterday, today); // get all the values stored in the database in the past day
var prevState = null; // we need to keep track of data outside to filter so we know when a change occured
var changes = allStates.filter( persistedItem => { // filter all the PersitedItem Objects
if(persistedItem.state != prevState) { // If the current state is different from the last change, save it and keep the value
prevState = persistedItem.state; // save the change
return true;
}
return false; // this entry isn't a change, remove it
});
changes.forEach(change => console.info('Item: MyItem, State: ' + change.state + ', Time: ' change.timestamp); // loop through the filtered PersistedItems and log the state and timestamp
You already know the Item name because you have to use Item to get the persisted values in the first place.
Please please please do not just blindly try to use this code. Make sure you understand each and every line of it and why it’s there and what it does.
If JS or programming in general isn’t your thing, I cannot recommend Blockly strongly enough. See Design Pattern: Working with Groups in Rules for how to do filter and forEach and all those sorts of operations on lists in Blockly.