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


#1

1) Give a description of the problem
New to this, but I want to pull the latest weather observations from my WeatherFlow weather station through its API, but I only want to use the air temperature data. Here’s more about their API which I barely understand. I also know that my current piston doesn’t do anything, but the test shows all the latest weather data. https://weatherflow.github.io/SmartWeather/api/

2) What is the expected behaviour?
The API is populating with the latest observations, but I want to use the air temperature data.

3) What is happening/not happening?
Nothing yet. I wish to use the air temperature data in a color changing bulb so that it changes colors with the weather.

**4)Sample piston

I’d like to eventually combine the air temp reading with a piston similar to this.


#2

Try logging $response.obs.air_temperature


#3

Awesome thanks! That pulled out just the air temp as I was looking for.

Looks like they made it Celsius though, so I have to figure that out before trying to merge it with lighting.


#4

webCoRE to the rescue… :slight_smile:
https://wiki.webcore.co/Functions#fahrenheit

Syntax

decimal fahrenheit(decimal value)

Returns

Converts a temperature value from Celsius to Fahrenheit.


#5

Also, edit out your API key if those are real :slight_smile:


#6

Wow. Thank god. :slight_smile:
Thanks for your help. The API key is their public testing key so I’m probably OK?


#7

How would I add that syntax to the piston?


#8

Put the $response line inside fahrenheit()

fahrenheit($reponse.obs etc)


#9

Well that’s easy. Thanks. On to the next challenge on this one.


#10

So I think I’m getting closer. My test API piston and my test color bulb changing piston work separately. But now that I’ve combined them, I’m not sure I’m connecting the web requested value (temperature) to a color for the bulb properly. (since it’s not working)

The temperate from the API does come back with decimals, but not sure if that matters. For example 79.2.


#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)