How to do a Unix timestamp in a variable in rm4.0?

Hi All
I am new at rm4.0 i am trying to get Unix timestamp in a variable
here what I have tried

I have look for an example for rm4.0 but could not find one
thanks for any help

You can set the variable to the current epoch time like this:

I used a number variable here, since the current time in milliseconds wound be a long (or whatever similar data type Hubitat/Groovy ends up using for this). I do not believe these options are available for strings.

However, if you share what you're trying to accomplish (with the automation as a whole, not this specific step), someone might have other suggestions for how to get there. Often when people ask for this, there is a way to use a "Delay" or "Wait" to get the same effect instead, which may be easier.

here the idea I have a power plug I can read power usage
if is above 200 watts I went to get time then when it drops to 0 get time again
then I like to subtract the to get how long it has run and put that on my dashboard

That should work: one variable for when it starts, one variable for when it stops, then do whatever math you need to get the number of minutes (e.g., (var2 - var1) / 60000 if you used milliseconds like I did above, which might be overkill for this situation). Store that in another variable, then get it into your Dashboard somehow. If you don't have any ideas for that last thing, a Virtual Ominsensor device would work: use the setVariable() command to set the value of its variable attribute to your total time, then include that virtual device on your Dashboard with the "Custom Attribute" template and choose the "variable" attribute.

In mostly-RM but part-pseudo-RM, here is more or less what the rule might look like:

Trigger events: Power *changes*

Actions to run:

IF (Power > 200 W AND Private Boolean is True) THEN
  Set variable time1 to current time in seconds
  Set Private Boolean False
ELSE-IF (Power < 1) THEN
  Set Private Boolean True
  Set variable time2 to current time in seconds
  Set variable time3 to (time2-time1)/60
  Set custom attribute on Dashboard tile device to time3 // e.g., virtual omnisensor and setVariable()

Just use a variable connector. Don't need a separate virtual device, and the setting of the device is automatic when the variable is set. Then use the created connector device in a Dashboard.

Hi @bertabcd1234
what is a Private Boolean?
now just figure out how set that all up
in the rm interface

It's a built-in boolean variable you get for "free" with every rule (you don't have to set it up, and its default value is true). You can manipulate it as I suggested above by using rule actions from the "Set Private Boolean, Run/Cancel/Pause Rules" category. Choose "This Rule" when prompted for which rule (which reveals another thing about Private Boolean--they belong to each individual rule, but other rules can manipulate the Private Boolean of a different rule, which can help when you need to find a create way to combine multiple rules related to the same automation).

ok made it to the next to the last step
i do not see a way to do
Set variable time3 to (time2-time1)/60
I tried math variable
i got

I may have oversimplified that step a bit. :slight_smile: I think you'll need another variable--do the above for time3, then create time4 and set it to time3/60.

ok here it is

I need more testing but I am thinking I may have to find a way to rest variables 0 between runs
also I get a - number so I have to look into that

You want time3 to be time2 - time1, not time1 - time2, which should fix the negative-value problem. For "resetting' the time, I'm not sure what would work for your use case, but a general idea that might work is to make time4 a global variable instead (perhaps with a better name since it's global), then use another rule to "reset" that variable to whatever you want based on whatever triggers and possibly conditional actions you want.

I have been rethinking this idea to make it simple
are there system variables that would give date and time that cold just be used on the dashboard?

You can set a time variable to a specific time, so one option would be setting one to the start time and another to the end time (via rule actions), then displaying them both on your Dashboard. As far as I can tell, I don't think you can do math with them to calculate the difference. They are also time only, not time and date. I don't think your original idea is overly complicated, despite the extra step it takes (owing to the fact that you can only use two operands at a time), unless you're no longer interested in displaying the elapsed time.

i went to keep it basic elapsed time did not come out to work as i had hoped
is there a system variable for current time and date?

I'm not 100% sure what you're asking, but as mentioned above, you can use a variable of type "time" and set it to any time, including the current time. For more on that, I'd play around with one or read:

So if you want a variable that represents the current time, you'll need to set one to the current time right before using it. If you want a string that represents the current time, then RM has one built in ("current" with respect to the time of the trigger event, not necessarily now but basically the same unless you have delays or waits): %time% and %date% are built-in string variables for that purpose.

Hi what i was hope for was a time and date like 9/25/2020 13:05:01 that i cold place on the dash board to no pump hand been on then one for off

Then the %date% and %time% variables built in might do what you want. Construct a string variable in your desired format using them. Here is a minimal example of just that step (and you can see the value at the bottom of my screenshot after running the actions just now):

Set myString to '%time% on %date%' rule action

But note again that this depends on your rule triggers, as these built-in variables refer to the time of the trigger event, not necessarily the current time. But if your rule triggers at the time when the device you're monitoring turns on or off, then they would be effectively the same thing, which would work (and it looks like it would if your rule still looks like the one above).

That what i been looking for is there a list of built in variables ?
is the one for a long time format ?

The list is here:

But to quote the relevant sentence for you:

When setting a String value, %device%, %value%, %time%, and %date% are available for most recent event for that rule

I'm not aware of anything that would get a long-format date. A custom app certain could format something in whatever way you want, but that might be overkill for this one particular situation.

Download the Hubitat app