GET weather API data in a web request, for color changing bulb?


#11

I believe line 25 should use an Expression instead of Value, similar to this:

temp


It ends up looking like this:


#12

You can also test if the decimal is throwing off your color piston with floor().

floor(fahrenheit($response.obs...etc))

or ceil() if you lean that way LOL


#13

Or use “round” for a bit more precision :grin:

round((fahrenheit($response.obs.air_temperature)),0)

(The zero at the end will round to the nearest whole number, with zero decimals)


#14

Yes, that too! If you want wc to decide for you :slight_smile:


#15

Thanks. It’s doing more processing stuff (sorry I don’t know what it means), but it says “canceling statement #18’s schedules.” Is that a bad thing? line 18 is the “do” before the web request. It’s also not showing the color that would have been selected for the bulb, like my other tests did in a separate piston.

+0ms ╔Received event [Home].time = 1564008525285 with a delay of -1175ms
+133ms ║RunTime Analysis CS > 19ms > PS > 60ms > PE > 55ms > CE
+136ms ║Runtime (50593 bytes) successfully initialized in 60ms (v0.3.10d.20190627) (135ms)
+137ms ║╔Execution stage started
+146ms ║║Cancelling statement #18’s schedules…
+153ms ║║Sending external web request to: swd.weatherflow.com/swd/rest/observations/station/8616?api_key=20c70eae-e62f-4d3b-b3a4-8586e90f3ac8
+213ms ║║Executed virtual command httpRequest (61ms)
+220ms ║║Calculating (decimal) 84.0 , (decimal) 0.0 >> (decimal) 84.0
+225ms ║║Executed virtual command setVariable (3ms)
+228ms ║╚Execution stage complete. (90ms)
+229ms ║Setting up scheduled job for Wed, Jul 24 2019 @ 5:49:45 PM CDT (in 60.946s)
+237ms ╚Event processed successfully (237ms)

#16

We’d need to see a green snapshot with Trace turned on to see what Statement 18 is. (it is not based on lines)

Normally, it cancels right before it executes, but I’d need to see the above to confirm.


#17

OK. It looks like line 18 is different in the green photo.
EDIT: I also noticed that the color expression is showing the right color for temp from the response. It’s just for some reason not going to the color changing bulb perhaps.


#18

So this seems weird. The piston works on the bulb, but only if the state of the bulb changes. For example, IF the bulb changes to on, the piston performs correctly and chooses the right color.

Nothing happens if the bulb “is on.”

When the state of the bulb changes:
7/24/2019, 7:33:48 PM +214ms
+2ms ╔Received event [Living Room Accent].switch = on with a delay of 108ms
+1445ms ║RunTime Analysis CS > 1302ms > PS > 91ms > PE > 52ms > CE
+1448ms ║Runtime (50734 bytes) successfully initialized in 91ms (v0.3.10d.20190627) (1446ms)
+1449ms ║╔Execution stage started
+1463ms ║║Comparison (enum) on changes_to (string) on = true (1ms)
+1465ms ║║Cancelling condition #12’s schedules…
+1466ms ║║Condition #12 evaluated true (7ms)
+1467ms ║║Cancelling condition #11’s schedules…
+1468ms ║║Condition group #11 evaluated true (state changed) (10ms)
+1470ms ║║Cancelling statement #2’s schedules…
+1518ms ║║Calculating (dynamic) 80.0 <= (integer) 100 >> (boolean) true
+1523ms ║║Calculating (dynamic) 80.0 <= (integer) 95 >> (boolean) true
+1529ms ║║Calculating (dynamic) 80.0 <= (integer) 90 >> (boolean) true
+1534ms ║║Calculating (dynamic) 80.0 <= (integer) 85 >> (boolean) true
+1540ms ║║Calculating (dynamic) 80.0 <= (integer) 80 >> (boolean) true
+1545ms ║║Calculating (dynamic) 80.0 <= (integer) 75 >> (boolean) false
+1551ms ║║Calculating (dynamic) 80.0 <= (integer) 70 >> (boolean) false
+1556ms ║║Calculating (dynamic) 80.0 <= (integer) 65 >> (boolean) false
+1565ms ║║Calculating (dynamic) 80.0 <= (integer) 60 >> (boolean) false
+1571ms ║║Calculating (dynamic) 80.0 <= (integer) 55 >> (boolean) false
+1577ms ║║Calculating (dynamic) 80.0 <= (integer) 50 >> (boolean) false
+1583ms ║║Calculating (dynamic) 80.0 <= (integer) 45 >> (boolean) false
+1588ms ║║Calculating (dynamic) 80.0 <= (integer) 40 >> (boolean) false
+1594ms ║║Calculating (dynamic) 80.0 <= (integer) 32 >> (boolean) false
+1599ms ║║Calculating (dynamic) 80.0 <= (integer) 15 >> (boolean) false
+1604ms ║║Calculating (dynamic) 80.0 < (integer) 0 >> (boolean) false
+1633ms ║║Executed physical command [Living Room Accent].setColor([[hex: #b1ff20, hue:22, saturation:100, level:56]]) (18ms)
+1634ms ║║Executed [Living Room Accent].setColor (23ms)
+1657ms ║╚Execution stage complete. (208ms)
+1659ms ║Setting up scheduled job for Wed, Jul 24 2019 @ 7:34:37 PM CDT (in 47.243s)
+1673ms ╚Event processed successfully (1673ms)


#19

Up in the define section, I would remove the hard code for the variable {color}.
IE: leave it (not set)

Instead, I would set the {color} variable after your current “Set variable {Response}”.
Otherwise, it will always use old data


#20

Thanks for all your help. I appreciate it. I think I made the correct change. But there’s an issue still.

But now it only works correctly if I click on "Test."
7/24/2019, 9:15:17 PM +235ms
+0ms ╔Received event [Home].test = 1564020917234 with a delay of 0ms
+131ms ║RunTime Analysis CS > 19ms > PS > 61ms > PE > 51ms > CE
+134ms ║Runtime (46263 bytes) successfully initialized in 61ms (v0.3.10d.20190627) (133ms)
+135ms ║╔Execution stage started
+150ms ║║Comparison (enum) on is (string) on = true (1ms)
+152ms ║║Condition #12 evaluated true (10ms)
+153ms ║║Condition group #11 evaluated true (state did not change) (11ms)
+156ms ║║Cancelling statement #2’s schedules…
+161ms ║║Skipped execution of physical command [Living Room Accent].on([]) because it would make no change to the device. (3ms)
+162ms ║║Executed [Living Room Accent].on (5ms)
+175ms ║║Executed physical command [Living Room Accent].setColor([[hex: #63ff20, hue:28, saturation:100, level:56]]) (9ms)
+176ms ║║Executed [Living Room Accent].setColor (12ms)
+179ms ║╚Execution stage complete. (44ms)
+181ms ║Setting up scheduled job for Wed, Jul 24 2019 @ 9:16:11 PM CDT (in 53.755s)
+193ms ╚Event processed successfully (193ms)

The schedule for every minute does something different.
7/24/2019, 9:17:10 PM +98ms
+1ms ╔Received event [Home].time = 1564021031170 with a delay of -1072ms
+123ms ║RunTime Analysis CS > 18ms > PS > 61ms > PE > 44ms > CE
+126ms ║Runtime (46268 bytes) successfully initialized in 61ms (v0.3.10d.20190627) (124ms)
+127ms ║╔Execution stage started
+138ms ║║Cancelling statement #18’s schedules…
+145ms ║║Sending external web request to: swd.weatherflow.com/swd/rest/observations/station/8616?api_key=20c70eae-e62f-4d3b-b3a4-8586e90f3ac8
+180ms ║║Executed virtual command httpRequest (36ms)
+188ms ║║Executed virtual command setVariable (3ms)
+231ms ║║Calculating (dynamic) 74.3 <= (integer) 100 >> (boolean) true
+237ms ║║Calculating (dynamic) 74.3 <= (integer) 95 >> (boolean) true
+243ms ║║Calculating (dynamic) 74.3 <= (integer) 90 >> (boolean) true
+249ms ║║Calculating (dynamic) 74.3 <= (integer) 85 >> (boolean) true
+254ms ║║Calculating (dynamic) 74.3 <= (integer) 80 >> (boolean) true
+260ms ║║Calculating (dynamic) 74.3 <= (integer) 75 >> (boolean) true
+265ms ║║Calculating (dynamic) 74.3 <= (integer) 70 >> (boolean) false
+270ms ║║Calculating (dynamic) 74.3 <= (integer) 65 >> (boolean) false
+276ms ║║Calculating (dynamic) 74.3 <= (integer) 60 >> (boolean) false
+281ms ║║Calculating (dynamic) 74.3 <= (integer) 55 >> (boolean) false
+286ms ║║Calculating (dynamic) 74.3 <= (integer) 50 >> (boolean) false
+291ms ║║Calculating (dynamic) 74.3 <= (integer) 45 >> (boolean) false
+295ms ║║Calculating (dynamic) 74.3 <= (integer) 40 >> (boolean) false
+300ms ║║Calculating (dynamic) 74.3 <= (integer) 32 >> (boolean) false
+305ms ║║Calculating (dynamic) 74.3 <= (integer) 15 >> (boolean) false
+309ms ║║Calculating (dynamic) 74.3 < (integer) 0 >> (boolean) false
+314ms ║║Executed virtual command setVariable (3ms)
+317ms ║╚Execution stage complete. (190ms)
+318ms ║Setting up scheduled job for Wed, Jul 24 2019 @ 9:18:11 PM CDT (in 60.754s)
+328ms ╚Event processed successfully (328ms)


#21

Woohoo! This worked. I moved the bulb and set color into the execute every minute and now it’s working great. I’ll mess around to find a way to leave the bulb off if it’s already off.

Thanks for everyone’s help.


#22

The command:
Every X minutes, do Y
Is one of the few triggers that does not run top to bottom. It only executes what is inside the block at the time of execution. The rest of the code outside is ignored.


#23

Thanks. Just getting the hang of the terminology and how it all works.


#24

So this worked until recently. It appears that it cannot read the response with $response. Any ideas on how to write out a json expression to pull data?


#25

Check the below thread… it might be related.


#26

Are there any errors at account.smartthings.com > Live Logging? It looks like this API responds quickly enough to not be affected by the linked timeout issue, at least from your past logs. If there are no errors, try logging $httpStatusCode – is it in the 300’s?


#27

Oh nope, this is something new. webCoRE is sending a very wrong request header:
Accept: application/x-www-form-urlencoded

That doesn’t make sense, the Accept header tells the server what type of response it should send back. It should not be set to match the request. I don’t see any code in webCoRE doing this, SmartThings is defaulting that to the request type but I can override it. That’s an illogical default :confused:

If you need this quickly and want to apply a code patch, line 3301 in the webCoRE Piston smart app sets the requestContentType and needs the following contentType line added (this code is not specific to you, JSON is the only response that webCoRE understands and “prefers” but anything is acceptable)

requestContentType: requestContentType,
contentType: 'application/json, */*;q=0.9',  // add this line

Web requests may fail with 406 status code due to incorrect Accept header
#28

My god, you’re a genius. This now works and my lights finally turn color and my outdoor lights finally turn off when it’s light outside. I couldn’t be certain, but I recently updated webcore and this is what must have messed up everything. Thanks so much.

Should I post this as a bug, though I don’t understand the issue beyond what you typed. :slight_smile:


#29

I’ll write up a bug and link this task. It was convenient that your API URL was still in the thread!


#31

I have since confirmed that the edit sometimes works and sometimes doesn’t. There must be something else also happening that is affecting the reliability of the get request.

When it works
3/4/2021, 7:10:43 AM +298ms
+1ms ╔Received event [Home].wc_async_reply = httpRequest with a delay of 0ms
+76ms ║RunTime Analysis CS > 14ms > PS > 5ms > PE > 56ms > CE
+78ms ║Runtime (46581 bytes) successfully initialized in 5ms (v0.3.113.20210203) (77ms)
+79ms ║╔Execution stage started
+94ms ║║Calculating (decimal) 26.0 , (decimal) 0.0 >> (decimal) 26.0
+97ms ║║Executed virtual command setVariable (2ms)
+145ms ║║Calculating (dynamic) 26.0 <= (integer) 110 >> (boolean) true
+150ms ║║Calculating (dynamic) 26.0 <= (integer) 100 >> (boolean) true
+155ms ║║Calculating (dynamic) 26.0 <= (integer) 95 >> (boolean) true
+160ms ║║Calculating (dynamic) 26.0 <= (integer) 90 >> (boolean) true
+165ms ║║Calculating (dynamic) 26.0 <= (integer) 85 >> (boolean) true
+171ms ║║Calculating (dynamic) 26.0 <= (integer) 80 >> (boolean) true
+180ms ║║Calculating (dynamic) 26.0 <= (integer) 75 >> (boolean) true
+185ms ║║Calculating (dynamic) 26.0 <= (integer) 70 >> (boolean) true
+191ms ║║Calculating (dynamic) 26.0 <= (integer) 65 >> (boolean) true
+196ms ║║Calculating (dynamic) 26.0 < (integer) 60 >> (boolean) true
+200ms ║║Calculating (dynamic) 26.0 <= (integer) 55 >> (boolean) true
+206ms ║║Calculating (dynamic) 26.0 <= (integer) 50 >> (boolean) true
+210ms ║║Calculating (dynamic) 26.0 <= (integer) 45 >> (boolean) true
+215ms ║║Calculating (dynamic) 26.0 <= (integer) 40 >> (boolean) true
+219ms ║║Calculating (dynamic) 26.0 < (integer) 32 >> (boolean) true
+224ms ║║Calculating (dynamic) 26.0 <= (integer) 15 >> (boolean) false
+228ms ║║Calculating (dynamic) 26.0 <= (integer) 0 >> (boolean) false
+232ms ║║Calculating (decimal) 0.0 - (decimal) 5.0 >> (decimal) -5.0
+235ms ║║Calculating (dynamic) 26.0 <= (decimal) -5.0 >> (boolean) false
+239ms ║║Calculating (decimal) 0.0 - (decimal) 10.0 >> (decimal) -10.0
+242ms ║║Calculating (dynamic) 26.0 <= (decimal) -10.0 >> (boolean) false
+246ms ║║Executed virtual command setVariable (2ms)
+249ms ║║Cancelling statement #23’s schedules…
+289ms ║║Executed physical command [Weather Lamp].setColor([[hex: #4242ff, hue:67, saturation:100, level:63]]) (32ms)
+290ms ║║Executed [Weather Lamp].setColor (37ms)
+292ms ║╚Execution stage complete. (213ms)
+294ms ║Setting up scheduled job for Thu, Mar 4 2021 @ 7:15:44 AM CST (in 300s)
+304ms ╚Event processed successfully (304ms)

When it doesn’t
3/4/2021, 7:06:08 AM +56ms
+0ms ╔Received event [Home].time = 1614863168599 with a delay of -543ms
+75ms ║RunTime Analysis CS > 13ms > PS > 6ms > PE > 56ms > CE
+78ms ║Runtime (46576 bytes) successfully initialized in 6ms (v0.3.113.20210203) (77ms)
+80ms ║╔Execution stage started
+147ms ║║Calculating (decimal) 32.0 , (decimal) 0.0 >> (decimal) 32.0
+154ms ║║Executed virtual command setVariable (3ms)
+214ms ║║Calculating (dynamic) 32.0 <= (integer) 110 >> (boolean) true
+220ms ║║Calculating (dynamic) 32.0 <= (integer) 100 >> (boolean) true
+226ms ║║Calculating (dynamic) 32.0 <= (integer) 95 >> (boolean) true
+233ms ║║Calculating (dynamic) 32.0 <= (integer) 90 >> (boolean) true
+239ms ║║Calculating (dynamic) 32.0 <= (integer) 85 >> (boolean) true
+245ms ║║Calculating (dynamic) 32.0 <= (integer) 80 >> (boolean) true
+251ms ║║Calculating (dynamic) 32.0 <= (integer) 75 >> (boolean) true
+257ms ║║Calculating (dynamic) 32.0 <= (integer) 70 >> (boolean) true
+263ms ║║Calculating (dynamic) 32.0 <= (integer) 65 >> (boolean) true
+269ms ║║Calculating (dynamic) 32.0 < (integer) 60 >> (boolean) true
+275ms ║║Calculating (dynamic) 32.0 <= (integer) 55 >> (boolean) true
+281ms ║║Calculating (dynamic) 32.0 <= (integer) 50 >> (boolean) true
+288ms ║║Calculating (dynamic) 32.0 <= (integer) 45 >> (boolean) true
+293ms ║║Calculating (dynamic) 32.0 <= (integer) 40 >> (boolean) true
+298ms ║║Calculating (dynamic) 32.0 < (integer) 32 >> (boolean) false
+304ms ║║Calculating (dynamic) 32.0 <= (integer) 15 >> (boolean) false
+309ms ║║Calculating (dynamic) 32.0 <= (integer) 0 >> (boolean) false
+312ms ║║Calculating (decimal) 0.0 - (decimal) 5.0 >> (decimal) -5.0
+316ms ║║Calculating (dynamic) 32.0 <= (decimal) -5.0 >> (boolean) false
+320ms ║║Calculating (decimal) 0.0 - (decimal) 10.0 >> (decimal) -10.0
+323ms ║║Calculating (dynamic) 32.0 <= (decimal) -10.0 >> (boolean) false
+329ms ║║Executed virtual command setVariable (3ms)
+332ms ║║Cancelling statement #23’s schedules…
+347ms ║║Executed physical command [Weather Lamp].setColor([[hex: #428dff, hue:60, saturation:100, level:63]]) (7ms)
+348ms ║║Executed [Weather Lamp].setColor (10ms)
+386ms ║╚Execution stage complete. (306ms)
+388ms ║Setting up scheduled job for Thu, Mar 4 2021 @ 7:10:44 AM CST (in 275s)
+396ms ╚Event processed successfully (396ms)