Current States vs State Variables in a Device Driver

I may (or may not) be having issues with reading Attributes (Current States) shortly after calling the sendEvent to write the attribute. Is the way to typically solve this to store the value in a State Variable instead / in addition to an attribute?

My situation: This is my Unified Thermostat driver for Mitsubishi units, particularly those in Aus/NZ in this case. The calls come from outside in an RM rule to adjust the thermostat operating state, e.g. off -> heating, then shortly after adjusting the set point temperature, where I want to be able to read the current operating state, but I suspect it is not reading the value that was most recently set for the attribute.

I want to do some more testing / logging, but thought it would be good to also prompt this discussion at the same time.

Could try device.currentValue('attributeName', true) to force a read from the database. Writing to state may not be available fast enough, and a driver can't use atomicState.

3 Likes

Is the issue not that the writing of the values are not done immediately, i.e. the issue is not necessarily at the reading end?

State values generally get written to the database at the end of the transaction session, the event will normally get written fairly quickly, but the default for currentValue is to read from the cache which tends to lag slightly behind the actual recording of the value. Using the force database parameter normally gets around this (use it a lot in the Hub Information driver).

1 Like

ok, thanks, I'll give it a go.

I ran into this inconsistency in one of my drivers early on and used device attributes instead at that time. I’ve since learned about singleThreaded : true and use that instead and have not had that issue recur with the immediate reading of state variables right after they have been set.

Single threading ensures everything is sequential. Multi threading does some stuff in parallel which is where the inconsistency stems.

3 Likes

Interesting, I'll take a look at that one. Had never heard of that option before. Thanks for the tip.

I use it in all of my Zigbee. drivers, although I am not sure on the effect (if any)

1 Like