Do something just once a day

Hi.
I have a simple rule that was working in OH2 that I can’t really figure out how to get to work the same way in OH3. What I was is that if my light sensor reports a value below a specified limit and if the clock is between specific points in time in the afternoon, it should light up my house (ie a specific Item group). However this should only do this if it’s the first time today that this happens (so if it gets above the limit and then below it again nothing should happen).

In OH2 I did this using a global variable in the rules file to keep track on whether this was the first time (and then another rule to reset that variable at noon), but that option is gone now.

I guess this is really simple, I just need a nudge in the right direction :sunglasses:

Create a switch item that is on or off depending on if it is the first time.

Turn it off with expire, cron or some.other logic.

1 Like

You can use either a dummy item with expire addon. Then define that the item should expire e.g. after 6 hours. So when the item was used it will stay on fo 6 hours and goes OFF again. Set the item on the first run to ON and check on each run if it is already ON, if yes do not run the rule.

If you don’t like the expire binding or need to define a specific time to reset the item you can build this with two rules. One with your current logic. This sets the dummy item to ON. another rule triggered by a cron timer. Set this cron timer e.g. to 21:00 and in the rule just set the item to OFF.

1 Like

Ok. Guess I’ll have to introduce another Item. Feels kinda cludgy so I thought there would maybe be a better solution :face_with_raised_eyebrow:

A variable won’t survive restarts and a switch item is readable and can trigger other rules. You can also turn it on or off manually for testing purposes.
Consider looking at getHourOfDay which could work with some sound logic.

1 Like

Have you considered using the Astro binding which will track the sun for your location.

You can create your own offset to tweak your setting but basically because the sun normally only sets once a day you should only get one event trigger like

rule "example trigger rule"
when
    Channel 'astro:sun:home:set#event' triggered START
then
    ...
end

Many years ago, before I could afford my first light sensor, I based the lighting on calculations like this. In my opinion it was completely worthless, the lamps always turned on too early or too late, depending on the weather. No, nothing I would want to get back to :sunglasses:

1 Like

OK cool

Then here may help you

Are you moving your rules to JSONDB? My understanding is . rules files should still work, global variables and all.

If using JavaScript with MainUI , you can preserve a variable from one run to the next by setting it on this.

this.myvariable = something;

The gotcha though is it only works in the one script action. You can’t share that variable across separate rules or even script actions in the same rule.

There is a ScriptExtensons feature that theoretically could be used for that, but I’ve not figured out how to use that yet.

If using text based rules you should be able to define global variables per normal.

Yes, that was my understanding as well, but it doesn’t seem that way. None of my rules involving global variables actually seem to work anymore. My plan is to go over all rules and make them using the new UI instead though, so it doesn’t matter in the long run, I just hoped that I wouldn’t need to do it in a hurry :slight_smile:

I am using V3 with .rules files and the global variables work just like normal. I use global variables to do time stamps. I have not tried the rules in a script stored in the json DB yet as I like doing multiple rules in the same file.

If your using the new ‘create equipment from thing’ feature to auto create the items, you need to go into the rules files and update what the items are now called in V3 as they are probably not the same as V2. That could be why they dont work for you?

That is weird. Must be something else that didn’t work then. I didn’t dig any deeper, just assumed that this was the problem when I didn’t see anything apparent. All the Things and Items are just upgraded from OH2, not being created in any fancy new way.

Actually, now I have completely re-made those rules, using a dedicated Item to keep track on whether the lights have already been turned on. Seemed like a good idea to go through all my rules and do them using the new shiny UI :sunglasses: