(SOLVED) Timer help, turn device off IF wattage <X for 2minutes

  • Platform information:
    • Hardware: DS220+ INTEL Celeron J4025
    • OS: Docker on DSM 7.0.1-42218
    • openHAB version: 3.2

My TV is plugged into a Shelly plug-S.
My receiver is plugged into a separate Shelly plug-S

When my TV goes to standby (<10watts) it needs to turn off the receiver and the other way around.
This works of course but now and again my TV boots up in the background (when in stand-by) and goes to 80watt for 2seconds, so turning on the receiver.
When the TV is on but only showing blacks, it draws 40watt so I cant use this threshold since it then turns off the receiver during a dark scene.

When the wattage goes >10watts I need it to wait for a certain amount of time, check the wattage again, and only then turn on the receiver (and the other way around ofcourse).
Like a (delay) or timer function.

I searched and tried for ages but have no clue how to work with timers in openHab.
Wish it was as easy as “blink without delay” :wink:

Hope you can help me out, thanks again for your time

First choice you need to make is what rules system you prefer in OH3. No point in showing you hundreds of DSL examples in this forum if you want to use Groovy or Blockly.

Ofcourse, So far I used Blockly and ECMAScript

Use blockly, create a timer e g. For 2 minutes and inside the time add the code that should be executed after 2 minutes (e.g. checking the current energy consumption or using persistence service to get min/max/avg watt of the last 2 min)

I have made something similar, where I monitor the power consumption of a charger and have it switch off when the power consumption drops below a certain wattage.

I made 1 helper (Tasmota41128EChargingWatchdog) item with an expiration timer of 3 minutes and command = OFF. This item is first switched ON through a rule at the same time as the Tasmota switch is switched on.

I have a second rule that is triggered when this helper item is switched off (I.e. after 3 minutes). The code for that rule is below.

var item=items.getItem("Tasmota41128E_Power");
var timeInterval = 3;
var timeElapsed = time.ZonedDateTime.now() - (timeInterval * 60);
var minimumWattage = 4

if ((item.history.averageSince(time.ZonedDateTime.now().minusSeconds(180))) < minimumWattage ) {
  items.getItem("Tasmota41128E_State").sendCommand("OFF")
} else {
  items.getItem("Tasmota41128EChargingWatchdog").sendCommand("ON")
}

You could use something like this:

//below gets the historical sate if an item
var PersistenceExtensions = Java.type("org.openhab.core.persistence.extensions.PersistenceExtensions");
var ZonedDateTime = Java.type("java.time.ZonedDateTime");

var myPersistentItem = ir.getItem("Zigbeesengledbulb_Zigbeesengledbulb");
logger.info("The pyload is {}", myPersistentItem);

var sinceDate = ZonedDateTime.now().minusDays(7);
//var sinceDate = ZonedDateTime.now().minusHours(5);

var maximumValueSince = PersistenceExtensions.maximumSince(myPersistentItem, sinceDate);

logger.info("Historic item is {}", maximumValueSince);
logger.info("Item name is {}", maximumValueSince.name);
logger.info("Item state is {}", maximumValueSince.state);

You might already be aware of this: they sell a power strip that has “Master / Slave” thing. When the load on the Master is low, it turns off the slave outlets.

Example (and there are many out there): Amazon.com

Anyway, for your original question, it can be implemented in jruby like this:

require 'openhab'

LOW_POWER = 0|'W'...10|'W'
HIGH_POWER = 10|'W'..

rule 'Turn on the receiver' do
  changed TV_PowerDraw, from: LOW_POWER, to: HIGH_POWER, for: 5.seconds
  run { Receiver_Power.ensure.on }
end

rule 'Turn off the receiver' do
  changed TV_PowerDraw, from: HIGH_POWER, to: LOW_POWER, for: 2.minutes
  run { Receiver_Power.ensure.off }
end

The for: argument above ensures that the rule only executes if the item’s state remains in the to state for the specified duration. You won’t need to manually create your own timer to keep track of it.

The ensure will not send the on/off command if the item is already in that state.

Thank you all so much for your help!
What a great community this is!

I just want to commented to @Matze0211 that Blockly doesn’t support timers but now that I updated OH3 it does, awsome.

I also tried @mgeers way and this also works.
@ubeaut, thank you for your input but your suggestion is beyond what I can understand.

@JimT , I am looking into jruby next graveyard shift, it looks fairly straightforward.

Thanks again all.

1 Like

Works like a charm, glad the OH devs made is so eas for a noob like.
Thx again for everyones time!