#Action, wait for trigger, and branch depending on outcome

1 messages · Page 1 of 1 (latest)

empty sage
#

I have a cat feeder integrated, but I wanted some feedback to know the food actually came out to ensure no issues. It's happened sometimes that the cat feeder became unavailable due to a HA crash or other reasons, and it needs an integration reload to reconnect.

I've created a ffmpeg_noise sensor from a nearby camera that I tested and triggers when the food drops.

Wait for trigger works fine as the noise sensor is not instant, so I need a window to wait for it. If noise is detected I want the script to continue and the counter to increment.

If noise is NOT detected, I want it to launch a notification to my phone with a certain delay (to overcome a temporary HA crash) or a few times over the next hours.

How can I achieve this branching out depending on the wait_for_trigger timeout? Using a choose would work for branching, but the condition check is not a wait_for_trigger but an instant check instead.

This is my current script:

alias: Feed paca
sequence:
  - target:
      entity_id: number.cat_feeder_manual_feed
    data:
      value: "1"
    action: number.set_value
  - wait_for_trigger:
      - trigger: state
        entity_id:
          - binary_sensor.ffmpeg_noise
        to: "on"
    timeout:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
    continue_on_timeout: false
  - target:
      entity_id: counter.paca_portions
    data: {}
    action: counter.increment
mode: single
icon: fas:cat
verbal pawn
#

after the wait_for_trigger, you can use a template condition to inspect the wait variable

#

that will tell you if it timed out or was satisfied

empty sage
#

chatgpt suggests {{ trigger is not none }}

verbal pawn
#

Read documentation, not chatgpt

#

I think it's maybe {{ wait.trigger is not none }}, but don't remember 100%

empty sage
#

wait.completed == true seems like it?

#

or yeah, that seems correct as well

verbal pawn
#

I always get wait_for_trigger and wait_for_template mixed up, since they have slightly different wait variable

empty sage
#

Cool thanks.

It ended up like this. Testing time.

#

do script timers get interrupted if HA restarts? I guess so?

verbal pawn
#

yes running scripts are restarted on a restart. but if you make a timer helper that will persist over a restart

empty sage
# verbal pawn yes running scripts are restarted on a restart. but if you make a timer helper t...

like this?

repeat:
  sequence:
    - action: notify.mobile_app_redmi_note_13_pro_5g_nerea
      metadata: {}
      data:
        message: "Problem with Paca feeder. Food didn't drop. "
    - action: notify.mobile_app_xiaomi_12_lite_ax
      metadata: {}
      data:
        message: "Problem with Paca feeder. Food didn't drop. "
    - action: timer.start
      metadata: {}
      data: {}
      target:
        entity_id: timer.paca_food_recurrent_notification_timer
    - wait_for_trigger:
        - trigger: state
          entity_id:
            - timer.paca_food_recurrent_notification_timer
          to: idle
          from: active
      continue_on_timeout: false
  while:
    - condition: state
      entity_id: number.cat_feeder_manual_feed
      state: unavailable
verbal pawn
#

not exactly. Generally when you use a helper timer you don't have any waits inside your automation itself. you just start the timer, and then use triggers based off of the timer firing events to do the next steps

#

what you have might work, but it won't "resume" after a restart, if that's your goal.

empty sage
#

hmm I don't get what you mean. What should I replace the wait for trigger for? I start the timer, and then what?

#

the timer being active doesn't make the script wait for it automatically afaik

#

tbh even with this timer, if the script resets, it will just be pointless having the timer active, since it will be out of the repeat cycle already