How to get/retrieve the "Unit" value from the sendEvent command

In an device driver, how can I get/retrieve the "Unit" value from the sendEvent command?

sendEvent(name: "temperature", value: 67.3, unit: "F")

Edit: Change question to a device driver and not an App.


device.currentState(temperature)?.unit -- or -- device.currentState("temperature")?.unit

This is not working for me. Both are returning null

Do you have a valid device handle?

I think? In the original message I am sending it a "F". Is that wrong?

If you are sending an event from an app, what device are you using the store the attribute? Generally your code will look something like:

chdDevice.sendEvent(name: "temperature", value: 67.3, unit: "F")
unitValue = chdDevice.currentState("temperature")?.unit

This is for the Weather Underground device driver, not an app. Sorry.

unitValue = device.currentState("temperature")?.unit

should work if the device is querying it's own attributes. May need to give the event some time to record though. Could also try

unitValue = device.currentState("temperature",true)?.unit

to force a read from the database instead of cache.

That works. Thanks!

1 Like