Hi there I’m trying to autamte some lights based on my TV power consumption. It’s a smart tv so it consumes some power even if it’s “off”. So I want two compare the current consumption with the 2 second ago and if the consumption is higher then 20 watss, then trigger the lights. currently that’s my code:
if (ShellyplugSTvMonitor_Meter_.state > 25.0){
//logInfo("tesztOK")
if (ShellyplugSTvMonitor_Meter_.historicState(now.minusSeconds(5)).state < 25.0){
LivingTvLights_Switch.sendCommand("ON")
LivingTVled_Power.sendCommand("ON")
}
}
if (ShellyplugSTvMonitor_Meter_.state < 25.0){
LivingTvLights_Switch.sendCommand("OFF")
LivingTVled_Power.sendCommand("OFF")
}
end
But I’m getting this error:
openhab cannot invoke method public abstract org.eclipse.smarthome.core.types.State org.eclipse.smarthome.core.persistence.HistoricItem.getState() on null
Don’t do any of that with OH2
Careful when using old OH1 postings.
Obviously, you need to have installed a persistence service.
You need that service to persist your Item
You need that to be same service used by your historicState() call
Sorry for the noob question then. I thought it could be read back from the log itself. I have some experience with influxDB, but I think I don’t need to store the value changes for ever. So what persistence service would you recommend for this kind of a use case?
Is there a special reason for the 5 seconds? I would just use the previousState. This implicit variable is available in a rule that triggers on the change of an item.
That way you won’t need a persistence service installed.
BTW:
Sorry for missing that it was your first post, I should have noticed.
Welcome
That’s a persistence method, yes. Won’t work without a persistence service.
Rules have magical Implicit Variables, one of those is the similarly named previousState
This variable will give the previous state in any rule that is triggered by change.
You could use it like -
if (previousState < 25.0) {
although as it is a state object, I think you might need
if ( (previousState as Number) < 25.0) {
I’m too sure of the purpose of all this though. If the TV power changes to >25, wouldn’t you just want the lights ON and not care what the power was before?
You can check to see if lights already ON before issuing duplicate commands.