Skip to main content

Tells if, and how hard, it's raining at any location in Singapore

Project description

sgrain

Tells if it's currently raining at a given latitude and longitude in Singapore, or if there's rain nearbie. The data comes from rain radar images at https://www.weather.gov.sg/weather-rain-area-50km/. The images are updated every 5 minutes.

Link to project: https://github.com/nlannuzel/sgrain

Package installation

From PyPI

pip3 install nlannuzel.sgrain

Package usage

With the built-in script:

LAT=1.313383
LONG=103.815203
# shows rain here:
rain-intensity-at -a $LAT -o $LONG -n
# shows distance to nearest rain spot:
nearest-rain -a $LAT -o $LONG -c $(pwd) -n 1
# shows location of nearest rain spot:
nearest-rain -a $LAT -o $LONG -c $(pwd) -n 1 -l
# shows location of nearest rain spot of significant size (size of more than 20 pixels on the rain map):
nearest-rain -a $LAT -o $LONG -c $(pwd) -n 20 -l

With a custom script:

#!/usr/bin/env python3

from nlannuzel.sgrain.rain import RainAreas
from nlannuzel.sgrain.geo import Location

rain = RainAreas()

# Download the latest radar image from https://www.weather.gov.sg/weather-rain-area-50km/
rain.load_image()

# https://maps.app.goo.gl/9aA7i8chryYwuhUT8
picnic_spot = Location(1.313383, 103.815203)

# Returns a number between 0 and 31
intensity = rain.intensity_at(picnic_spot)

message = f"At location {picnic_spot}, time {rain.image_time}: "
if intensity == 0:
    rain.remove_blobs(max_size = 10)
    nearest_rain = rain.nearest_rain_location(picnic_spot)
    if nearest_rain is None:
        message += "it's not raining around here."
    else:
        d = picnic_spot.distance_to(nearest_rain)
        message += f"it's not raining, but there's rain about {d:.2f}km away."
elif intensity < 10:
	message += "it's raining a little bit ({intensity}), bring a umbrella."
else:
	message += "it's raining a lot ({intensity}), cancel the picnic."
print(message)

In home-assistant

Log into the home-assistant box, for example by connecting to the console of the VM where HA is installed and running. Then, attach to the homeasistant container:

docker exec -ti homeassistant bash

You are now inside the homeassistant container. Create a new venv under /config.

cd /config
python3 -m venv python_venv

Install this package inside the venv

cd python_venv
./bin/pip3 install nlannuzel.sgrain

Verify that the module works, this command should display a number between 0 and 31:

./bin/rain-intensity-at -a 1.313383 -o 103.815203 -p 1

Open the home-assistant GUI in a browser. If not already done, install the File Editor add-on. Open the configuration.yaml file (/homeassistant/configuration.yaml), and add a section like below:

command_line:
  - sensor:
      command: /config/venv/bin/python3 /config/venv/bin/rain-intensity-at -a LATITUDE -o LONGITUDE -p 1 -n 1
      name: rain-sensor
      unique_id: rain
      scan_interval: 300
      icon: mdi:weather-pouring
      availability: 1
      #device_class: None
      state_class: MEASUREMENT
      value_template: '{{ value | float | round(2) }}'
  - sensor:
      command: /config/venv/bin/python3 /config/venv/bin/nearest-rain -a LATITUDE -o LONGITUDE -n 10
      name: rain-sensor-distance
      unique_id: rain-distance
      scan_interval: 300
      icon: mdi:weather-pouring
      availability: 1
      device_class: distance
      unit_of_measurement: km
      state_class: MEASUREMENT
      value_template: '{{ value | float | round(2) }}'

Be sure to replace LATITUDE and LONGITUDE by the the actual coordinates. Finally, in the "developer tools", validate and then reload the config. You should have a new sensor available in Home Assistant.

Command line sensors are explained in HA website: https://www.home-assistant.io/integrations/command_line/

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nlannuzel_sgrain-1.0.9.tar.gz (37.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nlannuzel_sgrain-1.0.9-py3-none-any.whl (25.1 kB view details)

Uploaded Python 3

File details

Details for the file nlannuzel_sgrain-1.0.9.tar.gz.

File metadata

  • Download URL: nlannuzel_sgrain-1.0.9.tar.gz
  • Upload date:
  • Size: 37.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for nlannuzel_sgrain-1.0.9.tar.gz
Algorithm Hash digest
SHA256 3d0b9f4879216556ea516a3195e18b824eb476b674b2fd2fcf45007b21b9f4cc
MD5 ee80609fbaaa923319edfddb2f4514ad
BLAKE2b-256 8587168bb4dd867231e446571835d0d6a45db11c9a5ff1315bd6517ed6fa86a2

See more details on using hashes here.

File details

Details for the file nlannuzel_sgrain-1.0.9-py3-none-any.whl.

File metadata

File hashes

Hashes for nlannuzel_sgrain-1.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 d00e515f7313f7f07e2dd72c2b10b1376fec1b56e7b7cabe71676f9dfcd76aa9
MD5 cc497ab7a75303c9e12202956783de59
BLAKE2b-256 57ab0ac0950350aac321fe21c892b7fa5788aa45943a6f0af73820b0007bcabd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page