pmg_lucy_timer_loop:
  alias: "PMG Lucy - Notify Timer Loop"
  mode: restart
  sequence:
    - variables:
        binary: binary_sensor.pmg_stats_lucy
        current: sensor.pmg_current_lucy
        timer: timer.pmg_notify_lucy

    - repeat:
        while:
          - condition: template
            value_template: "{{ is_state(binary,'on') or is_state(current,'rec') }}"
        sequence:
          - choose:
              - conditions:
                  - condition: template
                    value_template: "{{ is_state(timer, 'idle') }}"
                sequence:
                  - action: timer.start
                    target:
                      entity_id: "{{ timer }}"
              - conditions:
                  - condition: template
                    value_template: "{{ is_state(timer, 'active') }}"
                sequence:
                  - wait_template: "{{ is_state(timer, 'idle') }}"
                    timeout: "00:02:00" # optional failsafe
    # Wieder starten wenn wieder verbunden
    - choose:
        - conditions:
            - condition: template
              value_template: "{{ is_state(current, 'rec') }}"
          sequence:
            - action: script.turn_on
              target:
                entity_id: script.pmg_lucy_timer_loop

        - conditions:
            - condition: template
              value_template: "{{ is_state(current, 'paused') }}"
          sequence:
            - wait_for_trigger:
                - trigger: state
                  entity_id: sensor.pmg_current_lucy
                  from: "paused"
              timeout: "00:05:00"
            - condition: template
              value_template: "{{ is_state(current, 'rec') }}"
            - action: script.turn_on
              target:
                entity_id: script.pmg_lucy_timer_loop
      default:
        - action: timer.cancel
          target:
            entity_id: "{{ timer }}"

pmg_nessie_timer_loop:
  alias: "PMG nessie - Notify Timer Loop"
  mode: restart
  sequence:
    - variables:
        binary: binary_sensor.pmg_stats_nessie
        current: sensor.pmg_current_nessie
        timer: timer.pmg_notify_nessie

    - repeat:
        while:
          - condition: template
            value_template: "{{ is_state(binary,'on') or is_state(current,'rec') }}"
        sequence:
          - choose:
              - conditions:
                  - condition: template
                    value_template: "{{ is_state(timer, 'idle') }}"
                sequence:
                  - action: timer.start
                    target:
                      entity_id: "{{ timer }}"
              - conditions:
                  - condition: template
                    value_template: "{{ is_state(timer, 'active') }}"
                sequence:
                  - wait_template: "{{ is_state(timer, 'idle') }}"
                    timeout: "00:02:00" # optional failsafe
    # Wieder starten wenn wieder verbunden
    - choose:
        - conditions:
            - condition: template
              value_template: "{{ is_state(current, 'rec') }}"
          sequence:
            - action: script.turn_on
              target:
                entity_id: script.pmg_nessie_timer_loop

        - conditions:
            - condition: template
              value_template: "{{ is_state(current, 'paused') }}"
          sequence:
            - wait_for_trigger:
                - trigger: state
                  entity_id: sensor.pmg_current_nessie
                  from: "paused"
              timeout: "00:05:00"
            - condition: template
              value_template: "{{ is_state(current, 'rec') }}"
            - action: script.turn_on
              target:
                entity_id: script.pmg_nessie_timer_loop
      default:
        - action: timer.cancel
          target:
            entity_id: "{{ timer }}"
