Repeat across different automations

Hi, I have the automations as in picture. They work fine in order 1,2,3,4 (ignore ARM/DISARM for now).
I’m trying to do so when it reaches 4 it goes back to 1 automatically and starts over the cycle (1,2,3,4) for a set number of times.

I tried setting a counter helper (initialized at 0 and incremented at step 4) and use that to restart (trigger in step 1: if counter > 0 ) but it doesn’t seem to be working (perhaps the counter is not checked again in step 1)?

note that for the very first run of step 1, I have motion detection setup and that’s working fine. I just want step 1 to be triggered for n times after the first one regardless of whether there is motion detected after the first time. Basically the question is how can I “call” step 1 at the end of step 4?
thanks!

I think we’ll need to see the yaml for this in order for us to help.

Also, if your setting up an alarm system then if recommend the HACS integration Alarmo.

Thanks, here’s the automations.yaml I’ll look into Alarmo for future updates!

- id: d200f0ba4ed340b9b2a828232573da6f
  alias: ARM
  description: Start camera when armed night
  trigger:
  - platform: state
    entity_id: alarm_control_panel.ha_alarm
    to: armed_night
  - platform: event
    event_type: telegram_command
    event_data:
      command: /arm
  action:
  - service: alarm_control_panel.alarm_arm_night
    target:
      entity_id: alarm_control_panel.ha_alarm
    data: {}
  - type: turn_off
    device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
    entity_id: switch.camera1_privacy
    domain: switch
  - type: turn_off
    device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
    entity_id: switch.camera2_privacy
    domain: switch
  - type: turn_off
    device_id: 04cc26f46a29c2a5c2d8393031afe809
    entity_id: switch.camera3_privacy
    domain: switch
  - service: counter.reset
    data: {}
    target:
      entity_id: counter.counter
- id: 71a5bfe98cb045e2af585115a439e74b
  alias: DISARM
  description: Stop camera when disarmed
  trigger:
  - platform: state
    entity_id: alarm_control_panel.ha_alarm
    to: disarmed
  - platform: event
    event_type: telegram_command
    event_data:
      command: /disarm
  action:
  - service: alarm_control_panel.alarm_disarm
    target:
      entity_id: alarm_control_panel.ha_alarm
    data: {}
  - type: turn_on
    device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
    entity_id: switch.camera1_privacy
    domain: switch
  - type: turn_on
    device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
    entity_id: switch.camera2_privacy
    domain: switch
  - type: turn_on
    device_id: 04cc26f46a29c2a5c2d8393031afe809
    entity_id: switch.camera3_privacy
    domain: switch
  - device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
    domain: button
    entity_id: button.camera1_manual_alarm_stop
    type: press
  - device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
    domain: button
    entity_id: button.camera2_manual_alarm_stop
    type: press
  - device_id: 04cc26f46a29c2a5c2d8393031afe809
    domain: button
    entity_id: button.camera3_manual_alarm_stop
    type: press
  - service: counter.reset
    data: {}
    target:
      entity_id: counter.counter
- id: cb796d481f114838a41978ee7552e558
  alias: 1 - Motion detected = trigger alarm
  description: ''
  trigger:
  - platform: event
    event_type: telegram_command
    event_data:
      command: /trigger
  - platform: state
    entity_id:
    - binary_sensor.camera1_motion
    - binary_sensor.camera2_motion
    - binary_sensor.camera3_motion
    from: 'off'
    to: 'on'
  action:
  - service: alarm_control_panel.alarm_trigger
    target:
      entity_id: alarm_control_panel.ha_alarm
    data: {}
    enabled: true
- id: adc024cb4e8d4090b9cb1d017a1d5871
  alias: ' 2 - Alarm is triggered'
  description: ''
  trigger:
  - platform: state
    entity_id: alarm_control_panel.ha_alarm
    to: triggered
  action:
  - service: persistent_notification.create
    data:
      message: '# INTRUDER!!!'
      title: '## Motion Detected ##'
  - device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
    domain: button
    entity_id: button.camera1_manual_alarm_start
    type: press
  - device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
    domain: button
    entity_id: button.camera2_manual_alarm_start
    type: press
  - device_id: 04cc26f46a29c2a5c2d8393031afe809
    domain: button
    entity_id: button.camera3_manual_alarm_start
    type: press
  - service: camera.snapshot
    data:
      filename: /media/snapshots/camera1_{{states('counter.counter')}}.jpg
    target:
      device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
  - service: camera.snapshot
    data:
      filename: /media/snapshots/camera2_{{states('counter.counter')}}.jpg
    target:
      device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
  - service: camera.snapshot
    data:
      filename: /media/snapshots/camera3_{{states('counter.counter')}}.jpg
    target:
      device_id: 04cc26f46a29c2a5c2d8393031afe809
  - service: notify.HASSIO_SICUREZZA
    data:
      title: ALLARME ATTIVATO
      message: ALLARME ATTIVATO
      data:
        photo:
        - file: /media/snapshots/camera1_{{states('counter.counter')}}.jpg
          caption: CAM 1
  - service: notify.HASSIO_SICUREZZA
    data:
      title: ALLARME ATTIVATO
      message: ALLARME ATTIVATO
      data:
        photo:
        - file: /media/snapshots/camera2_{{states('counter.counter')}}.jpg
          caption: CAM 2
  - service: notify.HASSIO_SICUREZZA
    data:
      title: ALLARME ATTIVATO
      message: ALLARME ATTIVATO
      data:
        photo:
        - file: /media/snapshots/camera3_{{states('counter.counter')}}.jpg
          caption: CAM 3
  - service: camera.record
    data:
      duration: 10
      filename: /media/video/camera1_{{states('counter.counter')}}.mp4
    target:
      device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
  - service: camera.record
    data:
      duration: 10
      filename: /media/video/camera2_{{states('counter.counter')}}.mp4
    target:
      device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
  - service: camera.record
    data:
      duration: 10
      filename: /media/video/camera3_{{states('counter.counter')}}.mp4
    target:
      device_id: 04cc26f46a29c2a5c2d8393031afe809
- id: dd028cb9c377484a95852aa17e841645
  alias: 3 - Video recorded
  description: Video recorded
  trigger:
  - platform: event
    event_type: folder_watcher
    event_data:
      event_type: created
  action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 15
      milliseconds: 0
  - service: hassio.backup_partial
    data:
      folders: media
      name: SICUREZZA-BACKUP
  - service: persistent_notification.create
    data:
      message: '# Video recorded'
      title: '## Motion Detected ##'
  - service: notify.HASSIO_SICUREZZA
    data:
      title: Video Registrato
      message: Video Registrato
- id: '1673356377777'
  alias: 4 - Video uploaded on google drive
  description: ''
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.backups_stale
    to: 'off'
  condition: []
  action:
  - service: persistent_notification.create
    data:
      message: '# Video uploaded'
      title: '## Motion Detected ##'
  - service: notify.HASSIO_SICUREZZA
    data:
      title: Video Caricato
      message: Video Caricato
  - service: counter.increment
    data: {}
    target:
      entity_id: counter.counter
  mode: single

I haven’t tried this - I’ve never used the counter and I’m not sure what unexpected things will occur with the automation.trigger service call - but I think you need to add this to the sequence of actions for your automation 4.

if:
  - condition: template
    value_template: "{{ states('counter.counter') < 3 }}"
then:
  - service: automation.trigger
    data: {}
    target:
      entity_id: automation.id_for_automation_4
1 Like

If you want the automations to execute in order one after another then why not just merge all the steps into one automation? Why are they separate automations at all if they aren’t intended to run independently of each other?

Because how would I setup triggers to be run after certain actions? For example in autom. 3 I have folder watcher as trigger. It triggers a partial backup but it needs to run only after videos are recorded (in step 2). Unless you are saying that actions are executed one after the other when the previous one finishes its job?

Yes. All actions execute sequentially. When the previous one finishes the next one starts. If you want steps to run in parallel you must use the parallel action, by default they are all sequential.

Now some things are still asynchronous. For instance if you have a step that tells some other device to do something most likely HA is going to continue after it successfully tells that other device to do something, its not usually going to wait for it to do the long-running action you told it to do.

In that case you could use a wait for trigger action. It causes the automation to wait at a particular spot until a trigger has occurred. The trigger options are the same as in automations.

ok…I think I understand…basically I can use a ‘wait for trigger’ instead of having another automation with its own trigger. Yet the problem is things are more complicated :slight_smile: because I have 3 cameras and unfortunately they do not start recording exactly at same time (currently trying to figure out why). also they don’t have a “finished recording” event to be triggered AFAIK (hence the need offolder watcher). So right now my folder watcher runs more than once due to cameras finishing at different times, but that’s ok (it means I get a backup after each camera finishes the recording and I don’t mind).

I’ll play around with this approach as it surely sound more appropriate but first I have to figure out the parallel action!

I would guess that the camera.record service actually does wait until the recording is complete before proceeding with the next action. Since its not just signaling to the other device to do something, it is pulling the feed from it and writing it to a file for the specified duration. The camera can’t see that file, only HA can so I would expect the automation to wait until that entire action has completed before proceeding.

I don’t use that service myself so I haven’t done much testing with it. But I would suggest testing it yourself. Make a simple script that does a camera.record action and then a persistent_notification.create action immediately after. Then you can see when that notification pops up in HA, is it as soon as the recording begins or after it is complete?

If it is the latter then you actually don’t need to involve folder watcher at all. Just start the recording and then list the steps that come after the recording finished like normal.

If the simple test shows HA does not wait for the recording to complete then your right, you’d need some sort of watcher and a trigger.

I think I’m confused about the function of the counter.

I see it being reset a couple of times and I see it incremented once at the end of automation 4. But I don’t see the counter value being used anywhere to trigger or control anything. And nothing more is being done with it in automations 1 thru 3.

So I don’t know what you mean by this.

it’s not checked because you don’t tell it to check it.

Am I missing something?

Hello I had changed the code since it wasn’t working. Here’s my current code. Counter > 0 is trigger for ALARM TRIGGERED… So technically:

  • first time, counter = 0, thus only motion detection or telegram can trigger the actions
  • second,third…fifth time, counter is always > 0 so it should trigger even without motion detection/telegram

So is the counter constantly checked while hassio is running (like a loop’s condition)?
Thanks

- id: d200f0ba4ed340b9b2a828232573da6f
  alias: ARM
  description: Start camera when armed night
  trigger:
  - platform: state
    entity_id: alarm_control_panel.ha_alarm
    to: armed_night
  - platform: event
    event_type: telegram_command
    event_data:
      command: /arm
  action:
  - service: alarm_control_panel.alarm_arm_night
    target:
      entity_id: alarm_control_panel.ha_alarm
    data: {}
  - type: turn_off
    device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
    entity_id: switch.camera1_privacy
    domain: switch
  - type: turn_off
    device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
    entity_id: switch.camera2_privacy
    domain: switch
  - type: turn_off
    device_id: 04cc26f46a29c2a5c2d8393031afe809
    entity_id: switch.camera3_privacy
    domain: switch
  - service: counter.reset
    data: {}
    target:
      entity_id: counter.counter
- id: 71a5bfe98cb045e2af585115a439e74b
  alias: DISARM
  description: Stop camera when disarmed
  trigger:
  - platform: state
    entity_id: alarm_control_panel.ha_alarm
    to: disarmed
  - platform: event
    event_type: telegram_command
    event_data:
      command: /disarm
  action:
  - service: alarm_control_panel.alarm_disarm
    target:
      entity_id: alarm_control_panel.ha_alarm
    data: {}
  - type: turn_on
    device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
    entity_id: switch.camera1_privacy
    domain: switch
  - type: turn_on
    device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
    entity_id: switch.camera2_privacy
    domain: switch
  - type: turn_on
    device_id: 04cc26f46a29c2a5c2d8393031afe809
    entity_id: switch.camera3_privacy
    domain: switch
  - device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
    domain: button
    entity_id: button.camera1_manual_alarm_stop
    type: press
  - device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
    domain: button
    entity_id: button.camera2_manual_alarm_stop
    type: press
  - device_id: 04cc26f46a29c2a5c2d8393031afe809
    domain: button
    entity_id: button.camera3_manual_alarm_stop
    type: press
  - service: counter.reset
    data: {}
    target:
      entity_id: counter.counter
- id: adc024cb4e8d4090b9cb1d017a1d5871
  alias: ALARM TRIGGERED
  description: Motion detected!!
  trigger:
  - platform: event
    event_type: telegram_command
    event_data:
      command: /trigger
  - platform: state
    entity_id:
    - binary_sensor.camera1_motion
    - binary_sensor.camera2_motion
    - binary_sensor.camera3_motion
    from: 'off'
    to: 'on'
  - platform: numeric_state
    entity_id: counter.counter
    above: 0
    below: 5
  action:
  - service: alarm_control_panel.alarm_trigger
    data: {}
    target:
      entity_id: alarm_control_panel.ha_alarm
  - service: notify.hassio_sicurezza
    data:
      message: ALLARME ATTIVATO
      title: ALLARME ATTIVATO
  - service: persistent_notification.create
    data:
      message: '# INTRUDER!!!'
      title: '## Motion Detected ##'
  - parallel:
    - service: camera.snapshot
      data:
        filename: /media/snapshots/camera1_{{states('counter.counter')}}.jpg
      target:
        device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
    - service: camera.snapshot
      data:
        filename: /media/snapshots/camera2_{{states('counter.counter')}}.jpg
      target:
        device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
    - service: camera.snapshot
      data:
        filename: /media/snapshots/camera3_{{states('counter.counter')}}.jpg
      target:
        device_id: 04cc26f46a29c2a5c2d8393031afe809
    alias: Take Camera Snapshots
  - parallel:
    - service: camera.record
      data:
        duration: 10
        filename: /media/video/camera1_{{states('counter.counter')}}.mp4
      target:
        device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
    - service: camera.record
      data:
        duration: 10
        filename: /media/video/camera2_{{states('counter.counter')}}.mp4
      target:
        device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
    - service: camera.record
      data:
        duration: 10
        filename: /media/video/camera3_{{states('counter.counter')}}.mp4
      target:
        device_id: 04cc26f46a29c2a5c2d8393031afe809
    alias: Start video recording
  - parallel:
    - device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
      domain: button
      entity_id: button.camera1_manual_alarm_start
      type: press
    - device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
      domain: button
      entity_id: button.camera2_manual_alarm_start
      type: press
    - device_id: 04cc26f46a29c2a5c2d8393031afe809
      domain: button
      entity_id: button.camera3_manual_alarm_start
      type: press
    alias: Start Camera Alarms
  - parallel:
    - service: notify.HASSIO_SICUREZZA
      data:
        title: ALLARME ATTIVATO
        message: ALLARME ATTIVATO
        data:
          photo:
          - file: /media/snapshots/camera1_{{states('counter.counter')}}.jpg
            caption: CAM 1
    - service: notify.HASSIO_SICUREZZA
      data:
        title: ALLARME ATTIVATO
        message: ALLARME ATTIVATO
        data:
          photo:
          - file: /media/snapshots/camera2_{{states('counter.counter')}}.jpg
            caption: CAM 2
    - service: notify.HASSIO_SICUREZZA
      data:
        title: ALLARME ATTIVATO
        message: ALLARME ATTIVATO
        data:
          photo:
          - file: /media/snapshots/camera3_{{states('counter.counter')}}.jpg
            caption: CAM 3
    alias: Notify Telegram Snapshots
- id: dd028cb9c377484a95852aa17e841645
  alias: Video recorded
  description: Video recorded. Creating backup
  trigger:
  - platform: event
    event_type: folder_watcher
    event_data:
      event_type: created
  action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 15
      milliseconds: 0
  - service: hassio.backup_partial
    data:
      folders: media
      name: SICUREZZA-BACKUP
  - service: persistent_notification.create
    data:
      message: '# Video recorded'
      title: '## Motion Detected ##'
  - service: notify.HASSIO_SICUREZZA
    data:
      title: Video Registrato
      message: Video Registrato
  - service: counter.increment
    data: {}
    target:
      entity_id: counter.counter

No.

triggers only get evaluated once at a moment in time when the monitored entity changes.

A numeric state trigger has to cross the defined threshold to fire.

So in your case the numeric state trigger will only ever evaluate when the counter state changes. And the trigger will only ever fire when the counter value goes from below 0 to above 0 or from above 5 to below 5.

I’m still trying to envision what exactly you want the the counter to do.

it seems like you want a condition instead of a trigger. But I’m not even sure of that.

the way you have it now (from your description and trying to interpret the code even tho it won’t work) as soon as the counter increments in the second to last automation above it would just restart the whole process of that automation (notification, snapshot, recording) over and over again until the counter value was above 5. And then (if the counter was used as a condition) it would never run again until the counter gets reset. But it only gets reset when you arm or disarm the alarm.

So that won’t work correctly either.

What exactly is the purpose of the counter?

that’s exactly what I’m trying to do. a basic loop as in any programming language. the counter keeps tracks of how many time the automation run and the automation runs until the condition counter > 0 && counter < 5 evaluates false.

here’s the full structure without this loop and how it should run:

  • counter = 0
  • motion detected
  • triggers alarm and camera starts video recording for 10 seconds
  • once the video is recorded and backed up increment counter +1
  • counter = 1 (> 0 && < 5)
  • triggers alarm and camera starts new video recording for 10 seconds
  • once the video is recorded and backed up increment counter +1
  • counter = 2 (> 0 && < 5)
  • triggers alarm and camera starts new video recording for 10 seconds
  • once the video is recorded and backed up increment counter +1
  • counter = 3 (> 0 && < 5)
  • triggers alarm and camera starts new video recording for 10 seconds
  • once the video is recorded and backed up increment counter +1
  • counter = 4 (> 0 && < 5)
  • triggers alarm and camera starts new video recording for 10 seconds
  • once the video is recorded and backed up increment counter +1
  • counter = 5 (> 0 but not < 5)
  • stop and call disarm

the counter is used to trigger the actions regardless of whether any motion is detected (after the first one)
because of the repetitions it’d be easier if there was a way to loop aroudn the same actions and use the counter to keep count of iterations.

Thanks

OK, thanks for the clarification.

I think this should do what you want (I only included the changed automation since the others should be OK):

- id: adc024cb4e8d4090b9cb1d017a1d5871
  alias: ALARM TRIGGERED
  description: Motion detected!!
  trigger:
  - platform: event
    event_type: telegram_command
    event_data:
      command: /trigger
  - platform: state
    entity_id:
    - binary_sensor.camera1_motion
    - binary_sensor.camera2_motion
    - binary_sensor.camera3_motion
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: counter.counter
    to: 
  condition:
    - condition: template
      value_template: "{{ states('counter.counter') | int < 5 }}"
  action:
  - service: alarm_control_panel.alarm_trigger
    data: {}
    target:
      entity_id: alarm_control_panel.ha_alarm
  - service: notify.hassio_sicurezza
    data:
      message: ALLARME ATTIVATO
      title: ALLARME ATTIVATO
  - service: persistent_notification.create
    data:
      message: '# INTRUDER!!!'
      title: '## Motion Detected ##'
  - parallel:
    - service: camera.snapshot
      data:
        filename: /media/snapshots/camera1_{{states('counter.counter')}}.jpg
      target:
        device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
    - service: camera.snapshot
      data:
        filename: /media/snapshots/camera2_{{states('counter.counter')}}.jpg
      target:
        device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
    - service: camera.snapshot
      data:
        filename: /media/snapshots/camera3_{{states('counter.counter')}}.jpg
      target:
        device_id: 04cc26f46a29c2a5c2d8393031afe809
    alias: Take Camera Snapshots
  - parallel:
    - service: camera.record
      data:
        duration: 10
        filename: /media/video/camera1_{{states('counter.counter')}}.mp4
      target:
        device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
    - service: camera.record
      data:
        duration: 10
        filename: /media/video/camera2_{{states('counter.counter')}}.mp4
      target:
        device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
    - service: camera.record
      data:
        duration: 10
        filename: /media/video/camera3_{{states('counter.counter')}}.mp4
      target:
        device_id: 04cc26f46a29c2a5c2d8393031afe809
    alias: Start video recording
  - parallel:
    - device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
      domain: button
      entity_id: button.camera1_manual_alarm_start
      type: press
    - device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
      domain: button
      entity_id: button.camera2_manual_alarm_start
      type: press
    - device_id: 04cc26f46a29c2a5c2d8393031afe809
      domain: button
      entity_id: button.camera3_manual_alarm_start
      type: press
    alias: Start Camera Alarms
  - parallel:
    - service: notify.HASSIO_SICUREZZA
      data:
        title: ALLARME ATTIVATO
        message: ALLARME ATTIVATO
        data:
          photo:
          - file: /media/snapshots/camera1_{{states('counter.counter')}}.jpg
            caption: CAM 1
    - service: notify.HASSIO_SICUREZZA
      data:
        title: ALLARME ATTIVATO
        message: ALLARME ATTIVATO
        data:
          photo:
          - file: /media/snapshots/camera2_{{states('counter.counter')}}.jpg
            caption: CAM 2
    - service: notify.HASSIO_SICUREZZA
      data:
        title: ALLARME ATTIVATO
        message: ALLARME ATTIVATO
        data:
          photo:
          - file: /media/snapshots/camera3_{{states('counter.counter')}}.jpg
            caption: CAM 3
    alias: Notify Telegram Snapshots

EDIT: you also need this (I think):

- id: 71a5bfe98cb045e2af585115a439e74b
  alias: DISARM
  description: Stop camera when disarmed
  trigger:
  - platform: state
    entity_id: alarm_control_panel.ha_alarm
    to: disarmed
  - platform: event
    event_type: telegram_command
    event_data:
      command: /disarm
  - platform: state
    entity_id: counter.counter
    to: 5
  action:
  - service: alarm_control_panel.alarm_disarm
    target:
      entity_id: alarm_control_panel.ha_alarm
    data: {}
  - type: turn_on
    device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
    entity_id: switch.camera1_privacy
    domain: switch
  - type: turn_on
    device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
    entity_id: switch.camera2_privacy
    domain: switch
  - type: turn_on
    device_id: 04cc26f46a29c2a5c2d8393031afe809
    entity_id: switch.camera3_privacy
    domain: switch
  - device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
    domain: button
    entity_id: button.camera1_manual_alarm_stop
    type: press
  - device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
    domain: button
    entity_id: button.camera2_manual_alarm_stop
    type: press
  - device_id: 04cc26f46a29c2a5c2d8393031afe809
    domain: button
    entity_id: button.camera3_manual_alarm_stop
    type: press
  - service: counter.reset
    data: {}
    target:
      entity_id: counter.counter

Hi! Finally I had time to test the code. however it doesn’t seem to be working as expected. I think at the end I should have 4 snapshots for each camers named with the counter. For example for camera 1 I should have files: snapshots/camera1_0.jpg, snapshots/camera1_1.jpg, snapshots/camera1_2.jpg, snapshots/camera1_3.jpg
for camera 2: snapshots/camera2_0.jpg, snapshots/camera2_1.jpg, snapshots/camera2_2.jpg, snapshots/camera2_3.jpg
and so on for camera 3

also same thing for the videos, I’d expect to see 4 videos for each camera,
however it’s not the case.

Is there any practical way to debug for example print somewhere the counter current value?
Here’s my updated code. Thanks

- id: d200f0ba4ed340b9b2a828232573da6f
  alias: ARM
  description: Start camera when armed night
  trigger:
  - platform: state
    entity_id: alarm_control_panel.ha_alarm
    to: armed_night
  - platform: event
    event_type: telegram_command
    event_data:
      command: /arm
  action:
  - service: alarm_control_panel.alarm_arm_night
    target:
      entity_id: alarm_control_panel.ha_alarm
    data: {}
  - type: turn_off
    device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
    entity_id: switch.camera1_privacy
    domain: switch
  - type: turn_off
    device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
    entity_id: switch.camera2_privacy
    domain: switch
  - type: turn_off
    device_id: 04cc26f46a29c2a5c2d8393031afe809
    entity_id: switch.camera3_privacy
    domain: switch
  - service: counter.reset
    data: {}
    target:
      entity_id: counter.counter
- id: 71a5bfe98cb045e2af585115a439e74b
  alias: DISARM
  description: Stop camera when disarmed
  trigger:
  - platform: state
    entity_id: alarm_control_panel.ha_alarm
    to: disarmed
  - platform: event
    event_type: telegram_command
    event_data:
      command: /disarm
  - platform: state
    entity_id:
    - counter.counter
    to: '3'
    from: '2'
  action:
  - service: alarm_control_panel.alarm_disarm
    target:
      entity_id: alarm_control_panel.ha_alarm
    data: {}
  - type: turn_on
    device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
    entity_id: switch.camera1_privacy
    domain: switch
  - type: turn_on
    device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
    entity_id: switch.camera2_privacy
    domain: switch
  - type: turn_on
    device_id: 04cc26f46a29c2a5c2d8393031afe809
    entity_id: switch.camera3_privacy
    domain: switch
  - device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
    domain: button
    entity_id: button.camera1_manual_alarm_stop
    type: press
  - device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
    domain: button
    entity_id: button.camera2_manual_alarm_stop
    type: press
  - device_id: 04cc26f46a29c2a5c2d8393031afe809
    domain: button
    entity_id: button.camera3_manual_alarm_stop
    type: press
  - service: counter.reset
    data: {}
    target:
      entity_id: counter.counter
- id: adc024cb4e8d4090b9cb1d017a1d5871
  alias: ALARM TRIGGERED
  description: Motion detected!!
  trigger:
  - platform: event
    event_type: telegram_command
    event_data:
      command: /trigger
  - platform: state
    entity_id:
    - binary_sensor.camera1_motion
    - binary_sensor.camera2_motion
    - binary_sensor.camera3_motion
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: counter.counter
    to:
  condition:
  - condition: template
    value_template: '{{ states(''counter.counter'') | int < 3 }}'
  action:
  - service: alarm_control_panel.alarm_trigger
    data: {}
    target:
      entity_id: alarm_control_panel.ha_alarm
  - service: notify.hassio_sicurezza
    data:
      message: ALLARME ATTIVATO
      title: ALLARME ATTIVATO
  - service: persistent_notification.create
    data:
      message: '# INTRUDER!!!'
      title: '## Motion Detected ##'
  - parallel:
    - service: camera.snapshot
      data:
        filename: /media/snapshots/camera1_{{states('counter.counter')}}.jpg
      target:
        device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
    - service: camera.snapshot
      data:
        filename: /media/snapshots/camera2_{{states('counter.counter')}}.jpg
      target:
        device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
    - service: camera.snapshot
      data:
        filename: /media/snapshots/camera3_{{states('counter.counter')}}.jpg
      target:
        device_id: 04cc26f46a29c2a5c2d8393031afe809
    alias: Take Camera Snapshots
  - parallel:
    - service: camera.record
      data:
        duration: 10
        filename: /media/video/camera1_{{states('counter.counter')}}.mp4
      target:
        device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
    - service: camera.record
      data:
        duration: 10
        filename: /media/video/camera2_{{states('counter.counter')}}.mp4
      target:
        device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
    - service: camera.record
      data:
        duration: 10
        filename: /media/video/camera3_{{states('counter.counter')}}.mp4
      target:
        device_id: 04cc26f46a29c2a5c2d8393031afe809
    alias: Start video recording
  - parallel:
    - device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
      domain: button
      entity_id: button.camera1_manual_alarm_start
      type: press
    - device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
      domain: button
      entity_id: button.camera2_manual_alarm_start
      type: press
    - device_id: 04cc26f46a29c2a5c2d8393031afe809
      domain: button
      entity_id: button.camera3_manual_alarm_start
      type: press
    alias: Start Camera Alarms
  - parallel:
    - service: notify.HASSIO_SICUREZZA
      data:
        title: ALLARME ATTIVATO
        message: ALLARME ATTIVATO
        data:
          photo:
          - file: /media/snapshots/camera1_{{states('counter.counter')}}.jpg
            caption: CAM 1
    - service: notify.HASSIO_SICUREZZA
      data:
        title: ALLARME ATTIVATO
        message: ALLARME ATTIVATO
        data:
          photo:
          - file: /media/snapshots/camera2_{{states('counter.counter')}}.jpg
            caption: CAM 2
    - service: notify.HASSIO_SICUREZZA
      data:
        title: ALLARME ATTIVATO
        message: ALLARME ATTIVATO
        data:
          photo:
          - file: /media/snapshots/camera3_{{states('counter.counter')}}.jpg
            caption: CAM 3
    alias: Notify Telegram Snapshots
  - delay:
      hours: 0
      minutes: 0
      seconds: 15
      milliseconds: 0
  - service: hassio.backup_partial
    data:
      folders: media
      name: SICUREZZA-BACKUP
  - service: counter.increment
    data: {}
    target:
      entity_id: counter.counter
- id: dd028cb9c377484a95852aa17e841645
  alias: Video recorded
  description: Video recorded. Creating backup
  trigger:
  - platform: event
    event_type: folder_watcher
    event_data:
      event_type: created
  action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 15
      milliseconds: 0
  - service: hassio.backup_partial
    data:
      folders: media
      name: SICUREZZA-BACKUP
  - service: persistent_notification.create
    data:
      message: '# Video recorded'
      title: '## Motion Detected ##'
  - service: notify.HASSIO_SICUREZZA
    data:
      title: Video Registrato
      message: Video Registrato
  - service: counter.increment
    data: {}
    target:
      entity_id: counter.counter

another update, I discovered there’s a REPEAT function that can be used for automations and on the paper it should be exactly what I need. however it doesn’t work in my case and I’m not able to debug it so I think I’ll give up for now. Here’s the code (it only runs the first time)

  - repeat:
      count: '3'
      sequence:
      - service: persistent_notification.create
        data:
          message: INTRUSIONE RILEVATA {{states('counter.counter')}}
          title: INTRUSIONE RILEVATA
      - parallel:
        - service: camera.snapshot
          data:
            filename: /media/snapshots/camera1_{{states('counter.counter')}}.jpg
          target:
            device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
        - service: camera.snapshot
          data:
            filename: /media/snapshots/camera2_{{states('counter.counter')}}.jpg
          target:
            device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
        - service: camera.snapshot
          data:
            filename: /media/snapshots/camera3_{{states('counter.counter')}}.jpg
          target:
            device_id: 04cc26f46a29c2a5c2d8393031afe809
        alias: Take Camera Snapshots
      - parallel:
        - service: telegram_bot.send_photo
          data:
            file: media/snapshots/camera2_"{{states('counter.counter')}}".jpg
        - service: notify.HASSIO_SICUREZZA
          data:
            title: ALLARME ATTIVATO
            message: ALLARME ATTIVATO
            data:
              photo:
              - file: /media/snapshots/camera2_1.jpg
                caption: CAM 2
        - service: notify.HASSIO_SICUREZZA
          data:
            title: ALLARME ATTIVATO
            message: ALLARME ATTIVATO
            data:
              photo:
              - file: /media/snapshots/camera3_1.jpg
                caption: CAM 3
        alias: Notify Telegram Snapshots
        enabled: false
      - parallel:
        - device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
          domain: button
          entity_id: button.camera1_manual_alarm_start
          type: press
        - device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
          domain: button
          entity_id: button.camera2_manual_alarm_start
          type: press
        - device_id: 04cc26f46a29c2a5c2d8393031afe809
          domain: button
          entity_id: button.camera3_manual_alarm_start
          type: press
        alias: Start Camera Alarms
        enabled: false
      - parallel:
        - service: camera.record
          data:
            duration: 10
            filename: /media/video/camera1_{{states('counter.counter')}}.mp4
          target:
            device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
        - service: camera.record
          data:
            duration: 10
            filename: camera2_{{states('counter.counter')}}.mp4
          target:
            device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
        - service: camera.record
          data:
            duration: 10
            filename: camera3_{{states('counter.counter')}}.mp4
          target:
            device_id: 04cc26f46a29c2a5c2d8393031afe809
        alias: Start Video Recording
      - delay:
          hours: 0
          minutes: 0
          seconds: 15
          milliseconds: 0
      - service: counter.increment
        data: {}
        target:
          entity_id: counter.counter
      - service: hassio.backup_partial
        data:
          folders: media
          name: SICUREZZA-BACKUP

Ok so I found the error! silly bug was breaking everything else and I still haven’t figure out how to debug properly.
Anyhow, this code is working and doing everything correctly!

- id: d200f0ba4ed340b9b2a828232573da6f
  alias: ARM
  description: Start camera when armed night
  trigger:
  - platform: state
    entity_id: alarm_control_panel.ha_alarm
    to: armed_night
    enabled: false
  - platform: event
    event_type: telegram_command
    event_data:
      command: /arm
  action:
  - service: alarm_control_panel.alarm_arm_night
    target:
      entity_id: alarm_control_panel.ha_alarm
    data: {}
  - type: turn_off
    device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
    entity_id: switch.camera1_privacy
    domain: switch
  - type: turn_off
    device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
    entity_id: switch.camera2_privacy
    domain: switch
  - type: turn_off
    device_id: 04cc26f46a29c2a5c2d8393031afe809
    entity_id: switch.camera3_privacy
    domain: switch
  - service: counter.reset
    data: {}
    target:
      entity_id: counter.counter
    enabled: true
- id: 71a5bfe98cb045e2af585115a439e74b
  alias: DISARM
  description: Stop camera when disarmed
  trigger:
  - platform: state
    entity_id: alarm_control_panel.ha_alarm
    to: disarmed
    enabled: false
  - platform: event
    event_type: telegram_command
    event_data:
      command: /disarm
  action:
  - service: alarm_control_panel.alarm_disarm
    target:
      entity_id: alarm_control_panel.ha_alarm
    data: {}
  - parallel:
    - type: turn_on
      device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
      entity_id: switch.camera1_privacy
      domain: switch
    - type: turn_on
      device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
      entity_id: switch.camera2_privacy
      domain: switch
    - type: turn_on
      device_id: 04cc26f46a29c2a5c2d8393031afe809
      entity_id: switch.camera3_privacy
      domain: switch
    alias: Camera privacy ON
  - parallel:
    - device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
      domain: button
      entity_id: button.camera1_manual_alarm_stop
      type: press
    - device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
      domain: button
      entity_id: button.camera2_manual_alarm_stop
      type: press
    - device_id: 04cc26f46a29c2a5c2d8393031afe809
      domain: button
      entity_id: button.camera3_manual_alarm_stop
      type: press
    alias: Camera alarm STOP
- id: adc024cb4e8d4090b9cb1d017a1d5871
  alias: ALARM TRIGGERED
  description: Motion detected!!
  trigger:
  - platform: event
    event_type: telegram_command
    event_data:
      command: /trigger
    enabled: false
  - platform: state
    entity_id:
    - binary_sensor.camera1_motion
    - binary_sensor.camera2_motion
    - binary_sensor.camera3_motion
    from: 'off'
    to: 'on'
  condition: []
  action:
  - service: alarm_control_panel.alarm_trigger
    data: {}
    target:
      entity_id: alarm_control_panel.ha_alarm
  - service: notify.hassio_sicurezza
    data:
      message: ALLARME ATTIVATO
      title: ALLARME ATTIVATO
  - repeat:
      count: '3'
      sequence:
      - service: persistent_notification.create
        data:
          message: INTRUSIONE RILEVATA {{states('counter.counter')}}
          title: INTRUSIONE RILEVATA
      - parallel:
        - service: camera.snapshot
          data:
            filename: /media/snapshots/camera1_{{states('counter.counter')}}.jpg
          target:
            device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
        - service: camera.snapshot
          data:
            filename: /media/snapshots/camera2_{{states('counter.counter')}}.jpg
          target:
            device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
        - service: camera.snapshot
          data:
            filename: /media/snapshots/camera3_{{states('counter.counter')}}.jpg
          target:
            device_id: 04cc26f46a29c2a5c2d8393031afe809
        alias: Take Camera Snapshots
      - parallel:
        - service: notify.HASSIO_SICUREZZA
          data:
            title: ALLARME ATTIVATO
            message: ALLARME ATTIVATO
            data:
              photo:
              - file: /media/snapshots/camera1_{{states('counter.counter')}}.jpg
                caption: CAM 1
        - service: notify.HASSIO_SICUREZZA
          data:
            title: ALLARME ATTIVATO
            message: ALLARME ATTIVATO
            data:
              photo:
              - file: /media/snapshots/camera2_{{states('counter.counter')}}.jpg
                caption: CAM 2
        - service: notify.HASSIO_SICUREZZA
          data:
            title: ALLARME ATTIVATO
            message: ALLARME ATTIVATO
            data:
              photo:
              - file: /media/snapshots/camera3_{{states('counter.counter')}}.jpg
                caption: CAM 3
        alias: Notify Telegram Snapshots
        enabled: true
      - parallel:
        - device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
          domain: button
          entity_id: button.camera1_manual_alarm_start
          type: press
        - device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
          domain: button
          entity_id: button.camera2_manual_alarm_start
          type: press
        - device_id: 04cc26f46a29c2a5c2d8393031afe809
          domain: button
          entity_id: button.camera3_manual_alarm_start
          type: press
        alias: Start Camera Alarms
        enabled: false
      - parallel:
        - service: camera.record
          data:
            duration: 10
            filename: /media/video/camera1_{{states('counter.counter')}}.mp4
          target:
            device_id: 32d6d5e6b6a5d11af8b2e9856a04f972
        - service: camera.record
          data:
            duration: 10
            filename: /media/video/camera2_{{states('counter.counter')}}.mp4
          target:
            device_id: 057faeb73bf2bd5d4a0d99efd92de5bf
        - service: camera.record
          data:
            duration: 10
            filename: /media/video/camera3_{{states('counter.counter')}}.mp4
          target:
            device_id: 04cc26f46a29c2a5c2d8393031afe809
        alias: Start Video Recording
      - delay:
          hours: 0
          minutes: 0
          seconds: 15
          milliseconds: 0
      - service: counter.increment
        data: {}
        target:
          entity_id: counter.counter
      - service: hassio.backup_partial
        data:
          folders: media
          name: SICUREZZA-BACKUP