[SOLVED] Rule at a specific time

Hello. I have a rule
rule “Оповещения телеграм”
when
Item mihome_sensor_magnet_1xxxxc_isOpen changed to OPEN
then
if (now.getHourOfDay <= 8 || now.getHourOfDay >= 18) {
sendTelegram(“bot1”, “Открыта дверь в кабинете”)
}
end
Now it send from 8:00 to 18:00. How can it send messages from 8:50 to 18:00

You need a time trigger

https://www.freeformatter.com/cron-expression-generator-quartz.html

You could write a second rule with a cron trigger that’s changes a variable at the two times and make that variable the if condition of you first rule

can you help me. how can I do this?

Something like this:

var timevar = 1

rule "set time var morning"
when
    Time cron "0 50 8 ? * * *"
then
    timevar = 1
end

rule "set time var evening"
when
    Time cron "0 00 18 ? * * *"
then
    timevar = 0
end

rule “Оповещения телеграм”
when
    Item mihome_sensor_magnet_1xxxxc_isOpen changed to OPEN
then
    if (timevar == 1) {
        sendTelegram(“bot1”, “Открыта дверь в кабинете”)
    }
end

Not tested and written before the first coffee. It will always trigger until the evening rule fires the first time

Instead of using a variable, use an item. The problem with using a variable in this case is that when you update the rule file between 18:00 and 08:50 the timevar value will be 1 while it should be 0.
An item can also be persisted so that you can restore its value on startup.

Also checkout the time of day design pattern.

1 Like

I’m stupid, incorrectly explained at what time to send messages. I want to sent message in time, like in photo)

in you items file as a dummy switch:

Switch TimeVar

and than the rules:

rule "set time var morning"
when
    Time cron "0 50 8 ? * * *"
then
    TimeVar.sendCommand(OFF)
end

rule "set time var evening"
when
    Time cron "0 00 18 ? * * *"
then
    TimeVar.sendCommand(ON)
end

rule “Оповещения телеграм”
when
    Item mihome_sensor_magnet_1xxxxc_isOpen changed to OPEN
then
    if (TimeVar.state !== OFF) {
        sendTelegram(“bot1”, “Открыта дверь в кабинете”)
    }
end

as @marcel_erkel said you can persist the TimeVar item with restore on Startup so that it always has the right state or even override and switch it manually.

Is it possible to alter the original code as the example below, then you don`t need a new varaible:
if ( now.getHourOfDay < 8 || (now.getHourOfDay == 8 && now.getMinuteOfHour >=50 ) || now.getHourOfDay >= 18)

1 Like

Thank you. I used your code and it works)

A more simple way to solve this:

if(now.getMinuteOfDay < 8 * 60 + 50 || now.getMinuteOfDay > 17 * 60 + 59)

now.getMinuteOfDay is 0 to 1439.

Another, more sophisticated option:

if(now.plusHours(6).getMinuteOfDay > 869) // 14 * 60 + 29

@ 18:00, in 6 hours it will be 0:00. @ 8:30, in 6 hours it will be 14:30

2 Likes