Using a web request to get a time, and using that time to run a piston


#1

1) Give a description of the problem
I am using a custom php file to scrape a URL to get the next moon rise/set time. The site I am scraping only lists the next event, not both. My goal is to capture the time into a global variable, and then one minute after that time, run another piston.

Currently, the PHP returns this:

"value": "1:27 am",
"time": "1:27",
"ampm": "a",

2) What is the expected behavior?
I am trying to program a piston to be run at a specific time based on a global variable that changes twice a day.

3) What is happening/not happening?
I have successfully created a global variable (@nextMoonEvent = 1:27 am)
but when creating a new piston, adding a timer only shows me system variables. (not any of MY variables) Unfortunately, webCoRE does not know the moon rise and set times.

4) Post a Green Snapshot of the piston!
trash

Any advice would be greatly appreciated.


#2

You have to save your data into a date/time or time variable type for wc to make it available in the selection.


#3

So I need to convert 1:27 am into a time stamp first?
(I was hoping to avoid this since I am already at 21 chunks in this piston)

But even if I figure that part out, there are no options to use any of MY variables when setting a timer, as seen in the screenshot above…


#4

Once it’s saved into the proper type, it will be available for your use.

See example here…


#5

Thanks a bunch. I have implemented your advice now, but I have to wait a few hours to see if it works. (I cannot test this piston manually without messing up my graph)


#6

Your tip worked once, but the second attempt it converted the JSON of:
"value": "12:09 pm"
to the global variable of:
@@nextMoonEvent 12:09:00 AM

Any idea why it is 12 hours off?


#7

Webcore does have access to moon rise and moon set.

Here is moon rise

$weather.astronomy.moon_phase.moonrise.hour’:’ $weather.astronomy.moon_phase.moonrise.minute

Returns 1:52 (For me)

And moon set
$weather.astronomy.moon_phase.moonset.hour’:’ $weather.astronomy.moon_phase.moonset.minute

Returns 11:34


#8

Wow thanks! I couldn’t find ANY documentation on moon data.

Unfortunately, since I imagine webCoRE is basing the moonrise times based on zipcode, and my PHP is scraping a site based on my GPS coordinates, there is a couple of minutes of discrepancy.


#9

Can anyone tell me why the JSON of:
"value": "12:09 pm"
turned into:
@@nextMoonEvent 12:09:00 AM

Here is my code and confusion. Notice line 51 & 52 are identical, except one writes to a local variable (correctly) and one writes to a global variable (incorrectly)… Do I need to make PM capitalized for the global to be written correctly?

trash

Here is my log showing both the local and global variables. I am baffled why they aren’t the same

trash


#10

Not sure why it’s doing that but if you drop the pm, it saves properly. You can tell it’s correctly saved if it includes the time zone like your global variable.


#11

Interesting (from the expression evaluation console):

(expression) string(time('12:09 pm')) »»» (string) 12:09:00 AM CST
(expression) string(time('12:09 am')) »»» (string) 12:09:00 PM CST

…and…

(expression) time('12:00 AM') »»» (time) 43200000
(expression) time('12:00 PM') »»» (time) 0

FWIW, 12:00 PM is 12 * 60 * 60 * 1000 = 43200000. Clearly the string-to-time conversion is improperly interpreting AM & PM.

But it looks like it’s only wrong for noon and midnight…

(expression) string(time('1:00 pm')) »»» (string) 1:00:00 PM CST
(expression) string(time('1:00 am')) »»» (string) 1:00:00 AM CST

#12

Well now I am totally baffled. Thanks @pnbruckner for reminding me of the console. I always forget that that is down there. I just ran about 40 tests similar to yours:

(expression) string(time(‘12:09 pm’)) »»» (string) 12:09:00 AM CST
(expression) string(time(‘12:09 am’)) »»» (string) 12:09:00 PM CST

and every hour except noon and midnight is accurate (PM yields PM, and AM yields AM)
but in the dozen of tests I ran scattered in the noon and midnight hours, were all the opposite.
(AM yields PM, and PM yields AM)

Is this a bug? Any advice on a workaround in the meantime?


#13

Ironically @eibyer , it’s the global that is incorrect, even though it has the time zone attached


#14

Looks like a bug to me.

I haven’t seen your whole piston, so guessing from the snippets, but assuming you have the (correct) time in a string (nextMoonEvent?), and you want to set the time variable @@nextMoonEvent to that, then use an expression something like:

StartsWith(nextMoonEvent, '12:') ? (contains(nextMoonEvent, 'am') ? replace(nextMoonEvent, 'am', 'pm') : replace(nextMoonEvent, 'pm', 'am')) : nextMoonEvent

You could even have it self adjust if/when this is fixed:

StartsWith(nextMoonEvent, '12:') && contains(string(time('12:00 am')), 'pm') ? (contains(nextMoonEvent, 'am') ? replace(nextMoonEvent, 'am', 'pm') : replace(nextMoonEvent, 'pm', 'am')) : nextMoonEvent


#15

I appreciate everyone’s input and advice. Since my piston is running out of room, I decided to tweak my PHP file a bit, and return the time in 24h format. Works like a charm until the webCoRE gets an update.


#16

You might find thus interesting. I just released it. Might work for what you need also.