Help with DateTimes

Hi
I want to initialize a DateTime variable with the current time at the start of my rules file to then use it in different rules.

What is the syntax?

Can you help me ?

Please do not reopen 6 year old threads with a reply that really isn’t even the topic of the original thread.

Please do open a new thread and provide more details including what rules language you are using and what you ahve tried.

Sorry but nothing indicated that the thread was closed!
I'm not very familiar with this type of site buI expected more understanding on an exchange and help site. I feel like I'm back at school.
THANKS

To make it a bit more clear and undestandable.
The original question here 6 years ago was about date-time formatting in a sitemap, but you want to do something completely differet as far as I understand. Thats why your question does not fit into this topic here.
This has nothing to do with being back to school.
Btw this topic was so old, that it unfortunately did not get the autoclose flag 4 weeks after last post. This was added later.

I understood well.
Not being a computer scientist  (I tinker a bit with the rules files in openHAB) I don't always understand what I find in relation to a problem.
Can you enlighten me on my question even if it is not asked in the right place?

Long story short, @Dom76 can you create a new ticket? Don’t forget to mention the rule language you use so the right people can provide support.

Best regards

Thank you for this feedback but I don't know the name of the language that I actually use. I'm a beginner. I create my rules in the .rules file located in /etc/openhab/rules
with “Visual Studio Code”
Here an extract from my file What is the scientific name of this language? Thanks
:

var Boolean bLock = false
var Timer timer = null
var mailActions = getActions(“mail”,“mail:smtp:533b13b712”)

rule “Task Startup

when

System reached start level 100

then

logInfo (“Tache StartUp”, “Fichier rules rechargé”)
sendBroadcastNotification(“Fichier Rules rechargé”)
sendCommand (Routine_iSalon,“reboot”)
sendCommand (Alarme_jardin,ON)
sendCommand (Annonce,ON)
end

rule “Automatisation A1 Mode vacance”

when

Item ModeVacance received update

then

if (ModeVacance.state == ON ){
sendBroadcastNotification(“Mode vacance activé”)
val success = mailActions.sendMail(“dominique.deiber@wanadoo.fr”, "Mode Vacance ", “Le mode vacance a été activé”)
logInfo (“Test mail”, "Test mail "+success)
sendCommand(autoWeeek,OFF)
sendCommand(autoSun,OFF)
sendCommand(autoSat,OFF)

} else {
sendBroadcastNotification(“Mode vacance désactivé”)
val success1 = mailActions.sendMail(“dominique.deiber@wanadoo.fr”, "Mode Vacance ", “Le mode vacance a été déactivé”)
logInfo (“Test mail”, "Test mail "+success1)
sendCommand(autoWeeek,ON)
sendCommand(autoSun,ON)
sendCommand(autoSat,ON)
}

end

Your posts are kind of formatted strangely. You should only use code fences when posting code.

```
code goes here
```

It appears you are using code fences for your text and not using it for the code which makes it hard to read.

You are using Rules DSL. I highly encourage you to take a look at Blockly instead. It’s going to be a much more forgiving environment, particularly if you are not already a proficient coder.

That’s simple enough and you are already doing basically the same thing already. The gotcha is I don’t think now exists when the .rules file is first loaded, only inside a rule. So you have three options (in order of preference):

Use the cache

rule “Task Startup”
when
    System reached start level 100
then

    ...
    sharedCache.put("startTime", now) // setting the time
end

rule “Automatisation A1 Mode vacance”
when
    Item ModeVacance received update
then
    ...
    logInfo("A1", sharedCache.get("startTime")) // accessing the time
    ...
end

Use your startup rule to initialize it

...
var ZonedDateTime startTime = null

rule “Task Startup”
when
    System reached start level 100
then
    ...
    startTime = now // setting the time
end

rule “Automatisation A1 Mode vacance”
when
    Item ModeVacance received update
then
    ...
    logInfo("A1", startTime) // accessing the time
    ...
end

Use the ZonedDateTime class instead of now

...
var loadTime = ZonedDateTime.now

rule “Automatisation A1 Mode vacance”
when
    Item ModeVacance received update
then
    ...
    logInfo("A1", loadTime) // accessing the time
    ...
end

But all of this looks a whole lot like an XY Problem. Why do you want the time that the .rules file was loaded in your rules?

Finally, do not save Actions as a global variable like this (I’m referring to mailActions). The lines of code that populate global variables like that only get executed when the .rules file is loaded. If the Mail Thing happend to not be ONLINE when the .rules file is loaded then the mailAction is never going to work until you reload the .rules file. If the Thing goes OFFLINE after the .rules file was loaded it’s going to stop working until you reload the .rules file. Always pull Thing actions inside the rule that uses it.

Thank you for all this information, it really enlightens me.