For my washing machine I’d like to only get notifications when I’m at home.
This is my example in automations.yaml
- alias: notification
trigger:
...
action:
- service: notify.user_one
data:
message: "my message"
- condition: state
entity_id: 'device_tracker.user_two'
state: 'home'
- service: notify.user_two
data:
message: "my message"
- service: notify.user_three
data:
message: "my message"
The problem with this is that the third user won’t get the message, when the second one isn’t at home. So the condition will block everything below.
Do you know a nice way to avoid this? I have two ideas, but I don’t know if there is a better way.
One script for each user. But I also don’t know if the conditon of one script will block the others below?
Turn on an input_boolean and trigger three automations with that boolean. Each with its own condiotn and message. I don’t like this idea, because it would mean much unneccesary code.
Conditions can also be part of an action. You can combine multiple service calls and conditions in a single action, and they will be processed in the order you put them in. If the result of a condition is false, the action will stop there so any service calls after that condition will not be executed.
No worries. It can be done. I’m just trying to work out how to do it. give me a minute (or ten!).
EDIT: Actually this is trickier than I thought.
Before the action, you will need a set of ‘or’ conditions to make sure at least one person is home. Otherwise there is no one to notify. That’s not so hard.
There are 7 possible combinations of people to notify. (1+2+3 or 1+2 or 1+3 or 2+3 or just 1 or 2 or 3). This would seem to be best done with a for loop (for each user that is home -> notify). We’ve checked that at least one person is home in the conditions. This is do-able but I’m not sure how. Someone else may be able to help, @petro, @pnbruckner, or @anon43302295 perhaps.
Thanks I will try this. This seems like a really elegant way to afford this. I like it.
I think this also answers my other question. If there is a script in the automation with a condition, this condioton will NOT affect the scripts afterwards, as this is what you use in your code. Am I right?
Not sure this will work. A script cannot be called when it’s still running from being called previously. Depending on timing it might work, or it might work sometimes. I think it would be easiest to have three separate scripts. Actually, it’d be easiest with three automations, each using the same trigger, but each using a condition and action for the particular person.
EDIT: I’ve toyed with the idea of enhancing the scripting to allow a condition to optionally only affect the next step. I’ve also toyed with the idea of adding if/then/else options to scripting. Just haven’t had enough time to work it all out. But if that puts ideas into someone else’s head who does have the time…
I tried something like that when I first started using HA. But unfortunately you can’t count on that working either. Again, depending on timing, if the script’s state hasn’t changed fast enough before the automation gets to the wait_template, it will blow right past it, and you’ll end up with the same problem.
So much for compartmentalizing things. An easy solution would be to create new instances of scripts when executed. But that would probably break other peoples stuff. Maybe a new script type attribute that says ‘allow copy’ or something.
Yeah, it would make sense for the system to have separate contexts for each script execution. It might even already. I really haven’t dug into it that much. But I do know that it will abort with an error if a particular script is started while it’s still running. And, I probably actually do have some scripts using the wait_template technique you suggested that I wrote a long time ago that might only be working by the grace of god. Actually, from time to time I do get the “script already running” error, so… I do need to go back over everything I’ve written at some point, but…
Regarding the script enhancements, I was thinking of starting small. E.g., add if and endif options. The if would look and work exactly the same as a condition (it would even use the same code), but its effect would be to only conditionally run all the steps up until the endif step. Later elif could be added, and maybe deal with nesting if that didn’t “just work.” But, like I said, haven’t really had time to work though it. FWIW, it would look something like this:
- if: # same options as for condition
- service: ...
- service: ...
- endif
- service: ...
Might even make sense to indent the steps inside the if. Then the endif wouldn’t even be necessary:
- if: # same options as for condition
- service: ...
- service: ...
- service: ...
Personally I’m totally fine using one separate script for each user. However I don’t want to use three different automations for this because I have for notifcations I’d like to trigger (washing machine, dryer, windows open, …).
This way I can use the scripts for other automations too.
So would be the code I use (untill maybe there is even a better solution^^)
Yes, that would work, but you should use service instead of service_template, and I’d probably use a state condition instead of a template condition. E.g.: