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?
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:
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?
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.
Ok, fair enough, thank you 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.