You can’t just build a String that is the name of an Item and get that Items state. When you build the String all you have is a String and a String doesn’t have a state
. What you need is the Item. See Design Pattern: Associated Items for options.
Furthermore I have no idea what the head
in the filter is all about.
There are map and reduce functions that are used for this. See Design Pattern: Working with Groups in Rules for examples.
And you cannot modify a variable outside of a forEach lambda from inside the lambda. You can only access vals and vals cannot be changed. So you can’t reduce the list to a String using String concatenation. Thus you really do need to use the map/reduce methods or use some other data structure such as a StringBuilder which you can define as a val and then call append to add more text to it.
import org.eclipse.smarthome.model.script.ScriptServiceUtil
...
// Get the open stations with meaningful error log statements
val openStations = AllPetrolStations_E5.members.filter[ y |
ScriptServiceUtil.ir.getItem(y.name.replace('_E5', '_OpenState')).state == OPEN
]
val avgLess = openStations.filter[ y |
y.state < y.averageSince(now.minusDays(7))
]
val LowPriceStationE5 = avgLess.reduce[ str, y | str += y.name.replace('_', ' ') + ": " + y.state + "\n" ]
I’m not 100% positive on the above but the linked to design patterns should help. I mostly use Jython rules these days, here is a notional version in Jython.
from core.rules import rule
from core.triggers import when
from core.actions import PersistenceExtensions
@rule("E5_Low_PrivedRemind")
@when("Item Home_Dummyswitch changed")
def low_price(event):
if items["ALL_PetrolStations_E5_Remind"] != ON:
return
openStations = [ y for y in ir.getItem("ALL_PetrolStations_E5").members if items[y.name.replace('_E5', '_OpenState')] == OPEN ]
avgLess = [y for y in openStations if y.state < PersistenceExtensions.averageSince(y, DateTime.now().minusDays(7))]
LowPriceStationE5 = ""
for y in avgLess:
LowPriceStationE5 = "{} : {}\n".format(y.name.replace('_', ' '), y.state)
if LowPriceStationE5:
low_price.log.info("Reminder: currently these 7d-E5Lowprices: \n{}".format(LowPriceStationE5)