Rule based on Watt usege difference

Tags: #<Tag:0x00007f5c98b29de0> #<Tag:0x00007f5c98b29ca0> #<Tag:0x00007f5c98b29bb0>

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:

import org.joda.time.DateTime
import org.openhab.core.persistence.*
import org.openhab.core.library.types.*

rule “tvLights”

when

Item ShellyplugSTvMonitor_Meter_ changed

then
//logInfo(“tesztOK”)

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

Any thoughts? Thanks in advance!

I’d try it that way
if ((ShellyplugSTvMonitor_Meter_.historicState(now.minusSeconds(5)).state as Decimal) < 25.0)

However, does your persistence service really store values in 5 second steps?:thinking:

To be honest I have no idea. Can you point in the right direction?

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

1 Like

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 :raised_hand_with_fingers_splayed::raised_hand_with_fingers_splayed::raised_hand_with_fingers_splayed:

Tried it:

My code now:

rule “tvLights”

when

Item ShellyplugSTvMonitor_Meter_ changed

then

if (ShellyplugSTvMonitor_Meter_.state > 25.0){
    if (ShellyplugSTvMonitor_Meter_.previousState().state < 25.0){
        LivingTvLights_Switch.sendCommand("ON")
        LivingTVled_Power.sendCommand("ON")
   	    }
   	}


if (ShellyplugSTvMonitor_Meter_.state < 25.0){
    if (ShellyplugSTvMonitor_Meter_.previousState().state > 25.0){
        LivingTvLights_Switch.sendCommand("OFF")
        LivingTVled_Power.sendCommand("OFF")
        }
    }

end

Error in log:

2020-05-23 17:24:11.492 [WARN ] [nce.extensions.PersistenceExtensions] - There is no default persistence service configured!

2020-05-23 17:24:11.496 [WARN ] [nce.extensions.PersistenceExtensions] - There is no queryable persistence service registered with the id ‘null’

2020-05-23 17:24:11.501 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘tvLights’: cannot invoke method public abstract org.eclipse.smarthome.core.types.State org.eclipse.smarthome.core.persistence.HistoricItem.getState() on null

It seems like it needs a persistence service still sadly.

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.

2 Likes

Thank you, yes ti seems like, I found the wrong forum thread about the previousState. Thank you to everyone!