JSON API: access by name instead of IDX
Moderators: leecollings, remb0
- 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
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 :-)
DASHTICZ 🙃
RFXCOM, zwavejs2mqtt, zigbee2mqtt,
P1 meter & solar panel
Google home, Wifi Cams motion detection
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest, lots more :-)
Re: JSON API: access by name instead of IDX
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:
It also contains most of the commonly used api calls as a function:
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:
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:
It is also useful for a lot of other functions, like sending a telegram message:
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).
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"
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 '"'
}
Code: Select all
source /home/pi/scripts/Config.sh
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="$@"
}
It makes script writing a lot easier and quicker! downside is that scripts can't be shared very easy (from me to others).
- 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
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.
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 :-)
DASHTICZ 🙃
RFXCOM, zwavejs2mqtt, zigbee2mqtt,
P1 meter & solar panel
Google home, Wifi Cams motion detection
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest, lots more :-)
-
- 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
Before that, 'unique device name' should be implemented.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.
- 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
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 :-)
DASHTICZ 🙃
RFXCOM, zwavejs2mqtt, zigbee2mqtt,
P1 meter & solar panel
Google home, Wifi Cams motion detection
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest, lots more :-)
- 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
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
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Re: JSON API: access by name instead of IDX
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.
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.
- 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
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 :-)
DASHTICZ 🙃
RFXCOM, zwavejs2mqtt, zigbee2mqtt,
P1 meter & solar panel
Google home, Wifi Cams motion detection
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest, lots more :-)
Who is online
Users browsing this forum: No registered users and 1 guest