Automation - Multiple conditions / actions

Hi, I have a doorbell sensor that I what these rules:

  1. If door bell Rings:
    a. Send email notification with camera screen grab
    b. If between 9am and 9pm alert on all Alex’s
    c. If between 9pm and 9am only alert on downstairs Alexas (kids are in bed)

Currently I have 3 separate automations for each of the above which is working fine, but looks very messy and I am having to repeat a lot of lines of code.
How can I condence this down to one single automation?

Kind regards.

With templating

action:
  - service: notify.email
    data:
      ...
  - service: tts.google_say
    data_template:
      entity_id: >-
        {% if now().hour < 21 and now().hour > 8 %}
          media_player.one, media_player.two, media_player.three
        {% else %}
          media_player.one, media_player.two
        {% endif %}
      message: There's somebody at the door
2 Likes

You can also use conditions in the action block: https://www.home-assistant.io/docs/automation/action/

Thanks Tinkerer, only one thing which doesn’t work is using multiple devices, ‘media_player.lounge_echo, media_player.kitchen_echo’, where single does ‘media_player.lounge_echo’. How can I use multiple devices in target?

Does Alexa allow grouping like Google Home? I send my announcements to media_player.whole_house and it plays on all Google Home devices.

It does, but I wanted to specify individual echo devices.
This works:

example_script:
  sequence:
    - service: notify.alexa_media
      data_template:
        data:
          type: tts
        target: >-
          {% if now().hour < 21 and now().hour > 8 %}
            media_player.upstairs_echo
          {% else %}
            media_player.lounge_echo
          {% endif %}
        message: "Test Echo Template"

I have tried, but doesn’t work:

          {% if now().hour < 21 and now().hour > 8 %}
            media_player.upstairs_echo
          {% else %}
            media_player.lounge_echo, media_player.kitchen_echo
          {% endif %}

And, but also doesn’t work:

          {% if now().hour < 21 and now().hour > 8 %}
            media_player.upstairs_echo
          {% else %}
            media_player.lounge_echo
            media_player.kitchen_echo
          {% endif %}

Any other suggestions please?

this thread is marked as solved but your last post suggests that it’s not solved.

is it solved or not?

Sorry, it was, then wasn’t, then was, solved now. Here’s the final:

Using Groups:

downstairs_echo_group:
  name: Downstairs Echo Group
  entities:
    - media_player.lounge_echo
    - media_player.kitchen_echo

Then referencing that, using a sample script:

example_script:
  sequence:
    - service: notify.alexa_media
      data_template:
        data:
          type: tts
        target: >-
          {% if now().hour < 21 and now().hour > 8 %}
            group.everywhere_echo_group
          {% else %}
            group.downstairs_echo_group
          {% endif %}
        message: "Test Echo Template"

Thanks for all your help, im learning loads.

OK, no problem. Just trying to minimize confusion for others looking for answers to anything similar. :slightly_smiling_face:

It’s good that you got it working.

Hi guys, sorry on a much more basic automation, I am trying to turn on my desklight - Sonoff Basic (Tasmotized 8.2) when the temp from the DHT11 on GPIO14 goes above 25 degrees… I then want it to turn off when it goes below 22 degrees. I can only get the automation for turning on, to work!
When I try to add a second automation I get errors… I have changed indenting etc but no luck.

trigger:
  platform: numeric_state
  entity_id: sensor.desklight_am2301_temperature
  above: 25
action:
  service: light.power_on
  entity_id: light.desklight

Each automation needs an alias: which creates the entity ID for the automation. Also, unless you share the error messages we can’t really help you.

Also I replied to you on Discord earlier, might want to check there too (I’m also Tediore there).

1 Like

You’re going to give a lot more info than that.

You didn’t even provide a complete single automation let alone a second automation so we can see where the error is. Not to mention that you didn’t say what the error even was so we know where to look.

1 Like

Sorry,

alias: 'automation1'
trigger:
  platform: numeric_state
  entity_id: sensor.desklight_am2301_temperature
  above: 25
action:
  service: light.power_on
  entity_id: light.desklight


alias: 'automation2'
trigger:
  platform: numeric_state
  entity_id: sensor.desklight_am2301_temperature
  below: 25
action:
  service:  light.turn_off
  entity_id: light.desklight

So this is the full Automation that I wrote.
However in the file editor page at the top right I have the RED cation which says,
'duplicated mapping key at line 11, column 1:
alias: ‘automation2’
So when I scaled it back to just the 1st part I was able to get it to work. (Although I can’t get it to work anymore.)

Thanks Tediore,
Still learning to use Discord to find your comments. (and learning community… and Hassio… and etc… etc… )
Thanks for your help. :smiley:

If these are in the same file with the default automation: !include automations.yaml in configuration.yaml then you need to make them a list by putting a hyphen before the aliases like this (and aligning the elements in each automation accordingly):

- alias: Turn light on
  trigger:
    platform: numeric_state
    ...

- alias: Turn light off
  trigger:
    platform: numeric_state
    ...

The alias can be whatever you want, I just used those as examples.

THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU.
Solved… you are a champion.
I did try a variety of variations with indents and spacings but now can see what it needed.
This is the final

- alias: 'turn on'
  trigger:
    platform: numeric_state
    entity_id: sensor.desklight_am2301_temperature
    above: 25
  action:
    service: light.turn_on
    entity_id: light.desklight


- alias: 'turn off'
  trigger:
    platform: numeric_state
    entity_id: sensor.desklight_am2301_temperature
    below: 25
  action:
    service:  light.turn_off
    entity_id: light.desklight

Really do appreciate how frustrating it can be dealing with a newby like me, so I do genuinly appreciate your help! Thanks Adrian

All good, glad you got it working!