Is there a nice way to simplify this repeated automation logic for calendar bin reminders?

I have the automation shown below.

Its job is the following in words:

  • Trigger either at 3pm every day, or alternatively trigger when a “Done” event from an actionable alert is received on my phone.
  • There is a “choose” block to decide if the automation was triggered by a time or by the app.
  • If was triggered by the app, an input text helper is set to message “No bin to put out” and the input boolean is turned off.
  • If it was instead triggered by a time then I check whether there is a “bin” event in the calendar for tomorrow. If yes, then the boolean is set to “on” and the relevant message is displayed. If no, then the message is set to none again and the boolean set to off.
alias: Activate/deactivate bin reminder
description: ""
triggers:
  - trigger: time
    at: "15:00:00"
    id: time_trigger
  - trigger: event
    event_type: mobile_app_notification_action
    event_data:
      action: bin_is_out
    id: app_event_bin_is_out
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - time_trigger
        sequence:
          - action: calendar.get_events
            metadata: {}
            target:
              entity_id: calendar.bin_collection
            data:
              start_date_time: "{{today_at('00:01:00') + timedelta(days=1)}}"
              duration:
                hours: 23
                minutes: 58
                seconds: 0
            response_variable: agenda
          - variables:
              events: "{{ agenda['calendar.bin_collection']['events'] }}"
              is_bin_tomorrow: >-
                {{ events | selectattr('summary', 'search', 'bin collection') |
                list | count > 0 }}
          - if:
              - condition: template
                value_template: "{{ is_bin_tomorrow }}"
            then:
              - variables:
                  active_bins: >
                    {{
                    ['binary_sensor.green_bin_active','binary_sensor.recycling_bin_active','binary_sensor.garden_bin_active']
                    | select('is_state', 'on') | map('state_attr',
                    'friendly_name') | map('replace', ' bin active','') |
                    map('upper') | list}}
                  message: >
                    Don't forget to put the {{  active_bins | join(' and ' if
                    active_bins|count > 1 else '') + (' bins' if
                    active_bins|count > 1 else ' bin')}} out!  
              - action: input_text.set_value
                metadata: {}
                data:
                  value: "{{ message }}"
                target:
                  entity_id: input_text.bin_reminder_message
              - action: input_boolean.turn_on
                metadata: {}
                data: {}
                target:
                  entity_id: input_boolean.bin_needs_putting_out
            else:
              - variables:
                  message: |
                    No bins to put out
              - action: input_text.set_value
                metadata: {}
                data:
                  value: "{{ message }}"
                target:
                  entity_id: input_text.bin_reminder_message
              - action: input_boolean.turn_off
                metadata: {}
                data: {}
                target:
                  entity_id: input_boolean.bin_needs_putting_out
      - conditions:
          - condition: trigger
            id:
              - app_event_bin_is_out
        sequence:
          - action: input_text.set_value
            metadata: {}
            data:
              value: No bin to put out
            target:
              entity_id: input_text.bin_reminder_message
          - action: input_boolean.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: input_boolean.bin_needs_putting_out
mode: single

As you can see quite a lot of the logic appears twice. For example, the input text helper is set to “No bin to put out” and the boolean is set to “off” in both the case where there were no bin events found for tomorrow as well as the case when the app action is detected.

Can anyone see a way to rearrange the flow easily?

I was wondering if there is a way to define a single sequence inside the automation, which would essentially be the “deactivate reminder” sequence. This would consist of: A) set the variable “message” to “No bins to put out”, and B) set the boolean to “off”. Then I could just call the “deactivate” sequence when needed, and set the input text helper to “message” right at the end no matter what happened. I know I can define scripts in a separate file, but I was to learn if I’ve missed a nice way to simplify it whilst keeping everything in a single automation.

Thanks!

Hello teeeeee,

Are you saying that the sequence is the same in multiple automations, or the same with different data?
anentire sequence can be put into a script and called with data fields, so the code could be reused that way. You could also use a script blueprint, or you could use an automation blueprint and repeat entire automations with pre-selected data.

Put this part at the end of the automation so that it is always executed last, except for those times when it shouldn’t where you would use condition or stop to halt the automation before reaching the end.