[Solved] Rule from Habmin is not working anymore (Trigger a switch with Time cron)

Hello Community,

i have generated a rule with Habpanel, since 2 years, and with the latest 2.5 Version of openhab it doesnt work anymore.

// This rule file is autogenerated by HABmin.
// Any changes made manually to this file will be overwritten next time HABmin rules are saved.

 Imports
import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.joda.time.*

// Global Variables

rule "Steckerleiste Schlafzimmer um 2:00 Uhr ausschalten"
when
    Time cron "0 * * * * ?"
then
  if ((((new LocalTime().getLocalMillis()) >= (new LocalTime(2, 0, 0, 0).getLocalMillis())) && ((new LocalTime().getLocalMillis()) <= (new LocalTime(2, 30, 0, 0).getLocalMillis())) && (flagitemtvbed.state == ON)))  {
    sendCommand(Power_FF_Bed, OFF)
  }
end

I noticed it after I rewrote the rule because I haven’t used it for a long time.
i tried to change it to:

rule "Steckerleiste Schlafzimmer um 2:00 Uhr ausschalten"
when
	Item MartinsPhone received update OFF or
	Time cron "0 0 2 * * ?"
then
	if (flagitemtvbed.state == ON && Power_FF_Bed.state == ON) {
		Power_FF_Bed.sendCommand(OFF)
		}
end

can someone help me to get this rule working again, it should turn off a sonoff pow after 2 o clock or when my phone is not in the wlan, maybe someone can show me a better solution to do this with a rule.

I probably made a mistake because I previously commented everything out with // and have no backup of the original file

Take those imports out, not needed since openHAB-1
There has been work around Joda time, might explain your problem turning up now.

I’m not sure about the script.actions.* import, but nothing that you show here seems to require it.

How are you turning this item off? postUpdate or sendCommand?

MartinsPhone is created with the unifi binding and is a switch item.
so it should be a postUpdate i think

1 Like

I also use the Unifi binding.

I have the following item connected to the guestClients channel, to detect if any guests connect to the network (this is using the latest snapshot binding, not the default one from PaperUI):

Number nWifiGuestClients "Default Site: Guest Clients [%d]" { channel="unifi:site:home:default:guestClients" }

In a rule, I then simply check to see if the number has changed:

when
    Item nWifiGuestClients changed
then
    ....

Perhaps change your MartinsPhone trigger to:

Item MartinsPhone changed to OFF

(As @rossko57 says, for the particular rule you have shown you don’t need any of those imports.)

EDIT: Actually, also double check your cron expression. To trigger at 2am I would use:

0 0 2 ? * * *

generated from here - cron expressions need to be quartz cron expressions.

I think it would be best to scrap the rule and start from scratch. Explain what you would like to happen in the rule and don’t get hung up on how you think it should be written :slight_smile:. It seems like you want the television in your bedroom to turn off at any time between 2am and some other time, say 5am, if the television is on and your phone is on your network. How often should these conditions be checked? Here is an example that checks every 5 minutes from 2-5am…

rule "Steckerleiste Schlafzimmer um 2:00 Uhr ausschalten"
when
    Item MartinsPhone received update OFF
    or
    Time cron "0 0/5 2-4 * * ?"
then
    if (flagitemtvbed.state == ON && Power_FF_Bed.state == ON && (new DateTimeType).zonedDateTime.getHour > 2 && (new DateTimeType).zonedDateTime.getHour < 5) {
        Power_FF_Bed.sendCommand(OFF)
    }
end

thank you for this example, and how do i need to change the rule when i will change the time from 2 am to 5 pm? Can i change it to <17 (german time format)?
i should power off the powersocket of the tv when i am at work ore not at home, but it should be on when i am at home from work earlier ore in holidays, therefore I have the query with my cell phone whether I am at home. So your rule is the right one i need :slight_smile:

It is not clear what you mean. If you want 5pm, it would be 17 in 24-hour time format, which is what the getHour method returns. You would need to change the cron expression too.

I’m glad I could help, but if you would provide details on what you’d like the rule to do, then it would be much easier to help you out.

i work from 8am to 5pm, but i will start the rule at 2 am at night, because i dont need it on when i sleep.
flagitemtvbed is a dummy switch to turn on and off the rule (better said stop the execution of the rule)
Power_FF_Bed is a sonoff pow, on this the tv is plugged in

its good when the rule is checking every 5 min if the MartinsPhone is ON (than i am at home and the Power_FF_Bed should turn ON again that i can watch TV :slight_smile: )

Hello,

thank you for this rule, i have now setup like this:

rule "turn off powerplug"
when
    Item MartinsPhone received update OFF
    or
    Time cron "* 0/5 2-15 * * ?"
then
    if (flagitemtvbed.state == ON && Power_FF_Bed.state == ON && (new DateTimeType).zonedDateTime.getHour > 2 && (new DateTimeType).zonedDateTime.getHour < 19) {
        logInfo("Steckerleiste TV Schlafzimmer","Steckerleiste Schlafzimmer ausgeschalten")
        Power_FF_Bed.sendCommand(OFF)
    }
end

rule "turn on powerplug"
when
    Item MartinsPhone received update ON
    or
    Time cron "* 0/5 19-23 * * ?"
then
    if (flagitemtvbed.state == ON && Power_FF_Bed.state == OFF && (new DateTimeType).zonedDateTime.getHour > 19 && (new DateTimeType).zonedDateTime.getHour < 24) {
        logInfo("Steckerleiste TV Schlafzimmer","Steckerleiste Schlafzimmer eingeschalten")
        Power_FF_Bed.sendCommand(ON)
    }
end

Maybe i will add an option to ckeck if it is mo-fr also, and if it is sa-so it should not turn off the powerplug