Requirement is pretty simple but it's not easy to implement:

0 <= PM2.5 <= 12
12 < PM2.5 <= 35
35 < PM2.5 <= 55
55 < PM2.5 <= 150
150 < PM2.5 <= 250
PM2.5 > 250
Use this as default image

We need a way to (1) get the current PM2.5 level into a file (2) parse the number and choose the right image (3) send Line Notify

(1) Notify to File

After spent a few hours of using shell command in Home Assistant, I could not make it work somehow. Tried using below command in several ways but always failed.

shell_command:
  save_to_file: echo "{{states.sensor.ikea_vindriktning_pm25.state}}" > /tmp/pm25_level

So I come up with below setting which is super easy

notify:
  - name: pm25_temp_file
    platform: file
    filename: /tmp/pm25_level
    timestamp: true

Hints:

  • file path above "/tmp/pm25_level" is in "homeassistant" docker, you cannot see it in host path.
  • using "docker exec -it homeassistant bash" to look at the result of ¬†running "notify.pm25_temp_file" at /tmp/pm25_level

(2) Parse PM2.5 result and prepare the image file

First you need to prepare all image files into JPG format at /root/config/pm25 folder. Only files and folders under /root/config are mounted into "homeassistant" docker.

Save the code below into "/root/config/pm25/script.sh" and make it executable (chmod +x).

#!/bin/sh

# read the output from step 1: notify.pm25_temp_file
sfile="/tmp/pm25_level"
# use only the latest result and grab the number
LEVEL=$(cat "$sfile"|tail -1|awk '{print $2}')

if [ "$LEVEL" -gt 250 ] 
then  
    msource="/config/pm25/pm25_6.jpg"
elif [ "$LEVEL" -gt 150 ]
then
    msource="/config/pm25/pm25_5.jpg"
elif [ "$LEVEL" -gt 55 ]
then
    msource="/config/pm25/pm25_4.jpg"
elif [ "$LEVEL" -gt 35 ]
then
    msource="/config/pm25/pm25_3.jpg"
elif [ "$LEVEL" -gt 12 ]
then
    msource="/config/pm25/pm25_2.jpg"
elif [ "$LEVEL" -gt 0 ]
then
    msource="/config/pm25/pm25_1.jpg"
else
    msource="/config/pm25/pm25_0.jpg"
fi

# copy the right image to fix path
cp -f $msource /tmp/pm25.jpg

Make it executable via shell_command. We need to use the path inside Docker instance of "homeassistant". Modification can be done at both locations as it directly mounted from host to the Docker instance.

shell_command:
  pm25_parser: /config/pm25/script.sh

(3) Use "curl" to send the Line Notification

shell_command:
  pm25_notify: >
    /usr/bin/curl --location --request POST 'https://notify-api.line.me/api/notify' --header 'Authorization: Bearer YOUR_LINE_NOTIFY_TOKEN' --form 'message=Current PM2.5: {{states.sensor.ikea_vindriktning_pm25.state}}' --form 'imageFile=@/tmp/pm25.jpg' -H 'cache-control: no-cache'

Restart Core at this step.

Finally, collect all 3 steps into a Script

Add below configuration into "scripts.yaml".

get_pm25_and_alert_to_line_group_with_proper_image:
  alias: Get PM2.5 and Alert to Line Group with Proper Image
  sequence:
  - service: notify.pm25_temp_file
    data:
      message: '{{states.sensor.ikea_vindriktning_pm25.state}}'
  - service: shell_command.pm25_parser
    data: {}
  - service: shell_command.pm25_notify
    data: {}
  mode: single

Then you can use the "script.get_pm25_and_alert_to_line_group_with_proper_image" in any Automation's action easily.