We have moved at community.getvera.com

Author Topic: Remove user variables from a device  (Read 10880 times)

Offline usmampoer

  • Newbie
  • *
  • Posts: 16
  • Karma: +0/-0
Remove user variables from a device
« on: October 31, 2011, 10:56:57 am »
I have some code that will add a new variable to a device when I need to track specific information like the time that a door has been opened.

I create the variable using a luup.variable_set("urn:upnp-org:serviceId:HVAC_UserOperatingMode1", "UserState", "", 19)

Is there a way to remove these variables from the device when they are no longer needed...

Andre

Offline futzle

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3260
  • Karma: +192/-9
Re: Remove user variables from a device
« Reply #1 on: October 31, 2011, 04:03:22 pm »
Is there a way to remove these variables from the device when they are no longer needed...

No, so they're going to start cluttering up your device's Advanced tab over time.

That also means that you need to nominate a value (empty string, or "0" or something else reasonable) that stands in for "deleted", and when you read the variable check against this value as well as nil.

Offline usmampoer

  • Newbie
  • *
  • Posts: 16
  • Karma: +0/-0
Re: Remove user variables from a device
« Reply #2 on: November 01, 2011, 09:06:13 am »
Thanks for the reply, would it be reasonable to expect that the development team will consider this as a future "enhancement".

If it is that easy to add a variable should be just as easy to remove it from the data table


Offline mcvflorin

  • Administrator
  • Hero Member
  • *****
  • Posts: 1755
  • Karma: +11/-3
Re: Remove user variables from a device
« Reply #3 on: November 01, 2011, 10:31:09 am »
Removing a variable is possible, but relatively complicated and dangerous.

http://wiki.micasaverde.com/index.php/ModifyUserData
http://forum.micasaverde.com/index.php/topic,6277.0.html

Offline usmampoer

  • Newbie
  • *
  • Posts: 16
  • Karma: +0/-0
Re: Remove user variables from a device
« Reply #4 on: November 01, 2011, 04:53:53 pm »
Many thanks but not sure I have enough postings for me to try this......

I will wait for Micasaverde to offer this a new "feature" in U15+

Offline usmampoer

  • Newbie
  • *
  • Posts: 16
  • Karma: +0/-0
Re: Remove user variables from a device
« Reply #5 on: November 02, 2011, 09:56:20 am »
Thought about a workaround.....

Create a user plugin and use this to hold the various user created variables, does add some complexity to make sure that the naming conventions are good to "find" the correct variable for a specific device, might need to think of using the device name + device ID to create the variable. not yet sure how do that with string conjugation. and might not even be possible with luup.

Is it possible to create a variable name using something like ...

local name = "Sensor"+tostring(v)
luup.variable_set(SS_SID, name, somedata, v)

(where v is the device ID)

So if the variables get out of hand you should be able to delete the device and recreate the device. This will delete all user variables. In my case this is not a big issue I do not store long term variable so I will not lose valuable data, but it also smacks of bad programming techniques.


Offline utz

  • Sr. Member
  • ****
  • Posts: 274
  • Karma: +0/-0
delete variables
« Reply #6 on: November 14, 2011, 05:38:46 am »
Is there a way to delete variables stored in a name space? For example, I can use the UI to create a new variable (the advanced tab). Is there a way to delete some? I know I can delete the whole device and create it again but I do not want to do that.

Offline mcvflorin

  • Administrator
  • Hero Member
  • *****
  • Posts: 1755
  • Karma: +11/-3
Re: delete variables
« Reply #7 on: November 14, 2011, 06:26:40 am »
Theoretically it's possible using ModifyUserData, but it's relatively complicated and risky. I haven't tried it so I don't know if it works or not.

Offline utz

  • Sr. Member
  • ****
  • Posts: 274
  • Karma: +0/-0
Re: Remove user variables from a device
« Reply #8 on: November 15, 2011, 05:53:30 am »
ok, that looks complicated. I assume there is no easier way (e.g. edit a file/database on vera directly where the stuff is stored?).

Offline mcvflorin

  • Administrator
  • Hero Member
  • *****
  • Posts: 1755
  • Karma: +11/-3
Re: Remove user variables from a device
« Reply #9 on: November 15, 2011, 09:43:25 am »
You could uncompress and edit /etc/cmh/user_data.json.lzo.

Offline Ap15e

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1998
  • Karma: +12/-0
Re: Remove user variables from a device
« Reply #10 on: November 15, 2011, 01:02:51 pm »
... or write a Luup plugin, that removes variables (VRD - Variable Remover Device) - but joking aside: is there a specific reason why removing variables isn't implemented in MiOS?

Offline mcvflorin

  • Administrator
  • Hero Member
  • *****
  • Posts: 1755
  • Karma: +11/-3
Re: Remove user variables from a device
« Reply #11 on: November 15, 2011, 01:44:42 pm »
is there a specific reason why removing variables isn't implemented in MiOS?

Maybe it's harder to implement and it's not worth allocating time for it? We'll never know... though a Luup plugin that does this is possible. Any volunteers? :)

« Last Edit: November 17, 2011, 06:03:44 am by mcvflorin »

Offline utz

  • Sr. Member
  • ****
  • Posts: 274
  • Karma: +0/-0
Re: Remove user variables from a device
« Reply #12 on: November 16, 2011, 06:53:35 am »
You could uncompress and edit /etc/cmh/user_data.json.lzo.

Ok. So I could uncompress and edit. After edit do I have to restart some services? Or reboot? Can I copy the old file in place as a backup in case I make a mistake and destroy the whole system? How save is this operation?

In general, I would say you could just add a variable delete button to the advanced tab (I know, you probably don't like the idea because you would have a lot of dopes that delete variables and then complain that the system is not working)

Offline mcvflorin

  • Administrator
  • Hero Member
  • *****
  • Posts: 1755
  • Karma: +11/-3
Re: Remove user variables from a device
« Reply #13 on: November 17, 2011, 06:37:48 am »
Ok. So I could uncompress and edit. After edit do I have to restart some services? Or reboot? Can I copy the old file in place as a backup in case I make a mistake and destroy the whole system? How save is this operation?

Follow these steps:
  • Stop LuaUPnP:
    /etc/init.d/cmh stop
  • Make a backup of you current user_data (/etc/cmh/user_data.json.lzo)
  • Uncompress user_data
    pluto-lzo d /etc/cmh/user_data.json.lzo /tmp/user_data.json
  • Remove the variables you want (see how variables look in the user_data in the code below)
  • Validate your new user_data on http://jsonlint.com
  • Compress the new user_data
    pluto-lzo c /tmp/user_data.json /etc/cmh/user_data.json.lzo
  • Start LuaUPnP
    /etc/init.d/cmh start
I'd say that the most important steps are to make a backup of the old user_data and to check your new user_data on http://jsonlint.com. If these steps are made, then the whole operation is pretty safe.

Code: [Select]
"states": [
{
"service": "urn:micasaverde-com:serviceId:VistaAlarmPanel1",
"variable": "PluginVersion",
"value": "2.32",
"id": 0
},
{
"service": "urn:micasaverde-com:serviceId:VistaAlarmPanel1",
"variable": "DebugMode",
"value": "1",
"id": 1
},
{
"service": "urn:micasaverde-com:serviceId:VistaAlarmPanel1",
"variable": "NumPartitions",
"value": "0",
"id": 2
},
{
"service": "urn:micasaverde-com:serviceId:HaDevice1",
"variable": "CommFailure",
"value": "1",
"id": 3
}
],

Offline wezzix

  • Sr. Newbie
  • *
  • Posts: 36
  • Karma: +2/-0
Re: Remove user variables from a device
« Reply #14 on: September 21, 2012, 07:02:34 am »
The above steps works fine, apart from the validator that times out in my FF browser.
This link is more stable and provides option to format the json for easier reading: http://jsonformatter.curiousconcept.com/
Voting +1 for a convenience method to delete variables, like so: luup.variable_delete(service, name, device_id)
Thanks.