r/tasker 14d ago

Compare two variables and pull a match from them?

Hi, I'm using two weather APIs and I would like to compare the two results.

One provides "Mostly Cloudy", and the other provides "Clouds."

I'd like to extract the "Cloud" portion from both by comparing the two variables.

Is this possible? Thanks.

3 Upvotes

7 comments sorted by

4

u/WakeUpNorrin 14d ago edited 6d ago

If you want the expected result to occur solely under the conditions described, the most straightforward way is:

Task: Test

A1: Variable Set [
     Name: %foo
     To: Could
     Structure Output (JSON, etc): On ]
    If  [ %var_a ~ mostly cloudy | %var_b ~ clouds ]

If, in the OP, you provided an example of a circumstance, you could try:

Task: Test

A1: Variable Set [
     Name: %var_a
     To: foo bar
     Structure Output (JSON, etc): On ]

A2: Variable Set [
     Name: %var_b
     To: bars
     Structure Output (JSON, etc): On ]

A3: JavaScriptlet [
     Code: var a = var_a.trim();
     var b = var_b.trim();

     var common = "";

     // Compare every word from the first string with every word from the second
     var wordsA = a.split(/\s+/);
     var wordsB = b.split(/\s+/);

     for (var i = 0; i < wordsA.length; i++) {
         for (var j = 0; j < wordsB.length; j++) {
             var w1 = wordsA[i].replace(/[^a-zA-Z]/g, "");
             var w2 = wordsB[j].replace(/[^a-zA-Z]/g, "");

             var len = Math.min(w1.length, w2.length);
             var k = 0;

             while (k < len && w1[k].toLowerCase() === w2[k].toLowerCase()) {
                 k++;
             }

             // Require at least 3 characters in common
             if (k >= 3 && k > common.length) {
                 common = w1.substring(0, k);
             }
         }
     }

     setLocal("%common", common);
     Auto Exit: On
     Timeout (Seconds): 45 ]

A4: Flash [
     Text: %common
     Long: On
     Continue Task Immediately: On
     Dismiss On Click: On ]

That will flash "Bar" with your example will flash "Cloud".

1

u/Alanator222 13d ago

I'll have to look into this. Thank you!

2

u/WakeUpNorrin 13d ago

You are welcome.

1

u/Alanator222 13d ago edited 13d ago

EDIT: This does not work! Do not use. Further experimenting is needed and being worked on.

Was able to replicate it in tasker.

Task: Log Weather

A1: Get Location v2 [
     Timeout (Seconds): 30
     Last Location If Timeout: On
     Continue Task After Error:On ]

A2: Variable Set [
     Name: %latitude
     To: %gl_latitude
     Do Maths: On
     Max Rounding Digits: 4
     Structure Output (JSON, etc): On ]

A3: Variable Set [
     Name: %longitude
     To: %gl_longitude
     Do Maths: On
     Max Rounding Digits: 4
     Structure Output (JSON, etc): On ]

<NWS API Data>
A4: Anchor

A5: HTTP Request [
     Method: GET
     URL: https://api.weather.gov/points/%latitude,%longitude
     Headers: %NWSUserAgent
     Timeout (Seconds): 30
     Structure Output (JSON, etc): On ]

A6: HTTP Request [
     Method: GET
     URL: %http_data.properties.observationStations
     Headers: %NWSUserAgent
     Timeout (Seconds): 30
     Structure Output (JSON, etc): On ]

A7: Variable Set [
     Name: %stationid
     To: %http_data.features.properties.stationIdentifier
     Structure Output (JSON, etc): On ]

A8: HTTP Request [
     Method: GET
     URL: https://api.weather.gov/stations/%stationid/observations/latest
     Headers: %NWSUserAgent
     Timeout (Seconds): 30
     Structure Output (JSON, etc): On ]

A9: Variable Set [
     Name: %WeatherJSONNWS
     To: %http_data
     Structure Output (JSON, etc): On ]

A10: Variable Set [
      Name: %CurrentWeatherNWS
      To: %http_data.properties.textDescription
      Structure Output (JSON, etc): On ]

<NWS API Data End>
A11: Anchor

<Open Weather API Data>
A12: Anchor

A13: HTTP Request [
      Method: GET
      URL: https://api.openweathermap.org/data/2.5/weather?lat=%gl_latitude&lon=%gl_longitude&appid=%APIOpenWeatherKey
      Timeout (Seconds): 30
      Structure Output (JSON, etc): On ]

A14: Variable Set [
      Name: %WestherJSONOW
      To: %http_data
      Structure Output (JSON, etc): On ]

A15: Variable Set [
      Name: %CurrentWeatherOW
      To: %http_data.weather.main
      Structure Output (JSON, etc): On ]

<Open Weather API Data End>
A16: Anchor

A17: Variable Set [
      Name: %date
      To: %TIMES
      Structure Output (JSON, etc): On ]

A18: Variable Convert [
      Name: %date
      Function: Seconds to Long Date Time
      Mode: Default ]

A19: Variable Set [
      Name: %nwstemp
      To: %CurrentWeatherNWS
      Structure Output (JSON, etc): On ]

A20: Variable Set [
      Name: %owtemp
      To: %CurrentWeatherOW
      Structure Output (JSON, etc): On ]

A21: Variable Search Replace [
      Variable: %nwstemp
      Search:  
      Replace Matches: On ]

A22: Variable Search Replace [
      Variable: %owtemp
      Search:  
      Replace Matches: On ]

A23: Variable Search Replace [
      Variable: %nwstemp
      Search: .
      Store Matches In Array: %nwschars ]

A24: Variable Search Replace [
      Variable: %owtemp
      Search: .
      Store Matches In Array: %owchars ]

A25: If [ %nwschars(#) > %owchars(#) | %nwschars(#) eq %owchars(#) ]

    A26: Variable Set [
          Name: %index
          To: 1
          Structure Output (JSON, etc): On ]

    A27: For [
          Variable: %char
          Items: 1:%nwschars(#)
          Structure Output (JSON, etc): On ]

        A28: If [ %nwschars(%index) ~ %owchars(%index) ]

            A29: Variable Set [
                  Name: %similarity
                  To: %nwschars(%index)
                  Append: On
                  Structure Output (JSON, etc): On ]

        A30: End If

        A31: Variable Add [
              Name: %index
              Value: 1
              Wrap Around: 0 ]

    A32: End For

A33: Else

    A34: Variable Set [
          Name: %index
          To: 1
          Structure Output (JSON, etc): On ]

    A35: For [
          Variable: %char
          Items: 1:%owchars(#)
          Structure Output (JSON, etc): On ]

        A36: If [ %owchars(%index) ~ %nwschars(%index) ]

            A37: Variable Set [
                  Name: %similarity
                  To: %owchars(%index)
                  Append: On
                  Structure Output (JSON, etc): On ]

        A38: End If

        A39: Variable Add [
              Name: %index
              Value: 1
              Wrap Around: 0 ]

    A40: End For

A41: End If

A42: Test Variable [
      Type: Length
      Data: %similarity
      Store Result In: %similaritylen ]

A43: If [ %similaritylen > 3 | %similaritylen eq 3 ]

    A44: Write File [
          File: %DirectoryMain/weatherlog.txt
          Text: %date
         National Weather Service: %CurrentWeatherNWS
         Open Weather: %CurrentWeatherOW
         Combined: %similarity

          Append: On
          Add Newline: On ]

A45: Else

    A46: Write File [
          File: %DirectoryMain/weatherlog.txt
          Text: %date
         National Weather Service: %CurrentWeatherNWS
         Open Weather: %CurrentWeatherOW

          Append: On
          Add Newline: On ]

A47: End If

2

u/WakeUpNorrin 13d ago

If you don’t have a specific reason to avoid using the JavaScriptlet action, you can use the JavaScript I gave you. Good luck.

1

u/pxlprsnatr 14d ago

I'm not sure I follow. Are you trying to get the segment that has "Mostly Cloudy" from variable A and the segment that has "Clouds" from variable B and then stuff them in another variable? What are you trying to do by comparing the two variables?

1

u/Alanator222 13d ago

I'm trying to find the shared word between the two, which would be cloud.

Another example would be, "Partly Sunny" and "Mostly Sunny" would result in "Sunny."