How to switch volume and have alarm ring only 5 minutes

I’ve got HA installed on a mini PC x64 i5-8500 and I’m very new to HA.

I’ve got a moist or water detector and I don’t have problems to get it to work if it detects water on it’s sensors.

But this is what I want to achieve:
1.) If water is detected then
a. Set sirene volume to max
b. Turn sirene on using ringtone 1
c. Turn sirene on for max. 5 minutes

2.) after 5 minutes, turn all back to state before so
a. Turn sirene off
b.) Set sirene volume to medium
c.) Set sirene ringtone to what is was before, or just to a certain choice

However also, if water is not detected anymore within 5 minutes, then also stop sirene instantly and turn back volume and ringtone as described in 2.

This is what I have now in yaml:

description: Water gedetecteerd
triggers:
  - type: moist
    device_id: 92a0c460d62d86870d865922dd044ef0
    entity_id: 34b3cd80ddf0583d2e741307ae109b90
    domain: binary_sensor
    trigger: device
    for:
      hours: 0
      minutes: 0
      seconds: 0
conditions: []
actions:
  - type: toggle
    device_id: bfc1d8066997959e18fcd91a769cd712
    entity_id: 5f9e63c6392edfb61bfd3c786b0cb3f3
    domain: switch
mode: single

I tried using Block and use “run in parallel” and “run in sequence”, but every time I add something in the “Then do” block in the automation, the sirene refuses to sound.

So for me this is a big automation and I normally don’t use YAML but the GUI. So maybe somebody can help me getting this right. I’m sure I’m must be doing something wrong, I just don’t know what.

Great question. First I’d recommend creating an alert. Alerts are nice because you can also send them to your phone (if your away), you can acknowledge them (and hence cause the siren to stop)

Once you have the alert setup, here is an automation I use that turns a siren on and off (and does some other things). Since the automation has the mode “restart” whenever the alert updates the currently executing automation stops and a new instance starts.

This should get you started and try to not use guids it makes it harder to understand

- id: al emergency al_fa_dining_room_smoke
  alias: al emergency al_fa_dining_room_smoke
  trigger:
    - platform: state
      entity_id: alert.al_fa_dining_room_smoke
  mode: restart
  variables:
    to_state: "{{ trigger.to_state.state }}"
    from_state: "{{ trigger.from_state.state }}"
    cleared: "{{ to_state == 'idle' and (from_state == 'on' or from_state == 'off')}}"
    acknowledged: "{{ to_state == 'off' }}"
    generated: "{{ to_state == 'on' }}"
    skip_first: "{{state_attr('alert.al_fa_dining_room_smoke','alert_skip_first')}}"
    skip_delay: "{{state_attr('alert.al_fa_dining_room_smoke','alert_skip_delay')}}"
  actions:
    - if: "{{ skip_first and generated }}"
      then:
        - delay: 
            minutes: "{{ skip_delay }}"
    - action: system_log.write
      data:
        level: info
        message: "Alert al_fa_dining_room_smoke emergency generated: {{generated}} cleared: {{cleared}} ack: {{acknowledged}}"
        logger: alert
    - action: script.turn_on
      target:
        entity_id: script.al_send_alert_to_remote
      data:
        variables:
          alert_id: alert.al_fa_dining_room_smoke      
    - choose:
        - conditions: "{{ generated }}"
          sequence:
            - event: lt_all_on
              event_data:
                message: "On emergency alert.al_fa_dining_room_smoke"
            - repeat:
                until: "{{ repeat.index > 10 or states('alert.al_fa_dining_room_smoke') != 'on' }}"
                sequence:      
                  - event: al_announce_emergency
                    event_data:
                      message: >-
                        {% from 'al_macros.jinja' import al_gen_message %}
                        {% set message = al_gen_message('alert.al_fa_dining_room_smoke') %} 
                        {% set location = state_attr('alert.al_fa_dining_room_smoke','alert_location').replace("_"," ") %}                     
                        {{ states("sensor.aw_zone_home_name") + " " + message + " " + location }}
                      alert_id: alert.al_fa_dining_room_smoke
                      repeat: 5
                      siren: emergency
                      siren_action: turn_on
                  - delay: "00:01:00"
        - conditions: "{{ acknowledged or cleared }}"
          sequence:
            - event: al_announce_emergency
              event_data:
                message: "{{ 'Alert ' + ('Acknowledged' if acknowledged else 'Cleared') }}"
                alert_id: alert.al_fa_dining_room_smoke
                repeat: 2
                siren: emergency
                siren_action: turn_off

Here is the alert definition. I like to add metadata to alerts for use elsewhere.

homeassistant:
  customize:
    alert.al_fa_dining_room_smoke:
      alert_class: 'fire'
      alert_severity: 'emergency'
      alert_message: 'fire alarm smoke detected'
      alert_location: 'dining_room'
      alert_skip_first: false
      alert_skip_delay: 0
alert:
  al_fa_dining_room_smoke:
    name: fa_dining_room_smoke
    message: 'ALERT {{states("sensor.aw_zone_home_name")}} fa_dining_room_smoke fire alarm smoke detected'
    done_message: 'Cleared {{states("sensor.aw_zone_home_name")}} fa_dining_room_smoke fire alarm smoke detected'
    entity_id: binary_sensor.al_fa_dining_room_smoke_alert
    state: "on"
    title: '{{states("sensor.aw_zone_home_name")}} Fire'
    can_acknowledge: true
    skip_first: false
    notifiers:
      - sms_emergency_alert
    data:
      actions:
        - action: ACK_AL_FA_DINING_ROOM_SMOKE
          title: Acknowledge
        - action: NONE
          title: No Action
    repeat:
      - 1
      - 1
      - 1
      - 5

Thank you very mucht for your quick and great reply!
Creating an alert is a great idea. I didn’t realise I could do that too. I’m still digging deeper in all kind of things. There is so much possible.

Although the script looks great it’s abracadabra to me, looks like a kind of programming language.
However I’m sure that will come in very handy later on too, although we already have smoke detectors from the housing company which I can’t add in HA.

Isn’t there a bit less complicated way to reach what I would like with only a home assistant part?

Also. There are moments that I’m not at home and I forget my phone too, hence the reason to not have the sirene make noise for a longer period than 5 minutes.
I will start anyway by trying to get that alert in.

What do you mean by “try not to use guids”? Do you mean those device id and entity id I have in there?
If yes, I did rename those in the devices, but did not tick the “also rename in HA” option, because I was afraid it could mess things up.
But I can just rename them there too is that what you mean?

I think I found a bit more easy solution (for my doing for the time being) and build it like this.
Still curious about what you ment with the guid.

description: Water gedetecteerd
triggers:
  - type: moist
    device_id: 92a0c460d62d86870d865922dd044ef0
    entity_id: 34b3cd80ddf0583d2e741307ae109b90
    domain: binary_sensor
    trigger: device
    for:
      hours: 0
      minutes: 0
      seconds: 0
    id: VN1
  - type: not_moist
    device_id: 92a0c460d62d86870d865922dd044ef0
    entity_id: 34b3cd80ddf0583d2e741307ae109b90
    domain: binary_sensor
    trigger: device
    id: VN2
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - VN1
        sequence:
          - sequence:
              - device_id: bfc1d8066997959e18fcd91a769cd712
                domain: select
                entity_id: 5cec7d1eeb16bd9f6085a55c7a31d2cd
                type: select_option
                option: ringtone 3
              - device_id: bfc1d8066997959e18fcd91a769cd712
                domain: select
                entity_id: 17e4f054bc05cb3f5835825e357bd3ab
                type: select_option
                option: high
              - type: turn_on
                device_id: bfc1d8066997959e18fcd91a769cd712
                entity_id: 5f9e63c6392edfb61bfd3c786b0cb3f3
                domain: switch
              - device_id: bfc1d8066997959e18fcd91a769cd712
                domain: number
                entity_id: b4e7644a67c1dd41af21b6db6c251501
                type: set_value
                value: 300
      - conditions:
          - condition: trigger
            id:
              - VN2
        sequence:
          - sequence:
              - type: turn_off
                device_id: bfc1d8066997959e18fcd91a769cd712
                entity_id: 5f9e63c6392edfb61bfd3c786b0cb3f3
                domain: switch
              - device_id: bfc1d8066997959e18fcd91a769cd712
                domain: select
                entity_id: 5cec7d1eeb16bd9f6085a55c7a31d2cd
                type: select_previous
                cycle: false
              - device_id: bfc1d8066997959e18fcd91a769cd712
                domain: select
                entity_id: 17e4f054bc05cb3f5835825e357bd3ab
                type: select_previous
mode: single

Would that be correct or does this need adjustment? Maybe the mode:single on the bottom, I don’t know. It seems to work this way.

I will have to build in the alert later. First have to configure that.