For Loop Explanation


#1

1) Give a description of the problem
I am trying to have a “for loop” run only when 2 conditions are met.

2) What is the expected behaviour?
Whenever the date and category conditions are true then it should log a piece of info from that array (using $index)

3) What is happening/not happening?
It’s logging that info for the proper categories but it doing it for all the dates in the json data. I only want it to do it for the current date

4) Post a Green Snapshot of the pistonimage

5) Attach logs after turning logging level to Full
|+1ms|╔Received event [Home].test = 1570830143982 with a delay of 0ms|
|—|---|
|+188ms|║RunTime Analysis CS > 23ms > PS > 133ms > PE > 33ms > CE|
|+192ms|║Runtime (48677 bytes) successfully initialized in 133ms (v0.3.110.20191009) (189ms)|
|+193ms|║╔Execution stage started|
|+200ms|║║Cancelling statement #1’s schedules…|
|+211ms|║║Executed virtual command wait (1ms)|
|+212ms|║║Waiting for 1000ms|
|+1220ms|║║Sending external web request to: www.hebcal.com/hebcal/?v=1&cfg=json&year=now&month=now&maj=on&c=on&m=72&b=18&geo=zip&zip=11230|
|+1312ms|║║Executed virtual command httpRequest (92ms)|
|+1332ms|║║30|
|+1334ms|║║Executed virtual command log (3ms)|
|+1341ms|║║[2019-10-01, 2019-10-01T19:49:00-04:00, 2019-10-04T18:14:00-04:00, 2019-10-05T19:43:00-04:00, 2019-10-08, 2019-10-08T18:08:00-04:00, 2019-10-09, 2019-10-09T19:36:00-04:00, 2019-10-11T18:03:00-04:00, 2019-10-12T19:31:00-04:00, 2019-10-13, 2019-10-13T18:00:00-04:00, 2019-10-14, 2019-10-14T19:28:00-04:00, 2019-10-15, 2019-10-15T19:27:00-04:00, 2019-10-16, 2019-10-17, 2019-10-18, 2019-10-18T17:52:00-04:00, 2019-10-19, 2019-10-19T19:21:00-04:00, 2019-10-20, 2019-10-20T17:49:00-04:00, 2019-10-21, 2019-10-21T19:18:00-04:00, 2019-10-22, 2019-10-22T19:17:00-04:00, 2019-10-25T17:43:00-04:00, 2019-10-26T19:11:00-04:00]|
|+1342ms|║║Executed virtual command log (1ms)|
|+1374ms|║║Comparison (date) 1570766400000 is_equal_to (date) 1570766400000 = true (3ms)|
|+1376ms|║║Condition #22 evaluated true (13ms)|
|+1383ms|║║Comparison (dynamic) holiday is_equal_to (string) candles = false (1ms)|
|+1387ms|║║Condition #23 evaluated false (10ms)|
|+1388ms|║║Condition group #21 evaluated false (state did not change) (27ms)|
|+1414ms|║║Comparison (date) 1570766400000 is_equal_to (date) 1570766400000 = true (2ms)|
|+1415ms|║║Condition #22 evaluated true (10ms)|
|+1421ms|║║Comparison (dynamic) havdalah is_equal_to (string) candles = false (2ms)|
|+1424ms|║║Condition #23 evaluated false (8ms)|
|+1426ms|║║Condition group #21 evaluated false (state did not change) (20ms)|
|+1451ms|║║Comparison (date) 1570766400000 is_equal_to (date) 1570766400000 = true (3ms)|
|+1453ms|║║Condition #22 evaluated true (11ms)|
|+1459ms|║║Comparison (dynamic) candles is_equal_to (string) candles = true (2ms)|
|+1461ms|║║Cancelling condition #23’s schedules…|
|+1462ms|║║Condition #23 evaluated true (8ms)|
|+1463ms|║║Cancelling condition #21’s schedules…|
|+1464ms|║║Condition group #21 evaluated true (state changed) (22ms)|
|+1467ms|║║Cancelling statement #24’s schedules…|
|+1478ms|║║Calculating (string) candle lighting today is at + (string) 6:14 PM >> (string) candle lighting today is at 6:14 PM|
|+1483ms|║║candle lighting today is at 6:14 PM|
|+1484ms|║║Executed virtual command log (1ms)|
|+1490ms|║║2|
|+1491ms|║║Executed virtual command log (2ms)|
|+1516ms|║║Comparison (date) 1570766400000 is_equal_to (date) 1570766400000 = true (2ms)|
|+1518ms|║║Condition #22 evaluated true (11ms)|
|+1525ms|║║Comparison (dynamic) havdalah is_equal_to (string) candles = false (1ms)|
|+1527ms|║║Cancelling condition #23’s schedules…|
|+1530ms|║║Condition #23 evaluated false (11ms)|
|+1531ms|║║Cancelling condition #21’s schedules…|
|+1532ms|║║Condition group #21 evaluated false (state changed) (25ms)|
|+1560ms|║║Comparison (date) 1570766400000 is_equal_to (date) 1570766400000 = true (2ms)|
|+1561ms|║║Condition #22 evaluated true (12ms)|
|+1567ms|║║Comparison (dynamic) holiday is_equal_to (string) candles = false (1ms)|
|+1571ms|║║Condition #23 evaluated false (8ms)|
|+1572ms|║║Condition group #21 evaluated false (state did not change) (23ms)|
|+1599ms|║║Comparison (date) 1570766400000 is_equal_to (date) 1570766400000 = true (3ms)|
|+1601ms|║║Condition #22 evaluated true (11ms)|
|+1606ms|║║Comparison (dynamic) candles is_equal_to (string) candles = true (1ms)|
|+1608ms|║║Cancelling condition #23’s schedules…|
|+1609ms|║║Condition #23 evaluated true (7ms)|
|+1610ms|║║Cancelling condition #21’s schedules…|
|+1611ms|║║Condition group #21 evaluated true (state changed) (22ms)|
|+1614ms|║║Cancelling statement #24’s schedules…|
|+1626ms|║║Calculating (string) candle lighting today is at + (string) 6:08 PM >> (string) candle lighting today is at 6:08 PM|
|+1630ms|║║candle lighting today is at 6:08 PM|
|+1631ms|║║Executed virtual command log (2ms)|
|+1637ms|║║5|
|+1638ms|║║Executed virtual command log (1ms)|
|+1664ms|║║Comparison (date) 1570766400000 is_equal_to (date) 1570766400000 = true (3ms)|
|+1666ms|║║Condition #22 evaluated true (11ms)|
|+1672ms|║║Comparison (dynamic) holiday is_equal_to (string) candles = false (1ms)|
|+1673ms|║║Cancelling condition #23’s schedules…|
|+1677ms|║║Condition #23 evaluated false (10ms)|
|+1678ms|║║Cancelling condition #21’s schedules…|
|+1679ms|║║Condition group #21 evaluated false (state changed) (24ms)|
|+1704ms|║║Comparison (date) 1570766400000 is_equal_to (date) 1570766400000 = true (2ms)|
|+1706ms|║║Condition #22 evaluated true (10ms)|
|+1714ms|║║Comparison (dynamic) havdalah is_equal_to (string) candles = false (2ms)|
|+1717ms|║║Condition #23 evaluated false (9ms)|
|+1718ms|║║Condition group #21 evaluated false (state did not change) (23ms)|
|+1744ms|║║Comparison (date) 1570766400000 is_equal_to (date) 1570766400000 = true (3ms)|
|+1746ms|║║Condition #22 evaluated true (11ms)|
|+1752ms|║║Comparison (dynamic) candles is_equal_to (string) candles = true (1ms)|
|+1753ms|║║Cancelling condition #23’s schedules…|
|+1754ms|║║Condition #23 evaluated true (7ms)|
|+1756ms|║║Cancelling condition #21’s schedules…|
|+1757ms|║║Condition group #21 evaluated true (state changed) (22ms)|
|+1759ms|║║Cancelling statement #24’s schedules…|
|+1770ms|║║Calculating (string) candle lighting today is at + (string) 6:03 PM >> (string) candle lighting today is at 6:03 PM|
|+1774ms|║║candle lighting today is at 6:03 PM|
|+1776ms|║║Executed virtual command log (1ms)|
|+1782ms|║║8|
|+1783ms|║║Executed virtual command log (1ms)|
|+1808ms|║║Comparison (date) 1570766400000 is_equal_to (date) 1570766400000 = true (3ms)|
|+1810ms|║║Condition #22 evaluated true (10ms)|
|+1815ms|║║Comparison (dynamic) havdalah is_equal_to (string) candles = false (1ms)|
|+1817ms|║║Cancelling condition #23’s schedules…|
|+1820ms|║║Condition #23 evaluated false (8ms)|
|+1821ms|║║Cancelling condition #21’s schedules…|
|+1822ms|║║Condition group #21 evaluated false (state changed) (23ms)|
|+1847ms|║║Comparison (date) 1570766400000 is_equal_to (date) 1570766400000 = true (2ms)|
|+1848ms|║║Condition #22 evaluated true (10ms)|
|+1854ms|║║Comparison (dynamic) holiday is_equal_to (string) candles = false (1ms)|
|+1857ms|║║Condition #23 evaluated false (8ms)|
|+1859ms|║║Condition group #21 evaluated false (state did not change) (21ms)|
|+1883ms|║║Comparison (date) 1570766400000 is_equal_to (date) 1570766400000 = true (2ms)|
|+1885ms|║║Condition #22 evaluated true (10ms)|
|+1891ms|║║Comparison (dynamic) candles is_equal_to (string) candles = true (1ms)|
|+1892ms|║║Cancelling condition #23’s schedules…|
|+1893ms|║║Condition #23 evaluated true (7ms)|
|+1895ms|║║Cancelling condition #21’s schedules…|
|+1895ms|║║Condition group #21 evaluated true (state changed) (21ms)|
|+1898ms|║║Cancelling statement #24’s schedules…|
|+1909ms|║║Calculating (string) candle lighting today is at + (string) 6:00 PM >> (string) candle lighting today is at 6:00 PM|
|+1912ms|║║candle lighting today is at 6:00 PM|
|+1914ms|║║Executed virtual command log (1ms)|
|+1920ms|║║11|
|+1921ms|║║Executed virtual command log (1ms)|
|+1946ms|║║Comparison (date) 1570766400000 is_equal_to (date) 1570766400000 = true (3ms)|
|+1948ms|║║Condition #22 evaluated true (11ms)|
|+1954ms|║║Comparison (dynamic) holiday is_equal_to (string) candles = false (1ms)|
|+1955ms|║║Cancelling condition #23’s schedules…|
|+1958ms|║║Condition #23 evaluated false (9ms)|
|+1960ms|║║Cancelling condition #21’s schedules…|
|+1961ms|║║Condition group #21 evaluated false (state changed) (24ms)|
|+1986ms|║║Comparison (date) 1570766400000 is_equal_to (date) 1570766400000 = true (3ms)|
|+1988ms|║║Condition #22 evaluated true (10ms)|
|+1993ms|║║Comparison (dynamic) candles is_equal_to (string) candles = true (1ms)|
|+1995ms|║║Cancelling condition #23’s schedules…|
|+1996ms|║║Condition #23 evaluated true (8ms)|
|+1997ms|║║Cancelling condition #21’s schedules…|
|+1998ms|║║Condition group #21 evaluated true (state changed) (21ms)|
|+2001ms|║║Cancelling statement #24’s schedules…|
|+2012ms|║║Calculating (string) candle lighting today is at + (string) 7:28 PM >> (string) candle lighting today is at 7:28 PM|
|+2016ms|║║candle lighting today is at 7:28 PM|
|+2017ms|║║Executed virtual command log (1ms)|
|+2023ms|║║13|
|+2024ms|║║Executed virtual command log (2ms)|
|+2050ms|║║Comparison (date) 1570766400000 is_equal_to (date) 1570766400000 = true (2ms)|
|+2052ms|║║Condition #22 evaluated true (11ms)|
|+2058ms|║║Comparison (dynamic) holiday is_equal_to (string) candles = false (1ms)|
|+2059ms|║║Cancelling condition #23’s schedules…|
|+2062ms|║║Condition #23 evaluated false (9ms)|
|+2063ms|║║Cancelling condition #21’s schedules…|
|+2064ms|║║Condition group #21 evaluated false (state changed) (23ms)|
|+2091ms|║║Comparison (date) 1570766400000 is_equal_to (date) 1570766400000 = true (2ms)|
|+2092ms|║║Condition #22 evaluated true (11ms)|
|+2098ms|║║Comparison (dynamic) havdalah is_equal_to (string) candles = false (1ms)|
|+2102ms|║║Condition #23 evaluated false (8ms)|
|+2103ms|║║Condition group #21 evaluated false (state did not change) (22ms)|
|+2128ms|║║Comparison (date) 1570766400000 is_equal_to (date) 1570766400000 = true (3ms)|
|+2130ms|║║Condition #22 evaluated true (11ms)|
|+2136ms|║║Comparison (dynamic) holiday is_equal_to (string) candles = false (2ms)|
|+2140ms|║║Condition #23 evaluated false (8ms)|
|+2141ms|║║Condition group #21 evaluated false (state did not change) (22ms)|
|+2166ms|║║Comparison (date) 1570766400000 is_equal_to (date) 1570766400000 = true (2ms)|
|+2168ms|║║Condition #22 evaluated true (11ms)|
|+2174ms|║║Comparison (dynamic) holiday is_equal_to (string) candles = false (1ms)|
|+2177ms|║║Condition #23 evaluated false (8ms)|
|+2178ms|║║Condition group #21 evaluated false (state did not change) (21ms)|
|+2203ms|║║Comparison (date) 1570766400000 is_equal_to (date) 1570766400000 = true (2ms)|
|+2205ms|║║Condition #22 evaluated true (11ms)|
|+2211ms|║║Comparison (dynamic) holiday is_equal_to (string) candles = false (1ms)|
|+2214ms|║║Condition #23 evaluated false (8ms)|
|+2215ms|║║Condition group #21 evaluated false (state did not change) (21ms)|
|+2240ms|║║Comparison (date) 1570766400000 is_equal_to (date) 1570766400000 = true (2ms)|
|+2242ms|║║Condition #22 evaluated true (10ms)|
|+2248ms|║║Comparison (dynamic) candles is_equal_to (string) candles = true (2ms)|
|+2250ms|║║Cancelling condition #23’s schedules…|
|+2251ms|║║Condition #23 evaluated true (7ms)|
|+2252ms|║║Cancelling condition #21’s schedules…|
|+2253ms|║║Condition group #21 evaluated true (state changed) (21ms)|
|+2255ms|║║Cancelling statement #24’s schedules…|
|+2266ms|║║Calculating (string) candle lighting today is at + (string) 5:52 PM >> (string) candle lighting today is at 5:52 PM|
|+2270ms|║║candle lighting today is at 5:52 PM|
|+2271ms|║║Executed virtual command log (1ms)|
|+2278ms|║║19|
|+2279ms|║║Executed virtual command log (2ms)|
|+2304ms|║║Comparison (date) 1570766400000 is_equal_to (date) 1570766400000 = true (2ms)|
|+2306ms|║║Condition #22 evaluated true (11ms)|
|+2312ms|║║Comparison (dynamic) holiday is_equal_to (string) candles = false (1ms)|
|+2313ms|║║Cancelling condition #23’s schedules…|
|+2316ms|║║Condition #23 evaluated false (9ms)|
|+2317ms|║║Cancelling condition #21’s schedules…|
|+2318ms|║║Condition group #21 evaluated false (state changed) (23ms)|
|+2343ms|║║Comparison (date) 1570766400000 is_equal_to (date) 1570766400000 = true (3ms)|
|+2344ms|║║Condition #22 evaluated true (10ms)|
|+2350ms|║║Comparison (dynamic) havdalah is_equal_to (string) candles = false (1ms)|
|+2353ms|║║Condition #23 evaluated false (8ms)|
|+2355ms|║║Condition group #21 evaluated false (state did not change) (20ms)|
|+2379ms|║║Comparison (date) 1570766400000 is_equal_to (date) 1570766400000 = true (2ms)|
|+2381ms|║║Condition #22 evaluated true (10ms)|
|+2387ms|║║Comparison (dynamic) holiday is_equal_to (string) candles = false (1ms)|
|+2390ms|║║Condition #23 evaluated false (8ms)|
|+2391ms|║║Condition group #21 evaluated false (state did not change) (21ms)|
|+2415ms|║║Comparison (date) 1570766400000 is_equal_to (date) 1570766400000 = true (3ms)|
|+2417ms|║║Condition #22 evaluated true (9ms)|
|+2422ms|║║Comparison (dynamic) candles is_equal_to (string) candles = true (1ms)|
|+2424ms|║║Cancelling condition #23’s schedules…|
|+2425ms|║║Condition #23 evaluated true (7ms)|
|+2426ms|║║Cancelling condition #21’s schedules…|
|+2427ms|║║Condition group #21 evaluated true (state changed) (21ms)|
|+2429ms|║║Cancelling statement #24’s schedules…|
|+2440ms|║║Calculating (string) candle lighting today is at + (string) 5:49 PM >> (string) candle lighting today is at 5:49 PM|
|+2444ms|║║candle lighting today is at 5:49 PM|
|+2445ms|║║Executed virtual command log (1ms)|
|+2453ms|║║23|
|+2454ms|║║Executed virtual command log (2ms)|
|+2479ms|║║Comparison (date) 1570766400000 is_equal_to (date) 1570766400000 = true (3ms)|
|+2481ms|║║Condition #22 evaluated true (10ms)|
|+2486ms|║║Comparison (dynamic) holiday is_equal_to (string) candles = false (1ms)|
|+2488ms|║║Cancelling condition #23’s schedules…|
|+2491ms|║║Condition #23 evaluated false (9ms)|
|+2492ms|║║Cancelling condition #21’s schedules…|
|+2493ms|║║Condition group #21 evaluated false (state changed) (22ms)|
|+2517ms|║║Comparison (date) 1570766400000 is_equal_to (date) 1570766400000 = true (2ms)|
|+2519ms|║║Condition #22 evaluated true (10ms)|
|+2525ms|║║Comparison (dynamic) candles is_equal_to (string) candles = true (1ms)|
|+2526ms|║║Cancelling condition #23’s schedules…|
|+2527ms|║║Condition #23 evaluated true (7ms)|
|+2528ms|║║Cancelling condition #21’s schedules…|
|+2529ms|║║Condition group #21 evaluated true (state changed) (21ms)|
|+2532ms|║║Cancelling statement #24’s schedules…|
|+2542ms|║║Calculating (string) candle lighting today is at + (string) 7:18 PM >> (string) candle lighting today is at 7:18 PM|
|+2547ms|║║candle lighting today is at 7:18 PM|
|+2548ms|║║Executed virtual command log (1ms)|
|+2554ms|║║25|
|+2555ms|║║Executed virtual command log (2ms)|
|+2580ms|║║Comparison (date) 1570766400000 is_equal_to (date) 1570766400000 = true (2ms)|
|+2582ms|║║Condition #22 evaluated true (10ms)|
|+2588ms|║║Comparison (dynamic) holiday is_equal_to (string) candles = false (2ms)|
|+2590ms|║║Cancelling condition #23’s schedules…|
|+2592ms|║║Condition #23 evaluated false (9ms)|
|+2593ms|║║Cancelling condition #21’s schedules…|
|+2594ms|║║Condition group #21 evaluated false (state changed) (23ms)|
|+2619ms|║║Comparison (date) 1570766400000 is_equal_to (date) 1570766400000 = true (3ms)|
|+2621ms|║║Condition #22 evaluated true (10ms)|
|+2627ms|║║Comparison (dynamic) havdalah is_equal_to (string) candles = false (1ms)|
|+2630ms|║║Condition #23 evaluated false (8ms)|
|+2631ms|║║Condition group #21 evaluated false (state did not change) (21ms)|
|+2656ms|║║Comparison (date) 1570766400000 is_equal_to (date) 1570766400000 = true (2ms)|
|+2657ms|║║Condition #22 evaluated true (10ms)|
|+2663ms|║║Comparison (dynamic) candles is_equal_to (string) candles = true (2ms)|
|+2665ms|║║Cancelling condition #23’s schedules…|
|+2666ms|║║Condition #23 evaluated true (7ms)|
|+2667ms|║║Cancelling condition #21’s schedules…|
|+2668ms|║║Condition group #21 evaluated true (state changed) (20ms)|
|+2670ms|║║Cancelling statement #24’s schedules…|
|+2681ms|║║Calculating (string) candle lighting today is at + (string) 5:43 PM >> (string) candle lighting today is at 5:43 PM|
|+2685ms|║║candle lighting today is at 5:43 PM|
|+2686ms|║║Executed virtual command log (1ms)|
|+2692ms|║║28|
|+2693ms|║║Executed virtual command log (1ms)|
|+2718ms|║║Comparison (date) 1570766400000 is_equal_to (date) 1570766400000 = true (2ms)|
|+2720ms|║║Condition #22 evaluated true (11ms)|
|+2726ms|║║Comparison (dynamic) havdalah is_equal_to (string) candles = false (1ms)|
|+2727ms|║║Cancelling condition #23’s schedules…|
|+2730ms|║║Condition #23 evaluated false (9ms)|
|+2731ms|║║Cancelling condition #21’s schedules…|
|+2732ms|║║Condition group #21 evaluated false (state changed) (23ms)|
|+2756ms|║║Comparison (date) 1570766400000 is_equal_to (date) 1570766400000 = true (2ms)|
|+2758ms|║║Condition #22 evaluated true (10ms)|
|+2764ms|║║Comparison (dynamic) is_equal_to (string) candles = false (2ms)|
|+2767ms|║║Condition #23 evaluated false (8ms)|
|+2768ms|║║Condition group #21 evaluated false (state did not change) (21ms)|
|+2786ms|║╚Execution stage complete. (2592ms)|
|+2787ms|╚Event processed successfully (2787ms)|


#2

I don’t see where ‘Date’ is defined for your first condition. Did you mean to use ‘$date’?

It doesn’t look like your array syntax is working. Have you tried using the variable you defined? Based on your data types, I suggest you try ‘arrayItem($index, hebcal_date)’.


#3

I may be misunderstanding something, but…
I’m not sure about the variables in your define section, are these getting set to the correct values after the get call? Although you don’t use most, the loop limit variable is defined here, so is it using the correct value for the most recent get?

I’m also curious about the lightning bolt trigger, should this be a trigger? I suspect this is a work in progress and will either be called by another piston, or will have more code eventually.

It may also be worth logging the values of the two dates before the if condition.


#4

“date” in the first condition is converting the unix epoch datetime format to just date.

The syntax is working, I’m getting the correct data, I’ve logged it before.


#5

Yes the variables are getting the correct data after the call. I’ve worked out the syntax a while back.
Yes this is a work in progress. Noticed my title:)

The lighting bolt is because I use the virtual device “date” so its subscribing to the device.

I’ve tested the date part numerous times, that part is working properly.


#6

What I see in the logs is date($response.items[$index].date) always returns the same value. That is why I suggested you change to arrayItem($index,hebcal_date) which should return all the different values as desired.


#7

When testing on JSON results, I might recommend dropping the FOR LOOP until you discover what works hard coded. (using real numbers instead of $index) Once you have a working block or two, it is easy to convert back into a loop if you so desire.

In other words, drag lines 32-39 outside of the loop, and change $index to actual numbers during testing.

This lets you quickly test different scenarios & variations…


One other tip for troubleshooting:

If this were my piston, I would also change these 3 variables up top to say: (no value set)
temp

Then, in the body of the code (after the GET request) I would set these 3 variables using the "Set variable" command. This has the added benefit of always showing you exactly what was returned in any given moment. (which makes troubleshooting my previous tip even easier)


#8

I figured out the issue. The reason the date was always evaluating as true was because there was an error; webcore couldn’t convert the string from $response.items[$index].date to a date because it contained “-” instead of “/”. For some reason whenever there’s an error it just returned the date the error occurred (and if you asked for a time it would return the time the error occurred). I’m not sure the reason the system does this but that’s why the date was always true and why the date was the same for all $index.

@guxdude


#9

Thanks for the tip!
I keep on getting ahead of myself. I need to take this one step at a time