Skip to content

Instantly share code, notes, and snippets.

@orpolaczek
Last active November 13, 2023 19:03
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orpolaczek/5d3e6388f30c5b71df83dabdc237bd18 to your computer and use it in GitHub Desktop.
Save orpolaczek/5d3e6388f30c5b71df83dabdc237bd18 to your computer and use it in GitHub Desktop.
Home Assistant - 'Red Alert' sensor
Oct 2023 - SEE UPDATED VERSION IN THE COMMENTS BELOW
Update no. 2 -> see a proper integration build by Amit here:https://github.com/amitfin/oref_alert. It's the way to go
# Create a REST sensor (built in) - place the following lines in `sensors.yaml`
- platform: rest
resource: https://www.oref.org.il/WarningMessages/alert/alerts.json
scan_interval: 5
value_template: "{{ value_json.data if value_json is defined else [] }}"
headers:
Accept: application/json
Content-Type: application/json
x-requested-with: XMLHttpRequest
Referer: https://www.oref.org.il//12481-he/Pakar.aspx
# Use a template to generate binary sensors - add this in your configuration.yaml
sensor: !include sensors.yaml
template:
- binary_sensor:
- name: "Alert Givatayim"
state: "{{ 'גבעתיים' in states('sensor.rest_sensor') or 'רמת גן - מערב' in states('sensor.rest_sensor')}}"
- name: "Alert Otef Gaza"
state: "{{ 'ניר עם' in states('sensor.rest_sensor') or 'כפר עזה' in states('sensor.rest_sensor') or 'מפלסים' in states('sensor.rest_sensor') }}"
@orpolaczek
Copy link
Author

Example:
image

@hllhll
Copy link

hllhll commented May 17, 2021

I Sugguest doing
value_template: "{{ value_json.data if value else [] }}"
Insead for not getting the warning messeges

@orpolaczek
Copy link
Author

I Sugguest doing
value_template: "{{ value_json.data if value else [] }}"
Insead for not getting the warning messeges

Great suggestion! I revised.

@AryehGielchinsky
Copy link

AryehGielchinsky commented May 19, 2021

I'm getting any error, any ideas what I'm going wrong?
Invalid config for [template]: [binary_sensor] is an invalid option for [template]. Check: template->binary_sensor. (See /config/configuration.yaml, line 15).

configuration.yaml

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Text to speech
tts:
  - platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

# Use a template to generate binary sensors - add this in your configuration.yaml
sensor: !include sensors.yaml
template:
    - binary_sensor:
        - name: "Alert Givatayim"
          state: "{{ 'גבעתיים' in states('sensor.rest_sensor') or 'רמת גן - מערב' in states('sensor.rest_sensor')}}"
        - name: "Alert Otef Gaza"
          state: "{{ 'ניר עם' in states('sensor.rest_sensor') or 'כפר עזה' in states('sensor.rest_sensor') or 'מפלסים' in states('sensor.rest_sensor') }}"

sensors.yaml

# Create a REST sensor (built in) - place the following lines in `sensors.yaml`
- platform: rest
  resource: https://www.oref.org.il/WarningMessages/alert/alerts.json
  scan_interval: 5
  value_template: "{{ value_json.data if value else [] }}"
  headers:
    Accept: application/json
    Content-Type: application/json
    x-requested-with: XMLHttpRequest
    Referer: https://www.oref.org.il//12481-he/Pakar.aspx

@yalmog99
Copy link

This solution works great but a few month ago it stops working and I am getting Unknown value on the rest sensor.
Can someone help ?

@regevbr
Copy link

regevbr commented May 19, 2022

This solution works great but a few month ago it stops working and I am getting Unknown value on the rest sensor. Can someone help ?

Change the value_template to be "{{ value_json.data if value_json is defined else [] }}"

@orpolaczek I think this is the best method, maybe you should consider updating the gist

@orpolaczek
Copy link
Author

This solution works great but a few month ago it stops working and I am getting Unknown value on the rest sensor. Can someone help ?

Change the value_template to be "{{ value_json.data if value_json is defined else [] }}"

@orpolaczek I think this is the best method, maybe you should consider updating the gist

Thank you for that. Updated

@yalmog99
Copy link

yalmog99 commented Aug 5, 2022

Hi,
The rest sensor value is always on [] although in the telegram channel of pikud haoref there are alerts.
Any idea how to fix it ?

@ofeksit
Copy link

ofeksit commented May 10, 2023

Same for me.

@orpolaczek
Copy link
Author

Same for me.

Try increasing the refresh interval

@noamokman
Copy link

I used the rest integration, same thing but easier to extract multiple sensors and I don't need an extra sensor for the list.

rest:
  - scan_interval: 10
    resource: https://www.oref.org.il/WarningMessages/alert/alerts.json
	headers:
	  Accept: application/json
	  Content-Type: application/json
	  x-requested-with: XMLHttpRequest
	  Referer: https://www.oref.org.il//12481-he/Pakar.aspx
	sensor:
	  - name: 'Oref Alarm'
	    value_template: '{% set locations = (value_json.data if value_json is defined else []) %}{{ "גבעתיים" in locations or "רמת גן - מערב" in locations}}'

@orpolaczek
Copy link
Author

orpolaczek commented Oct 16, 2023

Update over update - Amit has built a proper integration that makes this solution redundant.
Please replatform to use his solution.

I'll leave the server up until the end of the current war and then it'll be deleted.

Stay safe!

==============================

Updating here -
I know many of you see cases where alerts are missed.

I believe it's due to the way Pikud HaOref cache the alerts JSON file.

I've created a server that merges the 'live' alerts from alerts.json and the historical alerts from GetAlarmsHistory.aspx and returns one, unified, array of all active alerts in the past 30 seconds. That way you won't miss alerts (maybe only get them in a slight delay).

I'm running this server free of charge. You can use it by adding a 'rest' sensor as follows:

- platform: rest
  name: restalert
  resource: https://DONT_USE_ME
  scan_interval: 2
  value_template: "{{ (value_json.active_alerts if value_json is defined else []) | count }}"
  json_attributes:
    - active_alerts
  verify_ssl: false
  headers:
    Referer: https://www.oref.org.il/
    X-Requested-With: XMLHttpRequest
    Content-Type: 'application/json'

It will publish a new sensor, with an attribute that contains all active alerts.

Then, you can set a binary sensor either in Hebrew or English (or Russian or Arabic) by adding a binary_sensor:

- name: "Alert Tel-Aviv"
   state: "{{ state_attr('sensor.restalert', 'active_alerts') is search('Tel Aviv')}}"
- name: "Alert Bnei-Brak"
   state: "{{ state_attr('sensor.restalert', 'active_alerts') is search('בני ברק') }}"

If you want to test it out, set the URL to https://----- instead of https://r------
and it'll return today's historical alerts
please be mindful and set the refresh interval for test alerts to be 3600 seconds or less. It's the same data.
Rate limit is enforced on the test endpoint to save me from excessive cost :)

@orpolaczek
Copy link
Author

I used the rest integration, same thing but easier to extract multiple sensors and I don't need an extra sensor for the list.

rest:
  - scan_interval: 10
    resource: https://www.oref.org.il/WarningMessages/alert/alerts.json
	headers:
	  Accept: application/json
	  Content-Type: application/json
	  x-requested-with: XMLHttpRequest
	  Referer: https://www.oref.org.il//12481-he/Pakar.aspx
	sensor:
	  - name: 'Oref Alarm'
	    value_template: '{% set locations = (value_json.data if value_json is defined else []) %}{{ "גבעתיים" in locations or "רמת גן - מערב" in locations}}'

It’s a valid approach.
however - I prefer one sensor with all active alerts, so I can configure more areas down the line. That’s the only benefit over your implementation.

@yalmog99
Copy link

yalmog99 commented Oct 17, 2023

Thanks Or, much appriciated !
I am starting to use your URL.
Update after checking:
I think the state of the sensor.restalert should be the value of the Json and not the alerts count (It will be easier to debug since the data will be visible in the logbook).

Updating here - I know many of you see cases where alerts are missed.

I believe it's due to the way Pikud HaOref cache the alerts JSON file.

I've created a server that merges the 'live' alerts from alerts.json and the historical alerts from GetAlarmsHistory.aspx and returns one, unified, array of all active alerts in the past 30 seconds. That way you won't miss alerts (maybe only get them in a slight delay).

I'm running this server free of charge. You can use it by adding a 'rest' sensor as follows:

- platform: rest
  name: restalert
  resource: https://redalert.pci.co.il/get_active_alerts
  scan_interval: 2
  value_template: "{{ (value_json.active_alerts if value_json is defined else []) | count }}"
  json_attributes:
    - active_alerts
  verify_ssl: false
  headers:
    Referer: https://www.oref.org.il/
    X-Requested-With: XMLHttpRequest
    Content-Type: 'application/json'

It will publish a new sensor, with an attribute that contains all active alerts.

Then, you can set a binary sensor either in Hebrew or English (or Russian or Arabic) by adding a binary_sensor:

- name: "Alert Tel-Aviv"
   state: "{{ state_attr('sensor.restalert', 'active_alerts') is search('Tel Aviv')}}"
- name: "Alert Bnei-Brak"
   state: "{{ state_attr('sensor.restalert', 'active_alerts') is search('בני ברק') }}"

If you want to test it out, set the URL to https://redalert.pci.co.il/get_test_alerts instead of https://redalert.pci.co.il/get_active_alerts and it'll return today's historical alerts

@orpolaczek
Copy link
Author

Thanks Or, much appriciated ! I am starting to use your URL. Update after checking: I think the state of the sensor.restalert should be the value of the Json and not the alerts count (It will be easier to debug since the data will be visible in the logbook).

Updating here - I know many of you see cases where alerts are missed.
I believe it's due to the way Pikud HaOref cache the alerts JSON file.
I've created a server that merges the 'live' alerts from alerts.json and the historical alerts from GetAlarmsHistory.aspx and returns one, unified, array of all active alerts in the past 30 seconds. That way you won't miss alerts (maybe only get them in a slight delay).
I'm running this server free of charge. You can use it by adding a 'rest' sensor as follows:

- platform: rest
  name: restalert
  resource: https://redalert.pci.co.il/get_active_alerts
  scan_interval: 2
  value_template: "{{ (value_json.active_alerts if value_json is defined else []) | count }}"
  json_attributes:
    - active_alerts
  verify_ssl: false
  headers:
    Referer: https://www.oref.org.il/
    X-Requested-With: XMLHttpRequest
    Content-Type: 'application/json'

It will publish a new sensor, with an attribute that contains all active alerts.
Then, you can set a binary sensor either in Hebrew or English (or Russian or Arabic) by adding a binary_sensor:

- name: "Alert Tel-Aviv"
   state: "{{ state_attr('sensor.restalert', 'active_alerts') is search('Tel Aviv')}}"
- name: "Alert Bnei-Brak"
   state: "{{ state_attr('sensor.restalert', 'active_alerts') is search('בני ברק') }}"

If you want to test it out, set the URL to https://redalert.pci.co.il/get_test_alerts instead of https://redalert.pci.co.il/get_active_alerts and it'll return today's historical alerts

It's that way on purpose, because all the alerts are exposed under the state attributes.
If you put the entire response in the state itself you'll get a really big state that will be hard to render/read.
Regardless - it's up to you :)

@orpolaczek
Copy link
Author

Thanks Or, much appriciated ! I am starting to use your URL. Update after checking: I think the state of the sensor.restalert should be the value of the Json and not the alerts count (It will be easier to debug since the data will be visible in the logbook).

Updating here - I know many of you see cases where alerts are missed.
I believe it's due to the way Pikud HaOref cache the alerts JSON file.
I've created a server that merges the 'live' alerts from alerts.json and the historical alerts from GetAlarmsHistory.aspx and returns one, unified, array of all active alerts in the past 30 seconds. That way you won't miss alerts (maybe only get them in a slight delay).
I'm running this server free of charge. You can use it by adding a 'rest' sensor as follows:

- platform: rest
  name: restalert
  resource: https://redalert.pci.co.il/get_active_alerts
  scan_interval: 2
  value_template: "{{ (value_json.active_alerts if value_json is defined else []) | count }}"
  json_attributes:
    - active_alerts
  verify_ssl: false
  headers:
    Referer: https://www.oref.org.il/
    X-Requested-With: XMLHttpRequest
    Content-Type: 'application/json'

It will publish a new sensor, with an attribute that contains all active alerts.
Then, you can set a binary sensor either in Hebrew or English (or Russian or Arabic) by adding a binary_sensor:

- name: "Alert Tel-Aviv"
   state: "{{ state_attr('sensor.restalert', 'active_alerts') is search('Tel Aviv')}}"
- name: "Alert Bnei-Brak"
   state: "{{ state_attr('sensor.restalert', 'active_alerts') is search('בני ברק') }}"

If you want to test it out, set the URL to https://redalert.pci.co.il/get_test_alerts instead of https://redalert.pci.co.il/get_active_alerts and it'll return today's historical alerts

Hi buddy,
please see my update about moving over to Amit's integration. It's the way to go

@yalmog99
Copy link

Thanks Or, much appriciated ! I am starting to use your URL. Update after checking: I think the state of the sensor.restalert should be the value of the Json and not the alerts count (It will be easier to debug since the data will be visible in the logbook).

Updating here - I know many of you see cases where alerts are missed.
I believe it's due to the way Pikud HaOref cache the alerts JSON file.
I've created a server that merges the 'live' alerts from alerts.json and the historical alerts from GetAlarmsHistory.aspx and returns one, unified, array of all active alerts in the past 30 seconds. That way you won't miss alerts (maybe only get them in a slight delay).
I'm running this server free of charge. You can use it by adding a 'rest' sensor as follows:

- platform: rest
  name: restalert
  resource: https://redalert.pci.co.il/get_active_alerts
  scan_interval: 2
  value_template: "{{ (value_json.active_alerts if value_json is defined else []) | count }}"
  json_attributes:
    - active_alerts
  verify_ssl: false
  headers:
    Referer: https://www.oref.org.il/
    X-Requested-With: XMLHttpRequest
    Content-Type: 'application/json'

It will publish a new sensor, with an attribute that contains all active alerts.
Then, you can set a binary sensor either in Hebrew or English (or Russian or Arabic) by adding a binary_sensor:

- name: "Alert Tel-Aviv"
   state: "{{ state_attr('sensor.restalert', 'active_alerts') is search('Tel Aviv')}}"
- name: "Alert Bnei-Brak"
   state: "{{ state_attr('sensor.restalert', 'active_alerts') is search('בני ברק') }}"

If you want to test it out, set the URL to https://redalert.pci.co.il/get_test_alerts instead of https://redalert.pci.co.il/get_active_alerts and it'll return today's historical alerts

Hi buddy, please see my update about moving over to Amit's integration. It's the way to go

Thanks, I have stopped using your server.
BTW, what's the advantage of Amit's integration over the original code in this thread ?
Does Amit's integration create request directly to https://www.oref.org.il/WarningMessages/alert/alerts.json or via another proxy server (like the one you suggested before at redalert.pci.co.il) ?

@orpolaczek
Copy link
Author

Thanks Or, much appriciated ! I am starting to use your URL. Update after checking: I think the state of the sensor.restalert should be the value of the Json and not the alerts count (It will be easier to debug since the data will be visible in the logbook).

Updating here - I know many of you see cases where alerts are missed.
I believe it's due to the way Pikud HaOref cache the alerts JSON file.
I've created a server that merges the 'live' alerts from alerts.json and the historical alerts from GetAlarmsHistory.aspx and returns one, unified, array of all active alerts in the past 30 seconds. That way you won't miss alerts (maybe only get them in a slight delay).
I'm running this server free of charge. You can use it by adding a 'rest' sensor as follows:

- platform: rest
  name: restalert
  resource: https://redalert.pci.co.il/get_active_alerts
  scan_interval: 2
  value_template: "{{ (value_json.active_alerts if value_json is defined else []) | count }}"
  json_attributes:
    - active_alerts
  verify_ssl: false
  headers:
    Referer: https://www.oref.org.il/
    X-Requested-With: XMLHttpRequest
    Content-Type: 'application/json'

It will publish a new sensor, with an attribute that contains all active alerts.
Then, you can set a binary sensor either in Hebrew or English (or Russian or Arabic) by adding a binary_sensor:

- name: "Alert Tel-Aviv"
   state: "{{ state_attr('sensor.restalert', 'active_alerts') is search('Tel Aviv')}}"
- name: "Alert Bnei-Brak"
   state: "{{ state_attr('sensor.restalert', 'active_alerts') is search('בני ברק') }}"

If you want to test it out, set the URL to https://redalert.pci.co.il/get_test_alerts instead of https://redalert.pci.co.il/get_active_alerts and it'll return today's historical alerts

Hi buddy, please see my update about moving over to Amit's integration. It's the way to go

Thanks, I have stopped using your server. BTW, what's the advantage of Amit's integration over the original code in this thread ? Does Amit's integration create request directly to https://www.oref.org.il/WarningMessages/alert/alerts.json or via another proxy server (like the one you suggested before at redalert.pci.co.il) ?

No. you're losing the proxy advantage, but if you have a stable internet connection, even if you missed the alarm from alerts.json you'll still get it processed from the historical alerts json, so you won't lose alerts.

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