Each time I drive past our local fire station I see that fire danger rating sign and take a quick look. Wouldn’t it be great to have this information right in your favourite home automation system?
Luckily the NSW Rural Fire Service provides an XML feed that contains the fire danger details for today and tomorrow for districts in the state.
There is currently no simple way that I am aware of to scrap random XML in Home Assistant and convert and then display values from that XML as sensor values.
I decided to develop a small custom platform that retrieves the XML and then stores the relevant values in state attributes. You can then use template
sensors to extract the data you actually want to display.
Just a quick note on using custom components in Home Assistant: The code may or may not work in future versions of Home Assistant.
Installation
Source code, documentation and examples can be found in my Github repository exxamalte/home-assistant-customisations.
Install custom component code
There has recently been an announcement that custom component code will need to follow a different folder structure in the future. We are currently seeing a transition between the two structures, and in the current 0.88 release both ways should still work, but I’d recommend migrate to the new structure now if you’re on that release and not wait until the old structure is not accepted anymore.
Also, another change in version 0.91 required a code fix in this custom component.
Install code on versions >=0.91
In your configuration folder create subfolder <config>/custom_components
and copy the folder <a href="https://github.com/exxamalte/home-assistant-customisations/tree/master/nsw-rural-fire-service-fire-danger/config/custom_components/nsw_rural_fire_service_fire_danger">nsw_rural_fire_service_fire_danger</a>
into that new custom_components
folder.
The latest version of this custom component on GitHub works without any modifications.
Install code on versions 0.90, 0.89 and 0.88
In your configuration folder create subfolder <config>/custom_components
and copy the folder <a href="https://github.com/exxamalte/home-assistant-customisations/tree/master/nsw-rural-fire-service-fire-danger/config/custom_components/nsw_rural_fire_service_fire_danger">nsw_rural_fire_service_fire_danger</a>
into that new custom_components
folder.
Due to a change in the Home Assistant code structure from version 0.91 onwards, you will need to change one line in this custom component code:
Line 14 needs to be changed from
from homeassistant.components.rest.sensor import RestData
to
from homeassistant.components.sensor.rest import RestData
Install code on versions <0.88
In your configuration folder create subfolders <config>/custom_components/sensor
and copy the file <a href="https://github.com/exxamalte/home-assistant-customisations/blob/master/nsw-rural-fire-service-fire-danger/config/custom_components/nsw_rural_fire_service_fire_danger/sensor.py">sensor.py</a>
into that new folder and rename it to nsw_rural_fire_service_fire_danger.py
.
The small code change described in the previous section must be applied for for installations on versions <0.88, too.
Install dependencies
This custom component uses the third-party library xmltodict
which you may need to install manually from the command line, into your virtual environment if you are running Home Assistant in venv
:
pip install xmltodict
Initial setup of the retrieval sensor
Have a look at the XML feed at http://www.rfs.nsw.gov.au/feeds/fdrToban.xml and find your district. The district’s name must be configured as district_name
as shown in the following example:
sensor:
- platform: nsw_rural_fire_service_fire_danger
district_name: Greater Sydney Region
After a restart of Home Assistant, a new sensor should now show up. In this example its entity id will be sensor.fire_danger_in_greater_sydney_region
, so your entity id will be different if you have chosen a different district.
The retrieval sensor’s state will either be ok
or unknown
, depending on if it was able to retrieve data from the XML feed or not, and you will find all the interesting bits in its state attributes.
Attribute | Description |
district | District name |
region_number | Internal number of this district |
councils | List of all councils in this district |
danger_level_today | Today’s danger level |
danger_level_tomorrow | Tomorrow’s danger level |
fire_ban_today | Indicates whether there is a fire ban today |
fire_ban_tomorrow | Indicates whether there is a fire ban today |
By default, the retrieval sensor updates from the feed every 10 minutes which should be fine under normal circumstances. You can change this by modifying the line SCAN_INTERVAL = timedelta(minutes=10)
in the sensor’s code and pick a different interval.
Configuring template sensors
At a minimum you are probably interested in today’s conditions, so let’s add two new template
sensors, one that shows today’s danger level and the other one indicating whether there is a fire ban today.
In the below examples I am using my previously configured sensor from above, so please check your sensor’s entity id and replace that in the configuration below.
Fire Ban Today
The fire ban indicator is already transformed into a true/false
value above, and can be used in a template
binary sensor as follows:
binary_sensor:
- platform: template
sensors:
fire_ban_today:
friendly_name: "Fire Ban Today"
value_template: "{{ state_attr('sensor.fire_danger_in_greater_sydney_region', 'fire_ban_today') }}"
device_class: safety
I am using the safety
device class here which I believe works well in badge mode:
But it may be a bit misleading when presented in a group. If there is a fire ban, the XML feed will contain the value “yes” which is turned into “true” in the retrieval sensor above. The safety
device class defines “true” as unsafe and displays a warning sign. Likewise the badge displays as a shield with tick if the sensor value is “false”. However, the textual representation would be “Safe” = no fire ban, or “Unsafe” = fire ban. Please have a think about if this works for you or not.
Danger Level Today
The danger level is a textual description that can be used in a template
sensor as follows:
sensor:
- platform: template
sensors:
fire_danger_level_today:
friendly_name: "Danger Level Today"
value_template: "{{ state_attr('sensor.fire_danger_in_greater_sydney_region', 'danger_level_today') }}"
icon_template: mdi:speedometer
Hiding the retrieval sensor
Once all sensors are fully set up you will probably want to hide the retrieval sensor:
homeassistant:
customize:
sensor.fire_danger_in_greater_sydney_region:
hidden: true
Notification
A simple notification would just let you know if the Danger Level changes. From experience, if the actual Danger Level changes then the XML feed is updated at midnight, and so will be your sensor.
Here is a sample automation sending an HTML formatted pushover notification. The condition is just there to avoid the cases where the XML feed is temporarily unavailable.
- id: fire_danger_level_today_changed
alias: "Danger Level Changed"
trigger:
- platform: state
entity_id: sensor.fire_danger_level_today
condition:
condition: template
value_template: "{{ (trigger.from_state.state != 'None') and (trigger.to_state.state != 'None') }}"
action:
- service: notify.pushover
data_template:
message: "Danger level changed to {{ trigger.to_state.state }} (was {{ trigger.from_state.state }}).
Fire Ban Today: {{ states.binary_sensor.fire_ban_today.state }}"
title: "Fire Danger Level Changed"
data:
html: 1
Outlook
So far I find it useful to have the fire danger information available in Home Assistant, but would yet need to come up with any more sophisticated use-cases – other than a simple notification – to make use of this information.
At this point in time I am not sure if it is worth it transforming this custom component into a proper contribution for Home Assistant. What do you think?
The XML feed provided by the NSW Rural Fire Service is in a custom format, and I haven’t found any comparable feeds from other states. If anyone is aware of anything suitable, please let me know.
Compatibility
At the time of writing this post, I used:
- Home Assistant 0.86.3 with Python 3.6.7
Update 04 Feb 2019
After publishing this on the Home Assistant community forum, I received some good feedback on this topic, and I just wanted to share one improvement in particular:
User DavidFW1960 is translating the fire danger rating into a dial gauge icon that looks just like the ones you actually see at your local fire station, and shares his Lovelace configuration and icons.
Update 22 Feb 2019
With the recent announcement to overhaul the structure of how custom components have to be stored, I wanted to be a bit proactive, and have modified the above post to reflect the new structure.
Update 31 Mar 2019
Home Assistant version 0.91 will contain a small restructure which in turn requires a small fix of this custom component. The latest version on GitHub already contains this fix.
Update 31 Oct 2020
Home Assistant 0.117 contains some structural changes that require a fix which is available on my GitHub repository now.
Also, I have worked on a separate repository which is compatible with HACS to the make the installation a bit easier. Watch out for more details on this blog very soon.
Leave a Reply