JSON API: access by name instead of IDX

Use this forum to discuss possible implementation of a new feature before opening a ticket.
A developer shall edit the topic title with "[xxx]" where xxx is the id of the accompanying tracker id.
Duplicate posts about the same id. +1 posts are not allowed.

Moderators: leecollings, remb0

Post Reply
User avatar
heggink
Posts: 977
Joined: Tuesday 08 September 2015 21:44
Target OS: Raspberry Pi / ODroid
Domoticz version: 12451
Location: NL
Contact:

JSON API: access by name instead of IDX

Post by heggink »

Was this ever considered? I have a number of scripts running on different systems that update devices using json/curl. Enabling these to be updated by name is much more future proof if ever I need to switch devices, rebuild the system and so on...
Docker in Truenas scale, close to latest beta
DASHTICZ 🙃
RFXCOM, zwavejs2mqtt, zigbee2mqtt,
P1 meter & solar panel
Google home, Wifi Cams motion detection
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest, lots more :-)
u01pei

Re: JSON API: access by name instead of IDX

Post by u01pei »

I Don't know if it would be so easy to use the name in the api, I think you get some extra difficulties when your switch name has a space in it.
But fortunately there is a work-around, I've been using this for some years now and it works like a charm!
I use shell scripts to control my house and I have 1 script called Config.sh. It contains a lot of switches by name and every name has a value assigned to it (the idx).
like this:

Code: Select all

        idxBadkamerLicht="1178"
        idxBankLicht="714"
        idxEettafelLicht="676"
        idxHalLicht="674"
        idxKeukenLicht="687"
        idxOverloopLicht="1069"
        idxTuinPuiLicht="796"
        idxTuinSchuurLicht="805"
        idxTrapLED="1168"
        idxTVLicht="698"
        idxVoordeurLicht="675"
        idxwasmachine="1227"
        idxZolderLicht1="1066"
        idxZolderLicht2="1067"
It also contains most of the commonly used api calls as a function:

Code: Select all

# GET Status NON-Dimming Devices:
    # Format: DeviceStatus=$(GetStatus $idxDevice JQ_Search_Term)
GetStatus() {
curl -s "$Server/json.htm?type=devices&rid=$1" | jq '.result[0].'$2'' | tr -d '"'
}
In order to use this you create a script as you would usually do and add a line just below the shebang which refers to your config file:

Code: Select all

source /home/pi/scripts/Config.sh
Then as you write your script you can use the shortcuts that you have created.
The following will return the status of my kitchen lights as On or Off:

Code: Select all

Status=$(Getstatus $idxKeukenLicht Status)


It is also useful for a lot of other functions, like sending a telegram message:

Code: Select all

TelegramJeroen() {
curl -s --data chat_id=$JeroenID "https://api.telegram.org/bot$PiBot/sendMessage" --data-urlencode text="$@"
}
So instead of typing the stupid url's and remembering all the IDX numbers I use referrals and use a naming system that makes sense to me.
It makes script writing a lot easier and quicker! downside is that scripts can't be shared very easy (from me to others).
User avatar
heggink
Posts: 977
Joined: Tuesday 08 September 2015 21:44
Target OS: Raspberry Pi / ODroid
Domoticz version: 12451
Location: NL
Contact:

Re: JSON API: access by name instead of IDX

Post by heggink »

Whilst I like the approach, you would still need to know the idx of the device. Agree that you could just build a simple function that retrieves the idx from a json query on startup and use that going forward but that indeed is a workaround generating (useless) extra queries.
In addition, I 'repurposed' both bash and python scripts so I would need to build something for both occasions again duplicating effort.

Spaces in device names really should not be any issue since this has been around for ages and json calls can handle these.
Docker in Truenas scale, close to latest beta
DASHTICZ 🙃
RFXCOM, zwavejs2mqtt, zigbee2mqtt,
P1 meter & solar panel
Google home, Wifi Cams motion detection
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest, lots more :-)
zak45
Posts: 952
Joined: Sunday 22 January 2017 11:37
Target OS: Windows
Domoticz version: V2024.4
Contact:

Re: JSON API: access by name instead of IDX

Post by zak45 »

heggink wrote: Wednesday 16 May 2018 10:12 Whilst I like the approach, you would still need to know the idx of the device. Agree that you could just build a simple function that retrieves the idx from a json query on startup and use that going forward but that indeed is a workaround generating (useless) extra queries.
In addition, I 'repurposed' both bash and python scripts so I would need to build something for both occasions again duplicating effort.

Spaces in device names really should not be any issue since this has been around for ages and json calls can handle these.
Before that, 'unique device name' should be implemented.
User avatar
heggink
Posts: 977
Joined: Tuesday 08 September 2015 21:44
Target OS: Raspberry Pi / ODroid
Domoticz version: 12451
Location: NL
Contact:

Re: JSON API: access by name instead of IDX

Post by heggink »

Why? I would consider that end user responsibility and take the first hit...
Docker in Truenas scale, close to latest beta
DASHTICZ 🙃
RFXCOM, zwavejs2mqtt, zigbee2mqtt,
P1 meter & solar panel
Google home, Wifi Cams motion detection
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest, lots more :-)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: JSON API: access by name instead of IDX

Post by waaren »

zak45 wrote: Wednesday 16 May 2018 12:49
Before that, 'unique device name' should be implemented.
have you tried to add a unique index on DeviceStatus(Name) ?
Would be interesting to see if that break something.
Off course you have to delete double entries first and drop the index for a moment when you accept new hardware.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
morilm
Posts: 35
Joined: Friday 27 January 2017 12:57
Target OS: OS X
Domoticz version:
Contact:

Re: JSON API: access by name instead of IDX

Post by morilm »

why don't you use the

d_name = "device name"
d_idx = otherdevices_idx[d_name]

this way you can get always the corresponding IDX for a device and use it not only on JSON but on all scripts.
User avatar
heggink
Posts: 977
Joined: Tuesday 08 September 2015 21:44
Target OS: Raspberry Pi / ODroid
Domoticz version: 12451
Location: NL
Contact:

Re: JSON API: access by name instead of IDX

Post by heggink »

Getting back to this: why don;t I use that? Because I call domoticz from other machines. Would be great to do that using a name rather than a fixed idx. If you change a device (for whatever reason) then you need to go to all the external systems and adapt.
Docker in Truenas scale, close to latest beta
DASHTICZ 🙃
RFXCOM, zwavejs2mqtt, zigbee2mqtt,
P1 meter & solar panel
Google home, Wifi Cams motion detection
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest, lots more :-)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest