Automation with 2 triggers

Hi everybody,

Sorry for the probable dumb question, but I need help on automations.

I’m new on Home Assistant. Before, I come from Jeedom, another automation program.
I already set up my frontend and components.

Lets get to the point.
I have a smart mirror plugged on a Zwave controler and I would like to light it up on two different ways:

  • between 05:30:00 and 08:00:00
  • 2 minutes when my motion sensor detect someone

I’m a bit lost with the different topics about automations. Some are about the ancient system, some with the new one.

Can you guys pinpoint me to the right direction?
Thanks a lot

Depends on what exactly are the conditions.
I understand it as : turn it on at 5.30 even if no motion detected and if motion is detected even during day. I would do 3 automations for that, they’d look like this :
alias : ‘morning light’ #start at 5:30
trigger : time
at: ‘05:30:00’
action: turn_on

alias: ‘turn off’ #at 8
trigger: time
at: ‘08:00:00’
action: turn_off

alias: ‘on motion’ #if something moves out of the full on times
trigger: motion
condition: time after:‘08:00:00’ before ‘05:30:00’
action: turn on, delay 2mn, turn off

This is of course to adapt to the actual services (I don’t use zwave), but the important part in your question seemed to be the trigger logic ?
If you wanted to have only one automation with several triggers, always remember that list of trigger is possible, in that case it means an ‘OR’ between the events.
While a ‘condition’ list is an ‘AND’ (and you can create an OR, even OR and AND nested)

check this out

1 Like

Thank you for your nice reply:

I thought about creating only one automation, to keep it clear.
But why not creating tree ^^

So for the first two, is the writing correct?

https://hastebin.com/ulufidekiq.coffeescript

the pastebin stuff looks almost correct…

but you can only have one “action:” section in an automation (as far as I know) so you will need to remove the 2nd & 3rd “action:” entries.

that section should end up looking something like this:

action:
  - service: switch.turn_on
    entity_id: switch.switch_4
  - delay: '00:02:00'
  - service: switch.turn_off
    entity_id: switch.switch_4

you realize tho that it will cause the light to turn off after 2 minutes even if the motion detector is still on (i.e. still detecting motion)? is that what you want?

Thanks for the tip.

Since it was not working, I used the configurator and just add the entity_id, so it’s now:

https://hastebin.com/atodiboreq.cs

…Working so far…
Yeah, you’re right… Ishould add a condition to keep the mirror on if the sensor detects someone.
Thanks againPreformatted text

I’m not sure what your feelings are on the configurator but I really dislike using it.

the way everything gets laid out is inconsistent and (therefore) confusing to try to parse out for us normal humans.

I’d much rather just type it in myself.

and another pro-tip :wink:

when you post code and want it to display properly formatted, highlight the code snippet and then click the </> button at the top of the editor window. after that it should format it exactly as you have it formatted in your configuration file.

i’m glad it’s working for you now.

I totally agree with you.
The configurator is very confusing for me

But since I’m very new here, I thought it would be the best practice.
So, in your opinion, how should I write my automation if it’s not like that?

- action:
  - entity_id: switch.switch_4
    service: switch.turn_on
  - delay: 00:02:00
  - entity_id: switch.switch_4
    service: switch.turn_off
  alias: Auto_Mirror_On_Mouvement
  condition:
  - after: 08:00:00
    before: 05:30:00
    condition: time
  - condition: state
    entity_id: binary_sensor.sensor_2
    state: 'off'
  id: '1521237256289'
  trigger:
  - entity_id: binary_sensor.sensor_2
    from: 'off'
    platform: state
    to: 'on'

(Wooo great, thank you for that tip too) :slight_smile:

The way I lay it out is like the examples in the docs. It makes the most sense to me:

- id: '1521237256289'
  alias: Auto_Mirror_On_Mouvement
  trigger:
    - platform: state
      entity_id: binary_sensor.sensor_2
      from: 'off'
      to: 'on'
  condition:
    condition: and
      conditions:
        - condition: time
          after: '05:30:00'
          before: '08:00:00'
        - condition: state
          entity_id: binary_sensor.sensor_2
          state: 'off'
  action:
    - service: switch.turn_on
      entity_id: switch.switch_4
    - delay: 00:02:00
    - service: switch.turn_off
      entity_id: switch.switch_4

the above should work.

notice you had your times switched around too.

I say “should” because I think I remember having a problem with a condition statement a recently where it didn’t like either an ‘or’ or an ‘and’ condition. I kept getting errors in the config check. if that happens let us know and there may be a way to work around it. But the docs say that an ‘and’ condition should work…so who knows :thinking:

Indeed, it makes more sense…
However, it’s not working.

Testing configuration at /home/homeassistant/.homeassistant
ERROR:homeassistant.util.yaml:mapping values are not allowed here
  in "/home/homeassistant/.homeassistant/automations.yaml", line 22, column 17
Failed config
  General Errors: 
    - mapping values are not allowed here
  in "/home/homeassistant/.homeassistant/automations.yaml", line 22, column 17

Successful config (partial)

The line 22 is this one:

  condition:
    condition: and
      **conditions**:
        - condition: time

I cannot understant that part. Could you explain why the “condition: and” is necessary?

Anither question is: What is this id? Is it radomly generated? If I write my own automation, can I write any ID?

The line you’ve put the stars round needs to come left 2 spaces so that the c for conditions is under the c of condition.

Then the two actual condition blocks all need moving back 2 spaces so their hyphens are under the n of conditions.

AND is saying both conditions need to be true for the automation to fire (as opposed to one OR the other)

ID is only used for the automation editor, so it can keep track of where in the file to edit. You don’t need to use it if you’re manually writing automations.

Hope this helps.

yeah, sorry about that. I got carried away with my indents. :grinning:

Like mf_social said you don’t need the id: but I have just started putting it in there as a habit just in case I want to use the automation editor at some point in time. and you can put any unique name in there that you want. if you use the automation editor to create the automation then it randomly assigns one by default.

In fairness, IMO it would make more sense if it was supposed to be indented like that, seeing as how everything else does. Just another idiosyncrasy of the system :smile:

Oh thank you guys, you really Rocks !!

I understand lot better how automation works. Again thank you.

I have no errors, but the mirror won’t light up.
I thougt it could be because of the “before” “after” time switched, but no.
It probably is because the two conditions are not compatible.

I still need to train my logic :smile:

I didn’t actually read the automation, but now I have, that one will never fire because the trigger is the sensor going on, but one of your conditions is the sensor being off. Obviously both can’t be true at the same time.

Yeah I was just looking over your initial post and the automation you posted above using the automation editor (and that I subsequently edited) won’t do what you initially wanted. it will only come on between 0530 and 0800 if it sees motion.

I have to step away for a little while but if i get the chance i’ll repost another automation for your use (unless you figure it out on your own by then :wink:).

EDIT:

OK and what mf_social said above too. I didn’t read it either I just laid what you had written out differently.

Yeah… Logic… :confused:

Thank you guys,
And thank you a lot finity.

In a perfect world, I’ll be able to figure out how to automatise the mirror in one single automation.
If I don’t, fell free to. Hahaha

By the way, I’m going to bed now.

Thank you again guys!

This would be better with 2 automations:

trigger:
  platform: time 
  at: '05:30:00'
action:
  - service: homeassistant.turn_on
    entity_id: switch.switch_4
  - delay: 02:30:00
  - service: homeassistant.turn_off
    entity_id: switch.switch_4
trigger:
  platform: state 
  entity_id: binary_sensor.sensor_2
  to: 'on' 
condition:
  condition: time
  before: '05:29:59'
  after: '08:00:01'
action:
  - service: homeassistant.turn_on
    entity_id: switch.switch_4
  - delay: 00:02:00
  - service: homeassistant.turn_off
    entity_id: switch.switch_4

You’re probably right.
It may be more efficient to split it in two automation.

Thank you for your proposal
This brings another question (sorry ^^)
Why use “service: homeassistant.turn_on” instead of “service: switch.turn_on”?

Good point to play with seconds to avoid issues.

Yeah, you could do it all in one, but in this case it would be overly complicated for no benefit.

In this case there’s no difference, but you can use homeassistant.turn_on across domains, like

service: homeassistant.turn_on
entity_id:
  - switch.switch_one
  - media_player.media_player_seven
  - light.kitchen
  - group.garage 

So I just get in the habit of using it all the time. Call it ‘best practice’ if we were being all corporate. :+1: