JSON to query device by name

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
kilbukas
Posts: 4
Joined: Friday 23 December 2016 19:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

JSON to query device by name

Post by kilbukas »

Hello All,

i have looked for the feature, but have failed to find. Pls, excuse me, if something similar exists.

Is it possible to extend JSON processing to query device information name, e.g. the following query

Code: Select all

http://domoticz:8080/json.htm?type=devices&name=Temp_test1
could respond with

Code: Select all

{

    "ActTime": 1474795292,
    "ServerTime": "2016-12-23 12:21:32",
    "Sunrise": "07:11",
    "Sunset": "19:08",
    "result": [
        {
            "AddjMulti": 1.0,
            "AddjMulti2": 1.0,
            "AddjValue": 0.0,
            "AddjValue2": 0.0,
            "BatteryLevel": 255,
            "CustomImage": 0,
            "Data": "0.0 C",
            "Description": "",
            "Favorite": 0,
            "HardwareID": 2,
            "HardwareName": "test",
            "HardwareType": "Dummy (Does nothing, use for virtual switches only)",
            "HardwareTypeVal": 15,
            "HaveTimeout": false,
            "ID": "14053",
            "LastUpdate": "2016-12-23 12:21:13",
            "Name": "Temp_test1",
            "Notifications": "false",
            "PlanID": "0",
            "PlanIDs": [
                0
            ],
            "Protected": false,
            "ShowNotifications": true,
            "SignalLevel": "-",
            "SubType": "LaCrosse TX3",
            "Temp": 0.0,
            "Timers": "false",
            "Type": "Temp",
            "TypeImg": "temperature",
            "Unit": 1,
            "Used": 1,
            "XOffset": "0",
            "YOffset": "0",
            "idx": "4"
        }
    ],
    "status": "OK",
    "title": "Devices"
}
this would make IoT devices to find respective Idx easier.

As far as i understand current JSON solution can query/filter devices by type. However this could lead to quite big response, which would require far more processing in IoT device, hence more energy consumption, than with proposed solution.

Thank you
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: JSON to query device by name

Post by Egregius »

You're IDX should remain the same, so you only need to set it once.

A workaround could be to query the api during startup of the device and store the IDX in a file or memory.
kilbukas
Posts: 4
Joined: Friday 23 December 2016 19:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: JSON to query device by name

Post by kilbukas »

Thanks for the answer. That is exactly the intention, to read Idx on the first access and then store it for subsequent use.

Then the question is, how would you suggest to query the api?
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: JSON to query device by name

Post by Egregius »

In the __CRON.php script on my github there's an example how I store idx, status and timestamp in cache for all used devices in one run.
kilbukas
Posts: 4
Joined: Friday 23 December 2016 19:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: JSON to query device by name

Post by kilbukas »

Thanks a lot for pointing it out. too bad, such php scripts are beyond my comprehension
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: JSON to query device by name

Post by Egregius »

You only need this part of it:

Code: Select all

$domoticz=json_decode(file_get_contents('http://127.0.0.1:8084/json.htm?type=devices&used=true'),true);
    if($domoticz){
        foreach($domoticz['result'] as $dom){
            $name=$dom['Name'];
            $type=$dom['Type'];
            if(isset($dom['SwitchType']))$switchtype=$dom['SwitchType'];else $switchtype='none';
            apcu_store('t'.$name,strtotime($dom['LastUpdate']));
            apcu_store('i'.$name,$dom['idx']);
            if($type=='Temp')apcu_store('s'.$name,str_replace(' C','',$dom['Data']));
            elseif($switchtype=='Dimmer'){
                if($dom['Data']=='Off')apcu_store('s'.$name,'Off');
                else apcu_store('s'.$name,filter_var($dom['Data'],FILTER_SANITIZE_NUMBER_INT));
            }
            else apcu_store('s'.$name,$dom['Data']);
        }
        usleep(10000);
    }
 
kilbukas
Posts: 4
Joined: Friday 23 December 2016 19:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: JSON to query device by name

Post by kilbukas »

Thank you very much, this has helped me out.
Now I see the idea - to use PHP to process JSON to give better response to IoT device. It has helped me on the other topic as well - Iot device time synchronization with domoticz server.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest