Skip to content

Instantly share code, notes, and snippets.

@yaronalk
Last active November 19, 2023 11:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaronalk/c9ee61b1ff19d9db20dc038a88680e44 to your computer and use it in GitHub Desktop.
Save yaronalk/c9ee61b1ff19d9db20dc038a88680e44 to your computer and use it in GitHub Desktop.
Quick-n'-Dirty צבע אדום alerts in Home Assistant
rest:
- resource: https://www.oref.org.il/WarningMessages/History/AlertsHistory.json
scan_interval: 3
binary_sensor:
- name: "Red Alert צבע אדום"
device_class: safety
value_template: >
{% set homeCitiesTf = 'כפר עזה ,בצת' %}
{%- set ns = namespace() -%}
{%- set ns.output=[]-%}
{%- set thresholdMin = 5 -%}
{%- set homeCities = homeCitiesTf.split(',') | map('trim') | list -%}
{%- for dict in value_json -%}
{%- set dateString = dict["alertDate"] -%}
{%- set dateObj = dateString | as_datetime | as_local -%}
{%- set timeFromNow = now() - dateObj -%}
{%- if timeFromNow.days == 0 and (timeFromNow.seconds < (thresholdMin * 60)) -%}
{%- set cityList = dict["data"].split(',') | map('trim') | list -%}
{%- for alertCity in cityList -%}
{%- for homeCity in homeCities -%}
{%- if alertCity.startswith(homeCity) -%}
{%-set ns.output=ns.output+[alertCity]-%}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
{%- endif -%}
{%- endfor -%}
{{ ns.output | unique | list | count > 0 }}
@yaronalk
Copy link
Author

yaronalk commented Nov 19, 2023

This script creates a Binary Sensor in Home Assistant, which activates during a Red Alert in specified locations. Features include:

  • Customizable list of places: Edit homeCitiesTf with a comma-separated list (whitespace tolerant). It can be a single place or multiple.
  • Alert window: Set to the past 5 minutes by default (modifiable in thresholdMin).

Installation:

  1. Open the File Editor and navigate to configuration.yaml.
  2. Copy and paste the contents of this gist into the file.
  3. Reload Entities: Go to Developer Tool, select YAML tab and click REST ENTITIES AND NOTIFY SERVICES and TEMPLATE ENTITIES

Important Notes:

  • It is not recommended to decrease the scan_interval to avoid the risk of being blocked by the service.
  • For cities with multiple zones (e.g., Ashdod A, B, C, D), note that this script does not differentiate between these sub-areas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment