Page 1 of 1

Custom fields for devices

Posted: Friday 17 June 2016 2:15
by juankar
Is there any way to add a new field to a device using JSON?
For example, I've saw that devivesStatus contains a column named Options. I can fill this field for any device using json, but I can read this value in the smae way (with JSON). I wonder if I coul add this field so it can be retrieved using JSON.
Even I've created a field in dialog window of edit device and I can fill the column Options from the GUI, that works fine, but when I try to edit the device I can't see the stored value of option. I think that fields of edit device form are retrieved via JSON, but It doesn't read the column options.

Thanks

Re: Custom fields for devices

Posted: Friday 17 June 2016 8:37
by gizmocuz
Probably there is, as when you add a dummy sensor, and press the 'create virtual sensor' button, this also calls a json API to create it.
So... Open your browsers developer console, log the network traffic, and you know ;)

Re: Custom fields for devices

Posted: Friday 17 June 2016 10:18
by juankar
Not so easy :cry:
I am debugin the utility,html and I can't find the key. I've found a function that creates the dialog box for edit the device and the arguments are sent as item.idx... and so. Now I need to find where thes item is built.

Re: Custom fields for devices

Posted: Friday 17 June 2016 13:14
by juankar
I think the question is: if when dialog box for editenergydevice (this is the kind of device I'm working on) is built, data are read from database or from device (through OZwave)?
I mean: when I edit a device I see the dialog with stored device name, description, type... , are these values retrieved from database (devicestatus) or from device (data stored in devices)?

Thanks

Re: Custom fields for devices

Posted: Tuesday 10 October 2023 16:10
by edgarhildering
Hi Juankar,
Even though this is a somewhat older post, I still have an answer.

The options field consists of a chain of keyword=value pairs, separated by ';'. For example, for a selector switch you will find the following content in the DeviceStatus table, options column:

Code: Select all

LevelActions:fHx8;LevelNames:T2ZmfERpc2NoYXJnZXxDaGVja3xDaGFyZ2U=;LevelOffHidden:ZmFsc2U=;SelectorStyle:MA==
The keys are readable but the values ​​are not. That's because the values ​​are b64 encoded.
the good news is that I wrote two small tools to read the option field and write it back:
  • getoptions.py -i <idx> retrieves the options field from the specified index and leaves it in the file: data.json
  • setoption.py reads the file, constructs the optionsfield and writes it back to the database. No parameter is needed because the IDX is stated in the file.
so, after ./getoptions.py -i 1987 de contents of data.json is:

Code: Select all

{
    "id": 1987,
    "name": "WWH-living-room-heater-state",
    "options": {
        "LevelActions": "|||",
        "LevelNames": "Off|Discharge|Check|Charge",
        "LevelOffHidden": "false",
        "SelectorStyle": "0"
    }
}
You can edit this file with nano or vi: adjust, add keywords and values, etc. As an example, I add a keyword 'watchdogminutes' with value 15: this device must be updated at least every 15 minutes.

Code: Select all

{
    "id": 1987,
    "name": "WWH-living-room-heater-state",
    "options": {
        "LevelActions": "|||",
        "LevelNames": "Off|Discharge|Check|Charge",
        "LevelOffHidden": "false",
        "watchdogminutes": "15",
        "SelectorStyle": "0"
    }
}
After ./setoptions.py the data is written back. (yes, you are allowed to change the idx. And no, the name field is never written back)

The strong point of the option field is that all keyword=value pairs placed here will also be mentioned in MQTT messages (domoticz/out). An external process subscribed to 'domoticz/out' can now take care of this. If the status of the device changes, this is the corresponding MQTT message:

MQTT:

Code: Select all

{
	"Battery" : 255,
	"LastUpdate" : "2023-10-10 15:40:16",
	"LevelActions" : "|||",
	"LevelNames" : "Off|Discharge|Check|Charge",
	"LevelOffHidden" : "false",
	"RSSI" : 12,
	"SelectorStyle" : "0",
	"description" : "",
	"dtype" : "Light/Switch",
	"hwid" : "2",
	"id" : "00014813",
	"idx" : 1987,
	"name" : "WWH-living-room-heater-state",
	"nvalue" : 0,
	"stype" : "Selector Switch",
	"svalue1" : "0",
	"switchType" : "Selector",
	"unit" : 1,
	"watchdogminutes" : "15"
}
be my guest and use the little tools. A small word of warning: managing the database is called 'a back door to domoticz' and should be avoided when possible...

kind regards and good luck, --Edgar