Persistent custom states

Posted on
Mon Jul 26, 2021 1:54 pm
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Persistent custom states

Take the case of a product that supports analog inputs from a variety of 0 to 5v sensors. The input voltage may have a variety of meanings depending on the sensor's function. For example, they could measure light, current, humidity, temperature, pressure, etc. Further, assume this device can report the actual measured voltage and also a meaningful number related to the input sensor's purpose

Now, assume I have built a custom indigo plugin device for this device and defined 2 states in Devices.xml: voltage and sensorValue. The voltage level is placed in voltage and whatever the sensor is actually measuring goes into sensorValue. So far, so good.

Now, for clarity, I would like one additional state. It will have the same value as sensorState, but with a meaningful name. Like humidity of temperature.

Right now I create these states on the fly as discussed in this post from 2014. That works. But, it has one drawback. Sometimes as the device is being instantiated in Indigo, like on server startup or plugin restart, a sensor measurement is received before the new state is available, so the updateStateOnServer attempt throws an Indigo error. The problem is, of course, short-lived. Note, other than the difference of this single state, all of the Indigo devices created for this product are identical.

So, my question is... Is there a way to create the new state so that it is persistent once it has been created (I.e. it survives plugin restarts)? Or, is there some other approach to this issue?

BTW, Yes, I know I can try to fine-tune the timing, but, a lot depends on external code over which I have no control.

Posted on
Mon Jul 26, 2021 2:48 pm
FlyingDiver offline
User avatar
Posts: 7210
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Persistent custom states

In one of my plugins, when the state list is determined, I save the list:

Code: Select all
        new_states = {}               
        state_list = []

        query = parse_qs(request.query)
        for key in query:
            value = query[key][0]
            state_list.append({'key': unicode(key), 'value': value})
            new_states[key] = value
 
        if saved_states != new_states:
            newProps = device.pluginProps
            newProps["saved_states"] = json.dumps(new_states)
            device.replacePluginPropsOnServer(newProps)



In getDeviceStateList(), I retrieve the list of the current states for the device:

Code: Select all
        saved_states = json.loads(device.pluginProps.get("saved_states", "{}"))
        for key in saved_states:
            dynamic_state = self.getDeviceStateDictForStringType(unicode(key), unicode(key), unicode(key))
            state_list.append(dynamic_state)

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Wed Jul 28, 2021 12:30 pm
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: Persistent custom states

Thanks for that idea. I think it will work for my needs.

Posted on
Wed Jul 28, 2021 1:35 pm
jay (support) offline
Site Admin
User avatar
Posts: 18212
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Persistent custom states

I think that's also what I did in the WeatherSnoop plugin, though I turned that over to Boisy so long ago I could be dreaming it... :wink:

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests

cron