February 7, 2020

Weather notification automation

Weather notification automation
notification

Every night I check the weather to see if it's going to be raining on my cycle home. My work is in a basement so it's not always clear what the weather is doing. I mean it's the UK, it probably is raining. I thought to myself, I can probably automate this, and so I did.

Every day at 5pm, as long as I am at work I will get a notification letting me know if I need to be taking my waterproof coat or if I can leave it in my bag.  

The automation tells me a couple of things, first if it looks like it may rain, if the probability is higher than 50% it will tell me that it's likely to rain and give me the likelihood in a percentage. It also tells me what the temperature is.

The automation uses only the dark sky weather sensor and the ios app.

To start with, make sure you have the following in your configuration.yaml:

zone:
  - name: Work
    latitude: <insert your latitude>
    longitude: <insert your longitude>
    radius: 250
sensor:
  - platform: darksky
    api_key: !secret
    monitored_conditions:    
      - summary
      - temperature
      - hourly_summary
      - temperature_high
      - temperature_low
      - precip_probability
person:
ios: 
mobile_app:

Once you have all this set up we can set up the automation.

- id: '1581076431149'
  alias: Weather at 5 notification
  description: 'Send a notification at 5pm with what the weather is doing'
  trigger:
  - at: '17:00'
    platform: time
  condition:
  - condition: zone
    entity_id: person.joshua
    zone: zone.work
  action:
  - data:
      message: Test
    service: notify.mobile_app_j_iphone

Here we have the basic set up for the automation. We have told Home Assistant to send a notification you your phone. At the moment if we run this automation you should see something like this.

If everything worked as expected we can start adding the weather to the notification. To do that we need to use the states of the sensors to pull the information we want.  First, let's add the chance of rain and the temperature to the notification.

- id: '1581076431149'
  alias: Weather at 5 notification
  description: 'Send a notification at 5pm with what the weather is doing'
  trigger:
  - at: '17:00'
    platform: time
  condition:
  - condition: zone
    entity_id: person.joshua
    zone: zone.work
  action:
  - data:
      message: There's a {{states.sensor.dark_sky_precip_probability.state}}% chance of rain and the temperature is {{states.sensor.dark_sky_temperature.state|round}}°C 
    service: notify.mobile_app_j_iphone

Now we could just leave it at that, it gives us all the imformation we need. But where's the fun in that. I wanted to use some scripting to make the notification a little more personal.

To test the messages before we make the automation we can use the templates tab in the developers tools. We are going to use the if command to give us a different message depending on what the weather is doing.

  {%- if '{{states.sensor.dark_sky_precip_probability.state}}' |int > 50 -%}
  Take a coat, there's a {{states.sensor.dark_sky_precip_probability.state}}% chance of rain,
  {% else %}
  There's no rain today.
  {% endif %}

This will check the state of the sensor and if the chance of rain is greater than 50% it will display the message. Assuming there is a 90% chance of rain the message will come back like this:
Take a coat, there's a 90% chance of rain,
If there is a 20% chance of rain it will come back like this:
There's no rain today.

We can start to add more complexity to the script, let's break it down into two parts, the first part will tell us if it's going to rain, and if it is, it will let us know the probability.

  {%- if '{{states.sensor.dark_sky_precip_probability.state}}' |int > 50 -%}  
  Take a coat, there's a {{states.sensor.dark_sky_precip_probability.state}}% chance of rain,  
  {%- else -%}  
  Looks like it's clear tonight,  
  {%- endif -%} 

The second part we want to know what the temperature is going to be.

  {%- if '{{states.sensor.dark_sky_temperature.state|round}}' |int > 12 -%}
  but it's going to be a warm cycle home, it's currently {{states.sensor.dark_sky_temperature.state|round}}°C  
  {%- elif '{{states.sensor.dark_sky_temperature.state|round}}' |int > 16 -%}
  but it's going to be a hot cycle home, it's currently {{states.sensor.dark_sky_temperature.state|round}}°C  
  {%- else -%} 
  but it's going to be a cold cycle home, it's currently {{states.sensor.dark_sky_temperature.state|round}}°C 
  {%- endif -%}

We use |round to give us a round number for the temperature.

You can change the numbers of the temperature to whatever you want, these are about what I would clasify as cold, warm and hot rides for my commute.

As long as you are getting a message back in the template editor we are ready to add this to our automation.
Screenshot-2020-02-07-at-12.35.26

paste the above code into the message portion of the automation and you should be good to go.

Once done, it should look like this:

- id: '1581076431149'
  alias: Weather at 5 notification
  description: 'Send a notification at 5pm with what the weather is doing'
  trigger:
  - at: '17:00'
    platform: time
  condition:
  - condition: zone
    entity_id: person.joshua
    zone: zone.work
  action:
  - data:
      service: notify.mobile_app_j_iphone
      message: >
      {%- if '{{states.sensor.dark_sky_precip_probability.state}}' |int > 50 -%}  
      Take a coat, there's a {{states.sensor.dark_sky_precip_probability.state}}% chance of rain,  
      {%- else -%}  
      Looks like it's clear tonight,  
      {%- endif -%} 
      {%- if '{{states.sensor.dark_sky_temperature.state|round}}' |int > 12 -%}
      but it's going to be a warm cycle home, it's currently {{states.sensor.dark_sky_temperature.state|round}}°C  
      {%- elif '{{states.sensor.dark_sky_temperature.state|round}}' |int > 16 -%}
      but it's going to be a hot cycle home, it's currently {{states.sensor.dark_sky_temperature.state|round}}°C  
      {%- else -%} 
      but it's going to be a cold cycle home, it's currently {{states.sensor.dark_sky_temperature.state|round}}°C  
      {%- endif -%}