Saving Date/Time from a device to a Variable in RM

@bravenel, I have found that I am unable to save a Date/Time value coming from a device to a Variable in Rule Machine. I haven’t tried doing this before, so maybe there’s a way and I’m not aware of how to do this yet…

Otherwise, is this something that could be added?

In the screenshot below, it is possible to see that the option to save from a device’s attribute is not available when using a date/time variable:

It is however possible when using another type of variable, line a number variable as an example:

Why am I asking for this?
I have created a rule that will adjust the charging amperage of my vehicle based on the amount of over-production from my solar array.

I want to change this value (add or take away from it) based on what has it has been set to previously. This data can come from either the device itself (best case) or the rule’s previously set value. I want to compare the last polling update of the device (which is stored as a state) to the value in a variable where I store the last time the rule ran.

The driver I use to connect to the vehicle polls it to get theirs data and I want to minimize the polling. I could forgo this by polling more often, but I would prefer to avoid this as much as possible. The vehicle will on occasion change the value, so I can not always rely on the value I stored in the app.

Here is a screenshot taken from the device’s states showing this date/time:

In general, drivers don't support date and time attributes. Having this attribute in a custom driver doesn't mean the date time value conforms to that which is required for a DateTime variable. So it doesn't make sense to open this particular can of worms since it probably would not work, but creates some expectation that it would.

I think what you would need to do is use a DateTime variable connector, and set that with the setDateTime() command, passing a properly formatted string.

Screenshot 2023-07-20 at 10.06.13 AM

You can see there the correct string format for this date/time:

Screenshot 2023-07-20 at 10.07.39 AM

This is the only mechanism I can think of that is going to allow you to go from a string (e.g. "2023-07-20T10:41:15.000-007") to a DateTime variable. Rule Machine does not have a method to do this, but a small custom app could do it.

1 Like

Thanks! I’ll see if I can work with other community members to design such an app. (I’ve tinkered with some drivers, but never any apps yet.)

Just tell me what the trigger would be (what device attribute), and I'll post a skeleton app for you. Not much to it.

Also, show me what format the lastUpdate or lastTokenUpdate attribute has internally in the driver. Is it a string as shown in your Current States?

1 Like

Much appreciated!

Ideally, I would want to trigger the app with a change to the lastUpdate value, but it could also be triggered by a button press or switch update event if that’s easier to do.

The lastUpdate attribute is indeed a string:

attribute "lastUpdate", "string"

Not sure if it makes a difference, but this is how it gets populated in the driver:


def initialize() {
…
    sendEvent(name: "lastUpdate", value: now, descriptionText: "Last Update: $now")
…

In case it is of any use, this is the driver I am going to be looking for a date from:

Since the attribute is just set to now, all you have to do is trigger the rule from that attribute changing, and set the DateTime variable to now. You don't need to do anything with the actual attribute value, since you know exactly what it is (plus a couple hundred milliseconds).

4 Likes

Oh - of course! That is just too easy! Funny how the simplest solutions evade us sometimes.

Thank you!

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.