Page 1 of 1

JSON API: access by name instead of IDX

Posted: Monday 14 May 2018 20:57
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...

Re: JSON API: access by name instead of IDX

Posted: Monday 14 May 2018 21:22
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).

Re: JSON API: access by name instead of IDX

Posted: Wednesday 16 May 2018 10:12
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.

Re: JSON API: access by name instead of IDX

Posted: Wednesday 16 May 2018 12:49
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.

Re: JSON API: access by name instead of IDX

Posted: Wednesday 16 May 2018 17:23
by heggink
Why? I would consider that end user responsibility and take the first hit...

Re: JSON API: access by name instead of IDX

Posted: Wednesday 16 May 2018 18:06
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.

Re: JSON API: access by name instead of IDX

Posted: Thursday 24 May 2018 20:29
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.

Re: JSON API: access by name instead of IDX

Posted: Tuesday 14 April 2020 12:24
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.