Tasker weather notification tutorial

Fancy some training on .json, subroutines, arrays and loops?

AutoNotification Table has been updated twice since I started the Tasker weather tutorial and this resulted in few improvements. Instead of showing you the final version only, I’ll also show you my first solution. Mainly, because I promised you the subroutines.

The profile is rather simple when we are no longer connected to a home WIFI (feel free to add more networks or create the dynamic location profile) a request is made to pull the current weather update with data reaching 12h ahead.

Tasker weather tutorial – Weather API

Profile Weather Alert On D/C
Profile: Weather Alert On D/C 
	State: Not Wifi Connected [ SSID:SSID MAC:* IP:* ]
Enter: Create Weather Alert 
	A1: HTTP Get [ Server:Port:https://api.openweathermap.org/data/2.5/
		forecast?lat=LAT&lon=LON&appid=APIKEY&units=metric&cnt=5 
		Path: Attributes: Cookies: User Agent: Timeout:10 
		Mime Type:text/plain Output File:Download/weather.json 
		Trust Any Certificate:Off ] 
	A2: Read File [ File:Download/weather.json To Var:%json ]  
	A3: AutoTools Json Read [ Configuration:Input Format: Json
		Json: %json
		Fields: list.main.temp(),list.weather.main(),list.dt(),list.weather.icon(),city.name
		Separator: , Timeout (Seconds):60 ] 
	A4: For [ Variable:%time Items:%list_dt() ] 
	A5: AutoTools Time [ Configuration:Dates to Format: %time
		Format: HH:mm
		Use Seconds: true
		Output Variable: timec Timeout (Seconds):30 ] 
	A6: Array Push [ Variable Array:%wtime Position:5 Value:%timecdate Fill Spaces:On ] 
	A7: End For 
	A8: For [ Variable:%icon Items:%list_weather_icon() ] 
	A9: Perform Task [ Name:Icons - Subroutine Priority:%priority+1 
		Parameter 1 (%par1):%icon Parameter 2 (%par2): 
		Return Value Variable:%readyicon Stop:Off ] 
	A10: Array Push [ Variable Array:%wicon Position:5 Value:%readyicon Fill Spaces:On ] 
	A11: End For 
		
	A12: AutoNotification Table [ Configuration:Title: Weather:
		Texts: %wtime1,%wtime2,%wtime3,%wtime4,%wtime5,		
		%list_main_temp1°C,%list_main_temp2°C,%list_main_temp3°C,%list_main_temp4°C,%list_main_temp5°C,
		%wicon1,%wicon2,%wicon3,%wicon4,%wicon5,
		%list_weather_main1,%list_weather_main2,%list_weather_main3,%list_weather_main4,%list_weather_main5
		Separator: ,
		SubText: %city_name
		Number of Columns: 5
		Cell Widths: 70
		Cell Height: 17
		Text Alignment: Center
		Status Bar Icon: /storage/emulated/0/Tasker/icons/weather/%list_weather_icon1.png
		Big Icon: /storage/emulated/0/Tasker/icons/weather/%list_weather_icon1.png
		Background Color: #E0F7FA
		Text Color: #D84315
		Command On Touch: openweather
		ID: weather
		Priority: 2 Timeout (Seconds):60 ] 
	
	A13: [X] AutoNotification Table [ Configuration:Title: Weather:
		Texts: %wtime1,%wtime2,%wtime3,%wtime4,%wtime5,
		%list_main_temp1°C,%list_main_temp2°C,%list_main_temp3°C,%list_main_temp4°C,%list_main_temp5°C,
		/storage/emulated/0/Tasker/icons/weather/%list_weather_icon1.png,
		/storage/emulated/0/Tasker/icons/weather/%list_weather_icon2.png,
		/storage/emulated/0/Tasker/icons/weather/%list_weather_icon3.png,
		/storage/emulated/0/Tasker/icons/weather/%list_weather_icon4.png,
		/storage/emulated/0/Tasker/icons/weather/%list_weather_icon5.png,
		%list_weather_main1,%list_weather_main2,%list_weather_main3,%list_weather_main4,%list_weather_main5
		Separator: ,
		SubText: %city_name
		Number of Columns: 5
		Cell Widths: 70
		Cell Height: 17
		Text Alignment: Center
		Status Bar Icon: /storage/emulated/0/Tasker/icons/weather/%list_weather_icon1.png
		Big Icon: /storage/emulated/0/Tasker/icons/weather/%list_weather_icon1.png
		Background Color: #E0F7FA
		Text Color: #D84315
		Command On Touch: openweather
		ID: weather
		Priority: 2 Timeout (Seconds):60 ]  

Create an account with openweather.org. Their API is free for up to 60 calls a day, and it’s simple to use. Once you have the account get your API key and take a look at the link for the HTTP GET request.

https://api.openweathermap.org/data/2.5/forecast?lat=YOUR_LATITUDE&lon=YOUR_LONGITUDE&appid=YOURKEY&units=metric&cnt=5

A few things to point out, &units=imperial if you want F degrees, and &cnt=5 tells you how many 3h intervals you want to download (5=current + 4 x 3h). Substitute the YOUR_LONGITUDE and YOUR_LONGITUDE from google maps. The reason why I’m using a fixed location rather than using get location is, that I want to receive a weather update the minute I leave the home rather than waiting an additional minute for a GPS fix. Just in case I have to get back to get an umbrella.

A1

The HTTP get will save a .json file containing a data use plain text as data type and save it as weather.json. The file will be created and saved.

A2

To use this data we have to load it to the variable using Read File action.

A3

The .json file is a very specific data format. Take a look at the https://jsonviewer.stack.hu/. Paste the data and take a look how data is organized and stored. This will help you understand how to access these cells using AutoTools Json Read action.

For my notification I’m using the following:

  • list.main.temp() – temperature (mine in C)
  • list.weather.main() – condition name
  • list.dt() – Time in sec
  • list.weather.icon() – icon code
  • city.name – location name

As you can see, the apart from the city.name, all data is stored in arrays. This means we will get multiple values. To access the tree structure we have to uncheck the ‘simple’ option. We are unable to use the simple methods as names are not unique (see main and weather word). The AutoTools Json Read action will load the cells into the arrays, but it will change the name of the relevant variables. Ie: list.main.temp() will become list_temp_main().

A4-A7

The FOR loop. The time is stored as a value in seconds. I’m only interested in the HH:MM format to be displayed. I have to convert each variable inside the array to the correct value. The %time variable will take the value of each variable from the array %list_dt() and will perform the conversion using AutoTools Time action.

AutoTools Time is set to operate in seconds (not milliseconds) and will ‘get rid off’ the date from the converted value. I used the custom output array %timecdate() – this is where the value will be stored.

Before we can take care of another entry in the %list_dt() we have to store this value. I will push the value to the new array %wtime() starting from the bottom. I have 5 values so my I will be pushing all the values from the 5th position moving the previous entries up. This way the final order in %wtime() will be the same as %list_dt().

If you want to know more about loops, subprocesses and flow control check out this video:

A9-A11 (optional)

By the time you see this, this loop will be obsolete and the AutoNotification Table supports pictures directly. I decided to keep this regardless as the subroutine exercise. I will skip the loop description, as it does things in the same fashion as A4-A7. Instead of converting the time we take each value in the array %list_weather_icon() and change the value of it to an emoticon that corresponds with the weather condition.

Perform Task action will take each value assigned to %icon from the FOR loop and it will pass it as %par1 to another task.

Subroutine

Task - Subroutine
Icons - Subroutine 
	A1: Variable Set [ Name:%readyicon To:☀
		Recurse Variables:Off Do Maths:Off Append:Off ] 
		If [ %par1 ~R 01 ]
	A2: Variable Set [ Name:%readyicon To:⛅ 
		Recurse Variables:Off Do Maths:Off Append:Off ] 
		If [ %par1 ~R 02 ]
	A3: Variable Set [ Name:%readyicon To:☁ 
		Recurse Variables:Off Do Maths:Off Append:Off ] 
		If [ %par1 ~R 03 ]
	A4: Variable Set [ Name:%readyicon To:☁ 
		Recurse Variables:Off Do Maths:Off Append:Off ] 
		If [ %par1 ~R 04 ]
	A5: Variable Set [ Name:%readyicon To:? 
		Recurse Variables:Off Do Maths:Off Append:Off ] 
		If [ %par1 ~R 09 ]
	A6: Variable Set [ Name:%readyicon To:? 
		Recurse Variables:Off Do Maths:Off Append:Off ] 
		If [ %par1 ~R 10 ]
	A7: Variable Set [ Name:%readyicon To:? 
		Recurse Variables:Off Do Maths:Off Append:Off ] 
		If [ %par1 ~R 11 ]
	A8: Variable Set [ Name:%readyicon To:? 
		Recurse Variables:Off Do Maths:Off Append:Off ] 
		If [ %par1 ~R 13 ]
	A9: Variable Set [ Name:%readyicon To:? 
		Recurse Variables:Off Do Maths:Off Append:Off ] 
		If [ %par1 ~R 50 ]
	A10: Return [ Value:%readyicon Stop:Off ] 

The task checks the value of the %par1 and sets the variable to a defined weather condition. Once the correct value is assigned the information is returned to the loop via Return action (%returnicon) and pushed inside a new array.

The values for the icons and weather conditions available has been taken from the API help.

EDIT extra vibration on the rainy days:

I have added another 2 actions to this loop. Variable %rainalarm is set to a specific vibration pattern. If the %icon contains 9-11 in the name (matches Regex), it’s gonna rain. A more pronounced vibration is set. Otherwise, vibration is less apparent. To get the AutoNotification to vibrate in an alternative way, use the %rainalarm in the vibration pattern option of the AutoNotification Table action.

My set up checks each value of the %icon within the loop for rain. But you can narrow it down to a specific time slot by using %wicon(x) where x is the time slot. %wicon1 would be the current weather update. If you want to go for specific time slots, the Variable set actions have to be placed outside of the loop.

A12 or A13

We have the data formatted correctly already, all we need is plug that into the AutoNotification Table. There are 2 ways we can substitute the icons. If you were using the subroutine, the icons are already stored in the relevant arrays and your text field will look like this:

Icon order
%wtime1,%wtime2,%wtime3,%wtime4,%wtime5, 
%list_main_temp1°C,%list_main_temp2°C,
%list_main_temp3°C,%list_main_temp4°C,%list_main_temp5°C,
%wicon1,%wicon2,%wicon3,%wicon4,%wicon5,
%list_weather_main1,%list_weather_main2,%list_weather_main3,
%list_weather_main4,%list_weather_main5

(bear in mind I have added the breaks to make the formatting clear, it should all be on one line separated by comma)

If you want to use custom pictures, place the day and night icon set into one folder. Icons should be named according to the API documentation. Instead of supplying the arrays, you have to add the path to the file:

Icon order - custom pix
%wtime1,%wtime2,%wtime3,%wtime4,%wtime5,
 %list_main_temp1°C,%list_main_temp2°C,%list_main_temp3°C,
%list_main_temp4°C,%list_main_temp5°C,
 /storage/emulated/0/Tasker/icons/weather/%list_weather_icon1.png,
 /storage/emulated/0/Tasker/icons/weather/%list_weather_icon2.png,
 /storage/emulated/0/Tasker/icons/weather/%list_weather_icon3.png,
 /storage/emulated/0/Tasker/icons/weather/%list_weather_icon4.png,
 /storage/emulated/0/Tasker/icons/weather/%list_weather_icon5.png,
 %list_weather_main1,%list_weather_main2,%list_weather_main3,
%list_weather_main4,%list_weather_main5

You can use the same method to substitute the icons for the big icon and notification icon. The %list_weather_main1 variable contains the current condition and it’s the best for that purpose. Lastly, add the %city_name to the sub-text field and set the number of columns to 5.

Now, you have yourself a nice weather notification. Time to enable the context and action on touch in the Tasker weather tutorial.

Tasker weather tutorial – Open Weather Google

Profile - Open Weather Google
Profile: Open Weather Google 
	Event: AutoApps Command [ Configuration:Command Filter: openweather ]
Enter: Weather Intent 
	A1: [X] Send Intent [ Action:com.google.android.googlequicksearchbox.GOOGLE_SEARCH
		Cat:Default Mime Type: Data: Extra:String
		Extra:query:WEATHER Extra: Package: Class: Target:Activity ] 
	A2: AutoShare [ Configuration:Package: com.google.android.googlequicksearchbox
		Class: com.google.android.apps.gsa.velour.DynamicActivityTrampoline
		App: Google
		Action: Weather
		Data: dynact://velour/weather/ProxyActivity Timeout (Seconds):10 ]

AutoNotification Table action should have a ‘openweather’ action assigned on touch. We are going to use AutoApps command trigger to launch our Weather Intent task.

A1

You can pass a simple intent to launch the weather app in the Google now by setting the intent as follows:

Action: com.google.android.googlequicksearchbox.GOOGLE_SEARCH
Extra: String
Extra: query: WEATHER
Target: Activity

A2

You can also use the AutoShare to open the weather screen directly. Open the plugin, click on Get more intents and click on the Google Weather to download autoconfig. It’s that simple and it looks much better.

Tell me the weather when I leave home!

The context for this file is very simple. I’m using ‘not connected to home WIFI’ as my condition. This means the moment I leave the house I get the quick weather update. Just in time to head back and get the umbrella if there is a need for it!

Profile Weather Alert on D/C
Profile: Weather Alert On D/C 
	Restore: no
	State: Not Wifi Connected [ SSID:FASTBERRY MAC:* IP:* ]
Enter: Create Weather Alert 

Dealing with temperature #UPDATE

This section has not been included in the original tutorial. The piece is long and scary as is, but due to questions often leading to converting 15.67°C to 16°C I decided to include the small tutorial and update the final file.

We are going to need another FOR loop. In the same way as before – each element from %list_main_temp() will be rounded up with ceil function. We have to iterate the array and perform the maths operation on a %final_temp variable, then push it to a new array %final_main_temp(). A notification has to be updated accordingly as well.

Temperature loop
Temp Loop
	A1: Variable Set [ Name:%counttemp To:1 
		Recurse Variables:Off Do Maths:Off Append:Off ] 
	A2: For [ Variable:%temp Items:%list_main_temp() ] 
	A3: Variable Set [ Name:%final_temp To:ceil(%temp) 
		Recurse Variables:Off Do Maths:On Append:Off ] 
	A4: Variable Add [ Name:%counttemp Value:1 Wrap Around:0 ] 
	A5: Array Push [ Variable Array:%final_main_temp 
		Position:5 Value:%final_temp Fill Spaces:On ] 
	A6: End For 
This should result in nice integers rounded up and stored in correct order.

Conclusion

By the end of the Tasker weather tutorial, you should be a master of AutoNotification Table. This notification is perfect for displaying organised data. I hope you will find this write-up useful.

Support NotEnoughTech
A lot of time and effort goes into keeping NotEnoughTech alive! If my work helped you out, consider buying me a coffee or check out exclusive rewards available to Patreon supporters.
SHARE