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.
Post Reply
User avatar
heggink
Posts: 455
Joined: Tuesday 08 September 2015 21:44
Target OS: Raspberry Pi
Domoticz version: V3.9530
Location: NL
Contact:

JSON API: access by name instead of IDX

Post by heggink » Monday 14 May 2018 20:57

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...
Pi3, latest beta
RFXCOM, z-wave (AEOTEC, switches, temhum, pir, contacts),
Plugwise2py, P1 'smart'meter & solar panel
Alexa, Wifi Cams motion detection
ESP: relays, PIR & Temp/TempHum
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest

u01pei
Posts: 47
Joined: Sunday 04 January 2015 17:17
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: JSON API: access by name instead of IDX

Post by u01pei » Monday 14 May 2018 21:22

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: 455
Joined: Tuesday 08 September 2015 21:44
Target OS: Raspberry Pi
Domoticz version: V3.9530
Location: NL
Contact:

Re: JSON API: access by name instead of IDX

Post by heggink » 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.
Pi3, latest beta
RFXCOM, z-wave (AEOTEC, switches, temhum, pir, contacts),
Plugwise2py, P1 'smart'meter & solar panel
Alexa, Wifi Cams motion detection
ESP: relays, PIR & Temp/TempHum
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest

zak45
Posts: 503
Joined: Sunday 22 January 2017 12:37
Target OS: Windows
Domoticz version: V3.9056
Contact:

Re: JSON API: access by name instead of IDX

Post by zak45 » Wednesday 16 May 2018 12:49

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: 455
Joined: Tuesday 08 September 2015 21:44
Target OS: Raspberry Pi
Domoticz version: V3.9530
Location: NL
Contact:

Re: JSON API: access by name instead of IDX

Post by heggink » Wednesday 16 May 2018 17:23

Why? I would consider that end user responsibility and take the first hit...
Pi3, latest beta
RFXCOM, z-wave (AEOTEC, switches, temhum, pir, contacts),
Plugwise2py, P1 'smart'meter & solar panel
Alexa, Wifi Cams motion detection
ESP: relays, PIR & Temp/TempHum
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest

User avatar
waaren
Posts: 603
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: JSON API: access by name instead of IDX

Post by waaren » Wednesday 16 May 2018 18:06

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.
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave , dzVents 2.4.n, Amazon echo

morilm
Posts: 31
Joined: Friday 27 January 2017 13:57
Target OS: OS X
Domoticz version:
Contact:

Re: JSON API: access by name instead of IDX

Post by morilm » Thursday 24 May 2018 20:29

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests