How to play XIaomi Gateway ringtones sound in loop?

Hi, Xiaomi Gateway ringtones are very shorts.
I wish to play them in loop when a door/wndows sensor is opened… how can I do it?

  - alias: Play ringtone when window is open
    trigger:
      - platform: state
        entity_id: binary_sensor.door_window_sensor_123456789
        from: 'off'
        to: 'on'
    action:
      - service: xiaomi_aqara.play_ringtone
        data:
          gw_mac: !secret gw_mac
          ringtone_id: 10
          ringtone_vol: 100
1 Like

Hi

I have done something similar for an Alarm Siren based on the Xiaomi Gateway. I have done this by a Script being called by the automation.

There is the script, I’m still learning scripting, so there may be a better way, but will give you an idea.

############################################################
##                   Alarm Siren Loop                     ##
############################################################

# START XIAOMI SIREN 
  xiaomi_alarm_siren:
    alias: Xiaomi Alarm Siren 
    sequence:
      - service: xiaomi_aqara.play_ringtone
        data_template:
          ringtone_id: 0
          ringtone_vol: 100
          gw_mac: 34:CE:00:91:F2:BC
      - service: xiaomi_aqara.play_ringtone
        data_template:
          ringtone_id: 0
          ringtone_vol: 100
          gw_mac: 34:CE:00:90:83:4F
      - delay:
#### time for siren to play before restarting 
          seconds: 14
      - service: script.turn_on
        data:
          entity_id: script.xiaomi_alarm_siren_loop

# LOOP BACK TO START SIREN AGAIN         
  xiaomi_alarm_siren_loop:
    alias: Xiaomi Alarm loop
    sequence:
      - delay:
#### time for siren to start
          seconds: .1
      - service: script.turn_on
        data:
          entity_id: script.xiaomi_alarm_siren
# CANCEL Siren when Disarm - refer to Automation
  xiaomi_alarm_siren_cancel:
    alias: Cancel Siren Cancel
    sequence:
      - delay:
          seconds: 1
      - service: script.turn_off
        data:
          entity_id: script.xiaomi_alarm_siren
      - service: script.turn_off
        data:
          entity_id: script.xiaomi_alarm_siren_loop

HI @Hefford27 I’m a newbie… after I put your code in script section of configuration.yaml or in the script.yaml file… how do I call this script in my automation?

hi try this (Again I am quite new, so might not be the best way :slight_smile: )

When Alarm Triggers Automation

- alias: 'Alarm Triggered Siren'
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: alarm_control_panel.house
    to: 'triggered'
  action:
  - service: script.turn_on
    entity_id: script.xiaomi_alarm_siren
  - service: switch.turn_on
    data:
      entity_id: switch.outside_alarm_siren

To Turn Off Siren When Alarm Disarmed Automation

- alias: 'Alarm Disarm Sound Turn off Siren and Stop Gateways Flashing'
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: alarm_control_panel.house
    to: 'disarmed'
  action:
  - service: xiaomi_aqara.play_ringtone
    data:
      gw_mac: 34:CE:00:90:83:4F
      ringtone_id: 10002
      ringtone_vol: 50
  - service: script.turn_on
    entity_id: script.alarmdisarmdashchange
  - service: script.turn_on
    entity_id: script.xiaomi_alarm_siren_cancel
  - service: switch.turn_off
    data:
      entity_id: switch.outside_alarm_siren

Just change the trigger to your sensor and state .

Please note I have a recorded tone that plays on Disarm and I have my Gateways Flash Red when the alarm triggers and this also turns them off, so need to remove those lines, I can if you want give you the Flash Gateway script if your interested.

I’m using the System Alarm Add-On, it is a complete system and it already have flashing lights of gateway and it is easy to configure

do you know this project?

1 Like

I have seen it but I have been using the one from

i like the Alarm UI and the check and stuff it does

you have an external siren, which type?

what is this?

yes please, thanks, which ringtone?

this is the call for the Script to change the display on the Wall Tablet for then the Alarm is disarmed all the Wall Panels show a Disarm Message…

Also have TTS working so I get an audible message too.

here you go

############################################################
###          ALARM TRIGGER START FLASHING LIGHT          ###
############################################################

# START FLASH  
  light_flash:
    alias: Alarm Trigger flash on
    sequence:
      - service: homeassistant.turn_on
        data:
          entity_id: 
          - scene.downstairs_night_light_red
          - scene.upstairs_night_light_red
          - scene.porch_led_light_red
      - delay:
#### time for flash light on
          seconds: .02
      - service: homeassistant.turn_off
        data:
          entity_id: 
          - light.gateway_light_34********
          - light.gateway_light_34********
          - light.gateway_light_34*********
      - service: script.turn_on
        data:
          entity_id: script.light_loop
# LOOP BACK TO START FLASH AGAIN         
  light_loop:
    alias: Light flash loop
    sequence:
      - delay:
#### time for flash light off
          seconds: .02
      - service: script.turn_on
        data:
          entity_id: script.light_flash
# CANCEL FLASHING 
  light_flash_cancel:
    alias: Cancel Light Flash
    sequence:
      - service: script.turn_off
        data:
          entity_id: script.light_flash
      - service: script.turn_off
        data:
          entity_id: script.light_loop

I recorded the alarm disarm tone and the 30 sec countdown tones from my old Alarm using the Xiaomi App, and play these when the alarm is activated / triggered (before the Siren after 30 secs) and a Disarm tone.

2 Likes

Thank you for the scripts!!! For some reason the alarm_siren_cancel works only half the time. Meaning I have to arm/disarm again when the first time the sound loop still keeps running. Do you have this issue? I wonder if my old laptop is too old to handle it correctly?

This is the automation i use.

  • alias: ‘Turn off alarm siren’
    trigger:
    • platform: state
      entity_id: alarm_control_panel.ha_alarm
      to: ‘disarmed’
      action:
      service: script.turn_on
      entity_id: script.xiaomi_alarm_siren_cancel

Please disregard. I have found the example where the author uses stop_ringtone instead.

https://community.home-assistant.io/t/xiaomi-gateway-alarm/44230

Anyone experienced that when the alarm goes off and the Xiaomi starts the loop, it only does it for 5 minutes, then stops and the ‘Yet another alarm component’ goes from Triggered to Armed Away again?

EDIT: I think this must be related to something else.

Hi All,

I have started adventure with HA week ago. Maybe this will be helpful for somebody.
To make a loop for xiaomi gateway sound or light just use scripts that start another script.
To turn off stop scripts. In automation call first script of loop.

scripts.yaml

## Siren Loop ##
xiaomi_alarm1:
  sequence:
    - service: xiaomi_aqara.play_ringtone
      data:
        ringtone_id: 2
        ringtone_vol: 1
        gw_mac: !secret xiaomi_mac
    - delay:
        seconds: 5
    - service: script.xiaomi_alarm2

xiaomi_alarm2:
  sequence:
    - service: xiaomi_aqara.play_ringtone
      data:
        ringtone_id: 2
        ringtone_vol: 1
        gw_mac: !secret xiaomi_mac
    - delay:
        seconds: 5
    - service: script.xiaomi_alarm1

## Light Loop ##
flash_gateway1:
  alias: Flash Gateway On Red
  sequence:
    - service: light.turn_on
      data:
        entity_id: light.gateway_light_<xiaomi_gw_mac>
        color_name: red
        brightness: 255
    - delay:
        milliseconds: 600
    - service: light.turn_off
      data:
        entity_id: light.gateway_light_<xiaomi_gw_mac>
    - service: script.flash_gateway2

flash_gateway2:
  alias: Flash Gateway On Blue
  sequence:
    - service: light.turn_on
      data:
        entity_id: light.gateway_light_<xiaomi_gw_mac>
        color_name: blue
        brightness: 255
    - delay:
        milliseconds: 600
    - service: light.turn_off
      data:
        entity_id: light.gateway_light_<xiaomi_gw_mac>
    - service: script.flash_gateway1

## Siren and Light Off ##
alarm_off:
  alias: Alarm was disarmed
  sequence:
    
    - service: script.turn_off
      data: 
        entity_id: 
        - script.flash_gateway1
        - script.flash_gateway2
        - script.xiaomi_alarm1
        - script.xiaomi_alarm2
## Disarm confirmation ##
    - service: light.turn_on
      data:
        entity_id: light.gateway_light_<xiaomi_gw_mac>
        color_name: green
        brightness: 255
    - delay:
        seconds: 60
    - service: light.turn_off
      data:
        entity_id: light.gateway_light_<xiaomi_gw_mac>

automation.yaml

## Alarm ##
- alias: Alarm!
  hide_entity: true
  trigger:
    - platform: state
      entity_id: alarm_control_panel.alarm
      to: 'triggered'
  action:
    - service: script.xiaomi_alarm1
    - service: script.flash_gateway1

## Disarm ##
- alias: Disarm!
  hide_entity: true
  trigger:
    - platform: state
      entity_id: alarm_control_panel.alarm
      to: 'disarmed'
  action:
    - service: script.alarm_off
1 Like

Does not work for some reason, plays second script and stops.

same thing happened to me, stops after 2nd script.

Further investigation highlighted that the first script was still running hence it would not run again.

Logger: homeassistant.components.script.security_countdown_1
Source: helpers/script.py:1156
Integration: Scripts (documentation, issues)
First occurred: 10:00:08 (3 occurrences)
Last logged: 10:16:38

security_countdown_1: Already running

To fix this i changed the scripts to include closing the other script. My code is as below:

script:
  security_countdown_1:
    sequence:
    - service: script.turn_off
      entity_id: script.security_countdown_2
    - service: xiaomi_aqara.play_ringtone
      data:
        gw_mac: f0b429ccxxxx
        ringtone_id: 29 
        ringtone_vol: 10
    - delay: "00:00:15"
    - service: script.turn_on
      entity_id: script.security_countdown_2
  security_countdown_2:
    sequence:
    - service: script.turn_off
      entity_id: script.security_countdown_1
    - service: xiaomi_aqara.play_ringtone
      data:
        gw_mac: f0b429ccxxxx
        ringtone_id: 29 
        ringtone_vol: 10
    - delay: "00:00:15"
    - service: script.turn_on
      entity_id: script.security_countdown_1
  stop_security_sounds:
    alias: Stop Security Sound Scripts on Gateway
    sequence:
    - service: script.turn_off
      target:
        entity_id:
          - script.security_countdown_1
          - script.security_countdown_2
    - service: xiaomi_aqara.stop_ringtone
      data:
        gw_mac: f0b429ccxxxx

I’ve done an extensive write-up on how I use the Xiaomi Gateway as a siren for a Home Assistant Alarm System. If you’re interested read it here: Home Assistant Manual Security System with Xiaomi Gateway as Siren