I wanted to test the new features on one of my automations that notify if the washer is done:
- id: lavatrice_finita_hub
alias: Lavatrice Finita
initial_state: 'on'
trigger:
- entity_id: input_boolean.lavatrice_switch
platform: state
to: 'off'
action:
- service: script.my_notify
data_template:
call_no_annuncio: 1
title: "Lavatrice"
message_tts: "la laatrice è terminata"
message: >-
{{ [
"La lavatrice è terminata. E' ora di sistemare i piatti!",
"La lavatrice ha finito.",
"I panni sono puliti. Adesso tocca stenderli!"
] | random }}
when should I put the “repeat” ?
repeat:
while:
- condition: state
entity_id: binary_sensor.lavatrice
state: 'on'
What I want to achieve is to receive notification until someone at home don’t open the washer.
How can I put also a timer? (repeat the message every 5 min for example)
Uhm. In my case I call a script not a notification. The script is the core of my “notification center” where all my voice messages are managed. I must use it. How can I do that with alert?
- id: lavatrice_finita_hub
alias: Lavatrice Finita
initial_state: 'on'
trigger:
- entity_id: input_boolean.lavatrice_switch
platform: state
to: 'off'
action:
repeat:
while:
- condition: state
entity_id: binary_sensor.lavatrice
state: 'on'
- service: script.my_notify
data_template:
call_no_annuncio: 1
title: "Lavatrice"
message_tts: "la laatrice è terminata"
message: >-
{{ [
"La lavatrice è terminata. E' ora di sistemare i piatti!",
"La lavatrice ha finito.",
"I panni sono puliti. Adesso tocca stenderli!"
] | random }}
repeat:
while:
- condition: state
entity_id: binary_sensor.lavatrice
state: 'on'
sequence:
- service: script.my_notify
data_template:
call_no_annuncio: 1
title: "Lavatrice"
message_tts: "la laatrice è terminata"
message: >-
{{ [
"La lavatrice è terminata. E' ora di sistemare i piatti!",
"La lavatrice ha finito.",
"I panni sono puliti. Adesso tocca stenderli!"
] | random }}
It’s saying: While the conditions are true (lavatrice is on), execute the sequence (call script.my_notify).
uhm, I think I missed the “sequence” part! Thank you!
Now it works, I tested it and I noticed that those messages never stops! But I need to repeat the tts only every 5 min for example.
Maybe adding this at the end?
- delay:
minutes: 5
like this:
repeat:
while:
- condition: state
entity_id: binary_sensor.lavatrice
state: 'on'
sequence:
- service: script.my_notify
data_template:
call_no_annuncio: 1
title: "Lavatrice"
message_tts: "la laatrice è terminata"
message: >-
{{ [
"La lavatrice è terminata. E' ora di sistemare i piatti!",
"La lavatrice ha finito.",
"I panni sono puliti. Adesso tocca stenderli!"
] | random }}
- delay:
minutes: 5
That’s right. It repeats the sequence as fast as Home Assistant on your computer can execute it.
Yes, adding a delay will prevent an overwhelming barrage of notifications.
You may also want the notifications to stop after you have received them ten times (10 x 5 minutes delay = you have been receiving notifications for 50 minutes). Just add another condition to while like this:
Just an FYI, there is a bug in the repeat action in 0.113.0 whereby the repeat variable is not defined while evaluating the while or until conditions; it’s only available within the sequence section of the repeat action. This is fixed in 0.113.1 which should be coming out today.
EDIT: As a work around in the meantime, that condition could be placed inside the sequence.
This isn’t pretty but there is a way to call a script using an alert. Since notifications are event driven like everything else you can create an automation that listens for the event and takes actions accordingly.
So for this use case, to start with you’d likely make a command line notifier that does nothing.
This then allows you to make an alert like this which calls your script every 5 minutes while the washing machine is running and then one last time when it stops to let people know its done.
alert:
washing_machine_running:
name: 'Washing Machine Running'
entity_id: input_boolean.lavatrice_switch
state: 'on'
title: 'Washing Machine Running'
message: 'Washing machine is running, don't open it'
done_message: 'Washing machine is done, you can open it now'
repeat: 300
notifiers:
- 'my_script'
I don’t know if its better then what you have but I personally like alerts a lot for stuff like this and will go out of my way to use them. Even if its somewhat uncomfortable like in this case with the script since they pack a bunch of automation into one concise bit of config.
They’re also entirely state-driven which is great, using a process can be problematic with a long-running washing machine. For instance with the automation if your HA restarted while the washing machine was running (like say if you were tweaking things while sitting on the couch waiting for it to finish) then your automation would just end and you wouldn’t get any more notifications. An alert doesn’t care about that though, it picks up again right away after a restart since its entirely state-driven, its not a running process that can be stopped.
That being said there is a catch here. I notice there are additional parameters on the script for call_no_annuncio and message_tts. Notification services generally don’t allow additional inputs beyond what their platform specifies. So if those parameters are not constant then there might be an issue here. I do have a few other tricks for that I personally use if anyone is interested but they definitely get even more hacky
Considering the scope and magnitude of all the changes you introduced, that’s a very small, and harmless, bug that slipped through. Great work overall, Phil!
Haha yep, can’t disagree with you there. I am listening for call_service after all, that’s a surefire sign of the start of a hacky solution.
Honestly my own “notification hub” is probably the most complicated piece of automation I have. All I wanted to do was use the native HTML5 notifications but turns out actually ensuring they all get to my phone is a very very complicated task. For my own personal HA my do nothing notification service is this:
By setting target to blank its essentially invalid config, it won’t actually send a notification (it actually logs a warning instead). But it will trigger the service call which means I can listen for it. And since it actually uses the HTML5 platform I can then send it all the parameters specific to HTML5 and have it pass input validation.
So yea really committed is a good way to put it. At least in the end its all hidden behind what looks like a normal notification service.