Getting Unit for an Event

Is there any way to get the Unit of an event?

For events that have the unit set Unit shows in the event log, but neither the event returned by

State currentState(String attributeName)

nor the events in the List returned by getCurrentStates has the unit property set, it is returned as null even though the event has a unit. Is there any other way/method for retrieving the unit of an event?

Anyone who knows how to retrieve the Unit from an Event?

If your app subscribes to the events from a device, you should be able to retrieve the unit as follows.

def eventHandler(evt) {
log.debug "The unit for this event: ${evt.unit}"
}

1 Like

Yes, thank you, that works, but how about historic events?

I am not sure. Perhaps a developer like @cobra or @bptworld would be able to offer better advice as they are well versed in Hubitat App development. You could also try tagging some of the Hubitat Team that are active here in the community.

You want to get an event object.. For historic events you can use:

events

Retrieve a list of events for the device. By default the maximum number of events returned in the list is 10 which can be overridden by the max option.

Signature

List<Event> events()

List<Event> events(Map options)

Parameters

options - Optional values for getting the list of events. Possible values:

max - The maximum number of events to retrieve.

Returns

List of Event objects for the device

eventsSince

Retrieve a list of events since a date/time.

Signature

List<Event> eventsSince(Date startDate)

List<Event> eventsSince(Date startDate, Map options)

Parameters

startDate - The date/time to list events since.

options - Optional values for getting the list of events. Possible values:

max - The maximum number of events to retrieve.

Returns

A list of Events (defaults to 10 events unless otherwise specified in options)

3 Likes

Thank you @bcopeland, I think I was unclear, what I really need is the latest value of all Current States, just like what getCurrentStates() provides, but with the actual Unit. Getting the unit works when subscribing to events and maybe with the events() and eventsSince() methods (I have not tried since this gives a list in a way I don't need). I could of course retrieve all the events and get the latest of each event in there, but that is an operation better suited for a DB query than filtering a Map, especially when doing this for many devices with a lot of events. I really only need the latest event of each event type.

So to try to be more concise in my question, is getCurrentStates() the only method that retrieves what I'm looking for? If so, why is the Unit always null? Is this a bug or by design?

1 Like

I'm not sure.. I haven't found (yet) a way to pull a single event object from a current attribute..

1 Like

Then the question is, why is Unit always null with getCurrentStates()? My feeling is that it is a bug, who in staff would know for certain?

currentState returns a state object not an event object..

@chuck.schwer would know for sure...

Yes, but that object also has the Unit attribute, albeit always set to null. Both objects represent similar information, if I get it as an Event object or a State object is not important to me in this particular instance.

:thinking: Ok.. Wasn’t aware that was there.. I’m going to test on something.. BRB

Here are the properties of the State object:

properties: name=temperature
properties: doubleValue=80.2
properties: numberValue=80.2
properties: id=null
properties: stringValue=80.2
properties: date=2020-05-11 19:10:09.487
properties: class=class com.hubitat.hub.domain.State
properties: unit=null
properties: dataType=NUMBER
properties: value=80.2
properties: jsonValue=80.2
properties: floatValue=80.2

Ok.. I have no valid answer for you.. and will let someone else jump in..

2 Likes

Its a bug

5 Likes

One that you will fix in a soon to come release?

1 Like

I just found out about it 3 minutes ago, I'm putting in a ticket for it, I do not know when it will be fixed.

2 Likes

Ok, fair enough, thank you :slight_smile: Any suggestions for a decently efficient workaround? If no, I'm sure I'll either live without the units or use what I can get in another slower way.

The only thing I can think of off the top of my head is to get the events instead of States and get the unit from there or subscribe to the event in question and get the unit from the event that comes in, then store that for use in the future. The device should not be changing units for each event so you should only need to get it once and use it going forward.

1 Like

Download the Hubitat app