How to optimize an automation to manage different triggers at the same time
Emiliano Guerzoni
Project Manager | BESS Development | Driving 3+ GW of Energy Storage Projects in Italy
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
领英推荐
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
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.