09-12-2023 07:05 AM
Hello,
I'm trying to write an Automation routine with multiple conditions (like in my post title).
It seems to be possible : GHome Automation give that example :
metadata:
name: Synchronize two lights
description: If one light is turned on, turn the other on, and if one light is turned off, turn the other off.
automations:
starters:
type: device.state.OnOff
device: Alexie Hue 1 - Alexie
state: on
is: true
condition:
type: device.state.OnOff
device: Alexie Hue 2 - Alexie
state: on
is: false
actions:
type: device.command.OnOff
devices: Alexie Hue 2 - Alexie
on: true
starters:
type: device.state.OnOff
device: Alexie Hue 1 - Alexie
state: on
is: false
condition:
type: device.state.OnOff
device: Alexie Hue 2 - Alexie
state: on
is: true
actions:
type: device.command.OnOff
devices: Alexie Hue 2 - Alexie
on: false
starters:
type: device.state.OnOff
device: Alexie Hue 2 - Alexie
state: on
is: true
condition:
type: device.state.OnOff
device: Alexie Hue 1 - Alexie
state: on
is: false
actions:
type: device.command.OnOff
device: Alexie Hue 1 - Alexie
on: true
starters:
type: device.state.OnOff
device: Alexie Hue 2 - Alexie
state: on
is: false
condition:
type: device.state.OnOff
device: Alexie Hue 1 - Alexie
state: on
is: true
actions:
type: device.command.OnOff
devices: Alexie Hue 1 - Alexie
on: false
There's an error at the 2nd "starter"...
It's possible to have several conditions in the same routine ?
Otherwise, I can write as much routines as I have conditions. But it's less clean... 😑
Answered! Go to the Recommended Answer.
09-12-2023 01:26 PM
I think I may have sorted this out (testing will happen tonight)...
The key points:
I've edited my automation to what I believe will work (as I said, testing will happen tonight). The validator returned no errors when I ran it. As always, with YAML, indentation is crucial, make sure it's right (I removed most of the auto-generated comments so something may have messed up in editing). Feel free to peruse the following code block. I'll have removed any sensitive information but it's fairly straightforward.
I hope this info helps you on your way!
metadata:
name: Bedtime Wind-Down
description: This automation will automatically turn off our nightstand lights after a set amount of time and the TV off after a set amount of time.
automations:
# ---- TV STARTERS ---- #
- name: TV Sleep Timer
starters:
- type: device.state.OnOff
state: on
is: true
device: <MY TV>
# ---- CONDITIONS ---- #
condition:
type: and
conditions:
- type: home.state.HomePresence
state: homePresenceMode
is: HOME
- type: device.state.ArmDisarm
state: currentArmLevel
is: HOME
device: <MY ALARM SYSTEM>
- type: time.between
before: 2:30 AM
after: 9:30 PM
weekdays:
- MON
- TUE
- WED
- THU
- FRI
- SAT
- SUN
# ---- ACTIONS ---- #
actions:
- type: time.delay
for: 1hour30min
- type: device.command.OnOff
on: false
devices: <MY TV>
# ---- <PERSON> LAMP AUTOMATION ---- #
# ---- Starter 1 ---- #
- name: <PERSON> Lamp Starter 1
starters:
- type: device.state.Brightness
state: brightness
is: 50
device: <PERSON LAMP>
# ---- CONDITIONS ---- #
condition:
type: and
conditions:
- type: device.state.OnOff
state: on
is: true
device: <PERSON LAMP>
- type: home.state.HomePresence
state: homePresenceMode
is: HOME
- type: device.state.ArmDisarm
state: currentArmLevel
is: HOME
device: <MY ALARM SYSTEM>
- type: time.between
before: 2:30 AM
after: 9:30 PM
weekdays:
- MON
- TUE
- WED
- THU
- FRI
- SAT
- SUN
# ---- ACTIONS ---- #
actions:
- type: time.delay
for: 5 min
- type: device.command.BrightnessAbsolute
brightness: 25
devices: <PERSON LAMP>
09-12-2023 09:20 AM
You can have multiple conditions in an automation. I think their example scripts aren't fully correct for how the new editor works.
Regardless, here's an example of multiple conditions using the "AND" type (they also support "OR" and "NOT"):
condition:
type: and
# "AND" together two or more conditionals
conditions:
- type: device.state.ArmDisarm # This function supports arming and disarming as used in, for example, security systems.
state: currentArmLevel
is: HOME
device: <MY SECURITY DEVICE>
- type: time.between
# Optional. Accepts either clock time (10:00:00 AM, with seconds optional, or in a 24 hour format), or 'sunrise' or 'sunset', with an optional offset ('sunrise+10m', for instance)
before: 2:30 AM
# Optional. Accepts either clock time (10:00:00 AM, with seconds optional, or in a 24 hour format), or 'sunrise' or 'sunset', with an optional offset ('sunrise+10m', for instance)
after: 9:30 PM # HH:MM XM (12 hours format). Adjust time as needed
# Optional. Days of the week to apply condition on.
weekdays:
- MON
- TUE
- WED
- THU
- FRI
- SAT
- SUN
- type: home.state.HomePresence
state: homePresenceMode
# [available operators: is, isNot]
is: HOME
You might want to make sure the formatting on that is correct if you copy/paste, this forum doesn't let me format the code block to YAML (which is what the script editor is in).
This block of conditions checks 3 things:
All of these conditions have to be TRUE in order for the automation to then continue.
Hope that helps!
09-12-2023 11:45 AM
Thanks for your answer linkian19
@linkian19 wrote:this forum doesn't let me format the code block to YAML (which is what the script editor is in).
Yes ! That's pretty curious for a forum talking, among other things, of Automation written in YAML... 🙄
Your script is talking about several conditions for the same action.
What I want to do is different actions depending on different conditions, like :
condition1
action1
condition2
action2
...
I know there's a workaround making as much routines there's different conditions. But it's not satisfying...
09-12-2023 11:50 AM
Oh! I'm sorry I misunderstood your question.
I'm looking for similar functionality, actually haha. As far as I know we can only set multiple conditions for whatever starters we have and then it will do whatever actions if conditions are met.
I would LOVE to be able to check the current state of the device before performing an action, especially if I have a time delay on an automation.
Hopefully Google will add more support for it (or make it clearer on how to achieve something like that).
09-12-2023 12:03 PM
In the example they gave (link in my 1st post), "starter" is repeated each time.
I'm going to try this, but it's heavy...
09-12-2023 01:26 PM
I think I may have sorted this out (testing will happen tonight)...
The key points:
I've edited my automation to what I believe will work (as I said, testing will happen tonight). The validator returned no errors when I ran it. As always, with YAML, indentation is crucial, make sure it's right (I removed most of the auto-generated comments so something may have messed up in editing). Feel free to peruse the following code block. I'll have removed any sensitive information but it's fairly straightforward.
I hope this info helps you on your way!
metadata:
name: Bedtime Wind-Down
description: This automation will automatically turn off our nightstand lights after a set amount of time and the TV off after a set amount of time.
automations:
# ---- TV STARTERS ---- #
- name: TV Sleep Timer
starters:
- type: device.state.OnOff
state: on
is: true
device: <MY TV>
# ---- CONDITIONS ---- #
condition:
type: and
conditions:
- type: home.state.HomePresence
state: homePresenceMode
is: HOME
- type: device.state.ArmDisarm
state: currentArmLevel
is: HOME
device: <MY ALARM SYSTEM>
- type: time.between
before: 2:30 AM
after: 9:30 PM
weekdays:
- MON
- TUE
- WED
- THU
- FRI
- SAT
- SUN
# ---- ACTIONS ---- #
actions:
- type: time.delay
for: 1hour30min
- type: device.command.OnOff
on: false
devices: <MY TV>
# ---- <PERSON> LAMP AUTOMATION ---- #
# ---- Starter 1 ---- #
- name: <PERSON> Lamp Starter 1
starters:
- type: device.state.Brightness
state: brightness
is: 50
device: <PERSON LAMP>
# ---- CONDITIONS ---- #
condition:
type: and
conditions:
- type: device.state.OnOff
state: on
is: true
device: <PERSON LAMP>
- type: home.state.HomePresence
state: homePresenceMode
is: HOME
- type: device.state.ArmDisarm
state: currentArmLevel
is: HOME
device: <MY ALARM SYSTEM>
- type: time.between
before: 2:30 AM
after: 9:30 PM
weekdays:
- MON
- TUE
- WED
- THU
- FRI
- SAT
- SUN
# ---- ACTIONS ---- #
actions:
- type: time.delay
for: 5 min
- type: device.command.BrightnessAbsolute
brightness: 25
devices: <PERSON LAMP>
09-13-2023 08:50 AM
Yes !
I juste wrote a test script with "- name:", and the script editor gave no error !
I have to do the same in my personnal routine...
Indentation is the hell ! 👺
And I must copy/paste the same starter for each name...
09-13-2023 11:09 AM
So just as a further update...
This did not seem to function exactly how I expected it to.
What I expected:
That didn't seem to be the case.
What I observed (or at the very least, this is what it seemed like it was doing):
So, to further explain my use case for my automation and what I want to happen:
What seemed to have happened is that when my TV turned on it began it's 1 hour 30 min wait to turn off and nothing else ran. Now I'm assuming that it wasn't because of a starter issue with any of the brightness of the lamps (my lamp starters are triggered by when the brightness is set to the defined number). If I stopped the routine and then manually changed brightness, it seemed like those would actions would perform as expected BUT only for that particular block (as in, if I updated both lamps brightness manually, the first to trigger would be the only one to run).
I need to perform some more testing to see if I can verify the behavior, but with thinking a little deeper about YAML, it makes sense that it doesn't quite function exactly how we expect an actual programming language to perform (like python) so IF statements or loops aren't really a thing.
If I can confirm the behavior I think I can get around it by making a few more routines (one for each lamp and the TV) and having multiple "name" blocks in those as needed. It's tedious but, I think, doable.
09-13-2023 02:56 PM
I think we're not trying to do the same thing.
Thanks to your advice, I managed to make a routine that uses the same triggers, but does not do the same actions depending on different conditions.
Your scene seems a little complicated to me. But if I understood correctly, you want your lights to dim when they are on at 50% and your TV is on.
So why not do:
starter: light = 50%
condition: TV = ON
?
(But you should dim the lights to 50% and turn on the TV at the same time)
09-13-2023 05:27 PM
I'm glad you got your use case figured out!
I'm basically just trying have my TV turn off after a set amount of time while also having my bedside lights dim and turn off over 20 minutes (unless it was already turned off).
I did find that many starters hav a "for" parameter that lets you set a time limit for how long the starter needs to be active before it continues (tv on for 5 minutes for example). I did some testing and it seemed to work the way I'm looking for.
I wrote a comment earlier that got flagged as spam for some reason, I appealed it so we'll see if that gets restored. It had an example and more information that I found out. If you're curious about it all I can write another comment later haha.
Happy automating!
02-04-2024 12:49 PM
My case is exactly the same as yours, I want to use the same starter, but have multiple condition/action blocks share that starter, so that 1 block may run, or the other block, or even both depending on the conditions.. But they are all triggered by the same starter. And obviously, I don't want to have the same problem as the other poster where the distance blocks seem to be blocking/sequential/single threaded.
Did you ever make any progress on this?
02-05-2024 12:48 PM
Unfortunately I wasn't really to come up with a solution (at least at the time) to this issue. The best I had come up with was creating several routines (which is ugly) and while that seemed to work better, it didn't seem to work how I expected, still.
I haven't taken another look at this since then because it was getting too frustrating, haha. I've been meaning to revisit some of this stuff again to see if Google extended support for more actions or scenarios, but haven't thought about it until now.
02-05-2024 11:30 AM
Hello,
If you have several conditions and the same action, you can use "or" in your condition.
If the several action depend of a condition, I'd try :
- starter: X
condition: A
action: 1
- starter: X
condition: B
action: 2
Always the same starter, with a "-".
02-05-2024 11:56 AM
No, mine was the same as your original problem. Different conditions for different actions, but the same starter
It seemed from this thread that both you and the other respondent had issues with your second suggestion:
- starter: X
condition: A
action: 1
- starter: X
condition: B
action: 2
The issue seemed to be that it wasn't running both starters simultaneously. Instead it was running them sequentially. So the first had to complete before the second was attempted. Is that correct? With a delay in the first, that caused a problem for Linkian19.
Were you successful with your use case of 2 x starters?
03-05-2024 12:11 PM
Been sitting here all morning trying to get a similar setup to work. So frustrating..it seems so close to validating. Then will have one line wheres it's like I don't understand why this is here... Gah