Custom fields for devices

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

Post Reply
juankar
Posts: 221
Joined: Saturday 30 August 2014 20:20
Target OS: Linux
Domoticz version: 4.
Location: Spain
Contact:

Custom fields for devices

Post 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
User avatar
gizmocuz
Posts: 2350
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: Custom fields for devices

Post 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 ;)
Quality outlives Quantity!
juankar
Posts: 221
Joined: Saturday 30 August 2014 20:20
Target OS: Linux
Domoticz version: 4.
Location: Spain
Contact:

Re: Custom fields for devices

Post 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.
juankar
Posts: 221
Joined: Saturday 30 August 2014 20:20
Target OS: Linux
Domoticz version: 4.
Location: Spain
Contact:

Re: Custom fields for devices

Post 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
edgarhildering
Posts: 31
Joined: Thursday 11 June 2015 22:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Contact:

Re: Custom fields for devices

Post 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
Attachments
optionstools.zip
(1.55 KiB) Downloaded 8 times
RaspberryPi running Domoticz 2023.2
PIRs + lightswitches
thermometers + heating control
hygrometers + dehumifiers
TUYA switches
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest