Using webCoRE to create an interface to another unsupported hub system (QwikSwitch) / Parsing data


#1

I started off by asking about creating an interface to/smart app for a system called Qwikswitch on the ST forum. The discussion turned towards webCore, so I thought I should continue the discussion here. (I hope this is an acceptable way of doing that.)

I currently use GET requests linked to virtual switches in a piston to control the Qwikswitch devices. The Qwickswitch API is available here. I however also use time triggers in pistons to turn lights on/off. The problem is that the Virtual Switch state and the actual device switch are most of the time out of sync and I thought there could be an easy way to create a smart app to correct this.

@Ryan780 suggested that I enhance my piston to update the Virtual Device state. He suggested using $args to obtain the device status:
image

I created a piston but I could not get the {args.FROMCAMP1} (as suggested by him) to return a result other than “null”. Don’t know if I’m being stupid and perhaps had to change the FROMCAMP1 part?

Looking at other pistons example I tried the $response variable instead. Running the GET request http://qwikswitch.com/api/v1/control/?device=@&setlevel=100 to turn a switch on, produces the following result:
[level:100, device:@1111, success:true]

My issue is that this result (in respect of the particular device) is only produced following a GET request to turn the device on/off.

According to my understanding of the API, the only way to get the state of a device, is via this GET request: https://qwikswitch.com/api/v1/state/:API_KEY/, which returns the status of all devices:

Calculating (string) @1111:[firmware:2b, epoch:1518243799, value:0, rssi:65%, type:RELAY QS-R-S5], @2222:[firmware:32, epoch:1518243618, value:0, rssi:45%, type:RELAY QS-R-S5], @3333:[firmware:2b, epoch:1518243798, value:0, rssi:62%, type:RELAY QS-R-S5], @4444:[firmware:2b, epoch:1518243796, value:0, rssi:59%, type:RELAY QS-R-S5], @5555:NO_REPLY, @207100:[firmware:40, epoch:1518243926, value:0, rssi:52%, type:RELAY QS-R-S30-SPEC] truncated

The device ID of each device follows after the “@” (which I have changed in the result above for security concerns). The state of the device is indicated by a 0 or 1 following “value:” I don’t know what “epoch” is.
I understand that what I need to do to get the status of a particular device, is referred to as parsing? My questions are therefore:
How do I parse the result of the state GET request to a variable which only contains the status of a particular device? My understanding is that I should look for the Device ID within the result of the GET request and then in some or other way, look what the value of “value” is immediately following the device’s ID?
I suppose once I have that information, I will use IF contents of variableContainingStateOfDevice555= 1, turn virtualSwitch555 On?
There will obviously be a statement in my piston IF virtualSwitch555 changes to On, run GET request to turn on Device555. If Device555 was turned on by a GET request from a piston, virtualSwitch555 will show as being Off, until the state statement of the piston turns it On to reflect the true state of Device555. The piston will also contain a statement IF virtualSwitch555 turns on, run GET request to turn on Device555. How will I prevent a status update to run this command? Or should I just live with that (since a GET request to turn on a device which is already on, will do nothing.) I’m just worried about the amount of traffic I’m going to generate on the system.


#2

As I told you, FROMCAMP1 wasn’t what you were supposed to use but was instead a placeholder for the argument that would be returned from your other system. it appears that you have all the data you need. It appears that your devices are @1111 and @2222 and @3333 and @4444 and the value of 0 or 1 will tell you whether it’s on or not. Have you tried turning a device on and seeing what your Get request returns?

Also, this is a forum. I don’t get paid by Samsung or WebCore or anyone else. So, anything I give you is to help. I’m not going to spend hours reading the documentation to an API that I will never use. So, the answers your are going to get, unless that person uses the same system, will help you to figure it out for yourself. If you want someone to figure it out for you, then hire a consultant.


#3

Are you sure this wouldn’t be more appropriate for what you’re looking to do? It looks like it’s device specific…

http://qwikswitch.com/doc/#api-History-GetHistory


#4

Thank you, will try that.


#5

I wonder if someone could help me to extract the status of each device from the following response I get when I send a GET request to the QwikSwitch hub.

[@0ce830:[firmware:2b, epoch:1520587216, value:0, rssi:64%, type:RELAY QS-R-S5], @224860:[firmware:32, epoch:1520587493, value:0, rssi:54%, type:RELAY QS-R-S5], @0c8c30:[firmware:2b, epoch:1520587214, value:0, rssi:64%, type:RELAY QS-R-S5], @0ceb70:[firmware:2b, epoch:1520587211, value:0, rssi:61%, type:RELAY QS-R-S5], @0d7810:NO_REPLY, @207100:[firmware:40, epoch:1520587518, value:0, rssi:49%, type:RELAY QS-R-S30-SPEC], @16a400:[firmware:32, epoch:1520587213, value:0, rssi:82%, type:RELAY QS-R-S5], @2070c0:[firmware:40, epoch:1520587516, value:1, rssi:40%, type:RELAY QS-R-S30-SPEC], @2070a0:[firmware:40, epoch:1520587459, value:1, rssi:53%, type:RELAY QS-R-S30-SPEC], @0ce880:[firmware:2b, epoch:1520586608, value:0, rssi:47%, type:RELAY QS-R-S5], @0cecb0:[firmware:2b, epoch:1520569581, value:0, rssi:55%, type:RELAY QS-R-S5], @2248b0:[firmware:32, epoch:1520587589, value:0, rssi:65%, type:RELAY QS-R-S5], @0ce7f0:[firmware:2b, epoch:1520587202, value:0, rssi:72%, type:RELAY QS-R-S5], @0c8b20:[firmware:2b, epoch:1520587213, value:0, rssi:65%, type:RELAY QS-R-S5], @0acf30:[firmware:2b, epoch:1520586619, value:0, rssi:57%, type:RELAY QS-R-S5], @09e490:[firmware:2b, epoch:1520449506, value:0, rssi:76%, type:RELAY QS-R-S5], @0acfd0:[firmware:2b, epoch:1520587203, value:0, rssi:67%, type:RELAY QS-R-S5], @0c8dd0:[firmware:2b, epoch:1520587207, value:0, rssi:75%, type:RELAY QS-R-S5], @169b80:[firmware:32, epoch:1520587204, value:0, rssi:74%, type:RELAY QS-R-S5], @0d0ce0:[firmware:2b, epoch:1520586618, value:0, rssi:48%, type:RELAY QS-R-S5], @0d53d0:[firmware:2b, epoch:1520587210, value:0, rssi:48%, type:RELAY QS-R-S5], @0ce7e0:[firmware:2b, epoch:1520587201, value:0, rssi:29%, type:RELAY QS-R-S5], @17d550:NOT_SUPPORTED, @0c8b70:[firmware:2b, epoch:1520587217, value:0, rssi:47%, type:RELAY QS-R-S5], @0cefb0:[firmware:2b, epoch:1520362504, value:0, rssi:60%, type:RELAY QS-R-S5], @17da20:NOT_SUPPORTED, @148320:[firmware:32, epoch:1520587212, value:0, rssi:66%, type:RELAY QS-R-S5], @2070b0:[firmware:40, epoch:1520587458, value:0, rssi:55%, type:RELAY QS-R-S30-SPEC], @0ce890:[firmware:2b, epoch:1520587209, value:0, rssi:59%, type:RELAY QS-R-S5], @0ce790:[firmware:2b, epoch:1520587208, value:0, rssi:70%, type:RELAY QS-R-S5], @1002d0:[firmware:32, epoch:1520587215, value:0, rssi:64%, type:RELAY QS-R-S5], @1fb970:[firmware:40, epoch:1520587457, value:0, rssi:70%, type:RELAY QS-R-S30-SPEC], @0c8bd0:[firmware:2b, epoch:1520587206, value:0, rssi:67%, type:RELAY QS-R-S5], success:true, @0ceba0:[firmware:2b, epoch:1520587206, value:0, rssi:65%, type:RELAY QS-R-S5], @0cefd0:[firmware:2b, epoch:1520587205, value:0, rssi:82%, type:RELAY QS-R-S5], @0ce770:[firmware:2b, epoch:1520587210, value:0, rssi:81%, type:RELAY QS-R-S5], @206b50:[firmware:40, epoch:1520587517, value:1, rssi:44%, type:RELAY QS-R-S30-SPEC], @0c8b90:[firmware:2b, epoch:1520587202, value:0, rssi:50%, type:RELAY QS-R-S5]]

Some background and to summarise:

The data comes from a home automation hub known as QwikSwitch. I would like to extract the status of each of the devices controlled by the QwikSwitch hub separately so that I can update the Virtual Switches I have created for the QwikSwitch devices in ST.

The data returned by the QwikSwitch hub in response to the GET request, provide the status of all the devices connected to that hub. (It is not possible to query the status of a single device.) The device ID is preceded by an “@” and the device status is indicated by a “0” or a “1” following “value:”.

I have read many posts and tried my best to figure out how to use the function commands, but so far I’ve been unable to extract the info I require.

The idea is to create variables representing each of the devices connected to the QwikSwitch hub and pass the device status to the variable created for each device. (I don’t know if there is a more elegant way of doing it.)

I played around with many examples of a few functions I have found in other posts, but the best I managed to do was to extract the data of the first device, i.e., I can’t figure out how to extract the status of the device with ID @0c8b20. The best I could do was to extract “value: 0”, but that does not help me (I tried formatting the result as an integer, and it appears to produce only the “0” or the “1”, but I’m unsure.)

The best solution would be to be able to extract the status of “device with ID @****”. Otherwise, I suppose extracting the info of the 1st; 2nd; 3rd etc. device could also work (but will probably cause problems should I later add a device to the QwikSwitch hub, as the new device will not necessarily be added at the end of the list.

I realise my attempts will seem somewhat ridiculous to programmers but bear in mind that I have no programming experience. It will probably be easier to see what I’m trying to accomplish by looking at the piston:


#6

The data you posted up top, is that just from one GET request? How many devices does that represent? I’m not a developer so mine is more of curiosity on how to extract info also :slight_smile:


#7

Yes, it is the result of a single get request. It reports the status of all devices. I haven’t counted them, but I guess there are about 30 devices.


#8

Thanks, I will play with it when things die down at work… :smiley:


#9

So after a bit of trial and error with arrayItem() and some other text functions… this is what I was able to extract.

3/9/2018, 1:29:40 PM +598ms
+2ms	â•”Received event [Oenz Minimote].button = pushed with a delay of 211ms
+329ms	â•‘device=0ce830: state=0
+453ms	â•‘device=224860: state=0
+575ms	â•‘device=0c8c30: state=0
+699ms	â•‘device=0ceb70: state=0
+888ms	â•‘device=207100: state=0
+1030ms	â•‘device=16a400: state=0
+1151ms	â•‘device=2070c0: state=1
+1273ms	â•‘device=2070a0: state=1
+1396ms	â•‘device=0ce880: state=0
+1519ms	â•‘device=0cecb0: state=0
+1640ms	â•‘device=2248b0: state=0
+1760ms	â•‘device=0ce7f0: state=0
+1885ms	â•‘device=0c8b20: state=0
+2010ms	â•‘device=0acf30: state=0
+2131ms	â•‘device=09e490: state=0
+2252ms	â•‘device=0acfd0: state=0
+2373ms	â•‘device=0c8dd0: state=0
+2494ms	â•‘device=169b80: state=0
+2635ms	â•‘device=0d0ce0: state=0
+2770ms	â•‘device=0d53d0: state=0
+2907ms	â•‘device=0ce7e0: state=0
+3127ms	â•‘device=0c8b70: state=0
+3277ms	â•‘device=0cefb0: state=0
+3477ms	â•‘device=148320: state=0
+3607ms	â•‘device=2070b0: state=0
+3734ms	â•‘device=0ce890: state=0
+3868ms	â•‘device=0ce790: state=0
+4011ms	â•‘device=1002d0: state=0
+4152ms	â•‘device=1fb970: state=0
+4286ms	â•‘device=0c8bd0: state=0
+4413ms	â•‘device=0ceba0: state=0
+4545ms	â•‘device=0cefd0: state=0
+4682ms	â•‘device=0ce770: state=0
+4809ms	â•‘device=206b50: state=1
+4934ms	â•‘device=0c8b90: state=0
+4975ms	â•šEvent processed successfully (4975ms)

#10

Fantastic. How did you do that?

I managed to do the following:
I use Replace a few times to get the data uniform. Then I extract every 4th cell (?) to get the status of each device. The status of a device is displayed as “value:1” or “value:0”. So far I’ve not figured out how to get it to only show a “1” or a “0” (I suppose I could do another replace, but I’m sure there is an easier way to format the result. I tried integer() but that did not work.

The results:
image

I’m sure there are more elegant ways to accomplish this.

It would be great to know how you got to your results and how do you suggest the data is used to get the status of each individual device.


#11

I also used a series of replace() to be able to use arrayItem(). I will clean up the code and post it, maybe it can help with what you’re trying to do.


#12

do a replace(string,'value:','') to remove it :), you’ll end up with 0 or 1


#13

Thanks Rey!
Do you perhaps know how I should use the array function to return only a “1” instead of “value:1”?


#14

K, let me get back to my desk so I can post that piston, back in 20 min or so.


#15

I appreciate your input and help.
I’m 99% where I want to go with this. Since it is a dumb way of getting the device status, things will probably go crazy when I add or remove devices. Currently, position (Not sure what the correct term is) 15 of the GET result data provides the status of Light 5 e.g. So I set the variable representing the status of light 5 to the data returned by position 15. But should I remove a device, position 15 will no longer report the result of light 5, but perhaps that of what is currently light 6.
But for now, I should be able set the status of the ST Virtual switches I’ll be creating for each of the deivces.


#16

The piston looks much bigger due to the data I had to include as a variable. Here’s a little explanation of how I massaged the data so I can perform some functions on it.

'\[','' - removed bracket
'\]','' - removed bracket
', @','^@' - replaced `' @' with '^@'
',''!' - replaced comma with !
'^',',' - replaced ^ with comma

Line 29, checked first position of the arrayItem to check if it’s the @ character so I know when to stop the loop.
Line 32, just making sure I’m not parsing the devices that doesn’t have the data we want, ex NOT_SUPPORTED


#17

This is seriously fancy! Thanks again.

I’ll try this tomorrow.

To make provision for the possibility that the order of devices may change in future, is there a way to look for the 0 and 1 (off/On) values of Device @12345? (as opposed to the way I do it by determining what the position of that data for Device @12345 within the data is, and then set the variable representing Device @12345 to that value.)


#18

Hmm, not sure. Do you show the status of the switches data on tiles or something?


#19

For now, I am going to create virtual switches in ST for each physical QwikSwitch device and use this piston to update the status of the virtual switches in ST (for when a QwikSwitch device is switched by the physical button or via the QwikSwitch mobile app.
I will probably add the ST virtual switches to ActionTiles (although I’ve run out of panels in ActionTiles, but that is a challenge for another day.)
I will use another piston to control the QwikSwitch devices from the ST side with the same ST Virtual Switches.


#20

Last question for today - I feel stupid asking this, but I can’t figure out how I would put the data you output to log, into a variable (from where I can then extract the data using the Array function.)