Trigger => action, _then_ condition(s) and action(s)?

Tags: #<Tag:0x00007f325db27a50> #<Tag:0x00007f325db27910>

Hi everybody,

I am currently writing an automation to use grocy.consume_product_from_stock when a physical (zigbee2mqtt) button is pressed.

This action is supposed to happen each and every time the button is pressed.

However, I would also like to use notify.alexa_media if certain conditions are met. Is this possible without using templates?

Example

  - alias: "Button Aqara Küche Mate Plus 1"
    trigger:
      - platform: state
        entity_id: sensor.button_aqara_kueche_kuehlschrank_action
        to: button_4_single
# this ALWAYS happens
    action:
      - service: grocy.add_product_to_stock
        data:
          product_id: 3
          amount: 1
# THEN a condition AFTER the first action...
    condition:
      condition: numeric_state
      entity_id: input_number.helper_mate_kuehlschrank
      below: 3
# ...and an action that only fires if the condition above is met
   action:
     - service: notify.alexa_media
       target: media_player.ben
       message: "You will die soon if you don't buy more Mate!"
       data:
         type: tts

There are two workarounds I can think off:

  1. use a complex template that checks the condition first, then triggers the actions accordingly

  2. create two separate automations =>
    2.1. One to only fire grocy.add_product_to_stock (each time the button is pressed) and
    2.2. One to only check the condition and, if it is met, fire notify.alexa_media

While both these options should work fine, I’d prefer the example yaml I posted above, if this is somehow possible (or, perhaps, planned for future releases of Home Assistant??).

Thank you for your ideas :slight_smile:

Yep perfectly valid. Like this:

action:
      - service: grocy.add_product_to_stock
        data:
          product_id: 3
          amount: 1
# THEN a condition AFTER the first action...
      - condition:
        condition: numeric_state
        entity_id: input_number.helper_mate_kuehlschrank
        below: 3
# ...and an action that only fires if the condition above is met

     - service: notify.alexa_media
       target: media_player.ben
       message: "You will die soon if you don't buy more Mate!"
         data:
           type: tts
1 Like

omg do I feel stupid. I didn’t see anything about this in the docs, so I thought it wasn’t possible (or just this easy). Thank you!

Halfway down this page.

1 Like

It’s covered in the script docs. Kind of a PITA to find if you don’t know where to look.

2 Likes