How to optimize an automation to manage different triggers at the same time

How to optimize an automation to manage different triggers at the same time

Hi everyone,

In this article we have seen how to send a notification when the battery of your smartphone reaches a determined level.

Based on the automation written, we should have a specific automation for each smartphone.

This is, of course, not very efficient as we need to have more automations, almost identical, to maintain and manage.

Today we're going to learn how to avoid this extra-effort thanks to a super optimized automation.

Let's get started!

Software configuration

  1. Home Assistant. I'm currently running version?2022.9.6.
  2. Mobile App integration?configured in Home Assistant. This integration allows Home Assistant mobile APP (Android / IOS) to integrate with your Home Assistant environment.
  3. The Home Assistant companion app must be configured to have the?battery level?sensor?enabled.

Home Assistant code (yaml) / procedure

This is the piece of code you have to write as?Automation:

alias: >-
? Smartphone - Android notification if the battery level of all the monitored smartphones reaches / crosses a determined level set via helper
description: >-
? Smartphone - Android notification if the battery level of all the monitored smartphones reaches / crosses a determined level set via helper
trigger:
? - platform: template
? ? value_template: >
? ? ? {{ states('sensor.pocox3nfc_battery_level')|float? >
? ? ? states('input_number.smartphone_livello_batteria_max')|float }}
? ? id: smartphone_A_battery_max
? - platform: template
? ? value_template: >
? ? ? {{ states('sensor.pocox3nfc_battery_level')|float? <
? ? ? states('input_number.smartphone_livello_batteria_min')|float }}
? ? id: smartphone_A_battery_min
? - platform: template
? ? value_template: >
? ? ? {{ states('sensor.sm_a326b_battery_level')|float? >
? ? ? states('input_number.smartphone_livello_batteria_max')|float }}
? ? id: smartphone_B_battery_max
? - platform: template
? ? value_template: >
? ? ? {{ states('sensor.sm_a326b_battery_level')|float? <
? ? ? states('input_number.smartphone_livello_batteria_min')|float }}
? ? id: smartphone_B_battery_min
condition: []
action:
? - if:
? ? ? - condition: trigger
? ? ? ? id: smartphone_A_battery_max
? ? then:
? ? ? - service: notify.mobile_app_pocox3nfc
? ? ? ? data:
? ? ? ? ? message: >-
? ? ? ? ? ? The battery level is at {{states("sensor.pocox3nfc_battery_level") }}%. Shut-off charging the battery.
? - if:
? ? ? - condition: trigger
? ? ? ? id: smartphone_A_battery_min
? ? then:
? ? ? - service: notify.mobile_app_pocox3nfc
? ? ? ? data:
? ? ? ? ? message: >-
? ? ? ? ? ? The battery level is at {{states("sensor.pocox3nfc_battery_level") }}%. Give your phone's battery a recharge.
? - if:
? ? ? - condition: trigger
? ? ? ? id: smartphone_B_battery_max
? ? then:
? ? ? - service: notify.mobile_app_sm_a326b
? ? ? ? data:
? ? ? ? ? message: >-
? ? ? ? ? ? The battery level is at {{states("sensor.sm_a326b_battery_level") }}%. Shut-off charging the battery.
? - if:
? ? ? - condition: trigger
? ? ? ? id: smartphone_B_battery_min
? ? then:
? ? ? - service: notify.mobile_app_sm_a326b
? ? ? ? data:
? ? ? ? ? message: >-
? ? ? ? ? ? The battery level is at {{states("sensor.sm_a326b_battery_level") }}%. Give your phone's battery a recharge.
mode: single        

Some specifics about the code / procedure above

  1. This automation has multiple triggers (4).
  2. For each trigger we have set a specific ID (smartphone_A_battery_max, smartphone_A_battery_min) and we are going to check if the current battery level is minor / greater than the value set via an input number. Using the input number instead of fixed values, allows us to have dynamic threholds you can modify on the Home Assistant user interface.
  3. The action to be executed includes the IF condition to check which trigger was fired based on the ID set before and, then, to sending the right notification to the specific smartphone for which the trigger has been fired.

As you can see, thanks to this optimization, we have just a single automation for two different smartphones at the same time.

Thank you for reading. ?? I hope you have enjoyed this new article.

I'm available for any feedback so, feel free to contact me or share your thoughts in the comments section below.

Your feedbacks will be very appreciated!

Meanwhile,?if you liked this article share it or?subscribe?to my newsletter?about?Home Assistant?to read all previous episodes and still up to date on latest news.

要查看或添加评论,请登录

Emiliano Guerzoni的更多文章

社区洞察

其他会员也浏览了