Xiaomi Gateway - Alarm

I’m trying to make a simple alarm system with Xiaomi gateway a door sensor and a wireless switch.
The thing I’m trying to achieve :
Press single click, set alarm after 60 seconds.
Door open -> wait 10 seconds -> trigger alarm for 10 minutes or until disarmed with long press.

It kinda works, I found 1 problem with it, after 5-6 seconds, the gateway stops playing the alarm ringtone. HA stays it is in “triggered” state and I can see it is alarming, but sound stops. Tried to increase trigger_time but no sound at all.

configuration.yaml

alarm_control_panel:
  - platform: manual
    name: HAalarm

automations.yaml

- alias: Alarm - Away
  trigger:
# alarm on fire
    - platform: state
      entity_id: binary_sensor.smoke_sensor_158d0001xxxxxx
      to: 'on'
# door opened
    - platform: state
      entity_id: binary_sensor.door_window_sensor_158d0001xxxxxx
      to: 'on'
# motion detected
    - platform: state
      entity_id: group.all_motion
      to: 'on'

  condition:
    - condition: state
      entity_id: alarm_control_panel.haalarm
      state: armed_away
  action:
    - service: alarm_control_panel.alarm_trigger
      entity_id: alarm_control_panel.haalarm



- alias: Alarm!
  hide_entity: true
  trigger:
    - platform: state
      entity_id: alarm_control_panel.haalarm
      to: 'triggered'
  action:
    - service: xiaomi_aqara.play_ringtone
      data:
        gw_mac: 34CE009xxxxx
        ringtone_id: 2
        ringtone_vol: 2

####################################################################################################

- alias: Disarm
  trigger:
    - platform: event
      event_type: click
      event_data:
        entity_id: binary_sensor.switch_158d0001xxxxxx
        click_type: long_click_press
  action:
    - service: xiaomi_aqara.play_ringtone
      data:
        ringtone_id: 11
        ringtone_vol: 1
        gw_mac: 34CE009xxxxx
    - service: alarm_control_panel.alarm_disarm
      entity_id: alarm_control_panel.haalarm



- alias: Disarm!
  hide_entity: true
  trigger:
    - platform: state
      entity_id: alarm_control_panel.haalarm
      to: 'disarmed'
  action:
    - service: xiaomi_aqara.stop_ringtone
      data:
        gw_mac: 34CE009xxxxx
2 Likes

Xiaomi Gateway ring tones don’t repeat and have only a few seconds duration.
You can upload a bigger file to the gateway to use instead
Or copy/paste the service: xiaomi_aqara.play_ringtone as many times as you want it to play/repeat :smile:

script:
  xiaomi_gateway_alarm:
    sequence:
      - service: homeassistant.turn_off
        entity_id: input_boolean.xiaomi_alarm_repeat
      - service: xiaomi_aqara.play_ringtone
        data:
          ringtone_id: 2
          ringtone_vol: 2
          gw_mac: 34CE009xxxxx
      - delay: 00:00:05
      - service: homeassistant.turn_on
        entity_id: input_boolean.xiaomi_alarm_repeat

input_boolean:
  xiaomi_alarm_repeat:
    name: "Xiaomi Alarm Repeat"
    initial: off

automation:
- alias: "xiaomi gateway alarm 01"
  hide_entity: true
  trigger:
    - platform: state
      entity_id: input_boolean.xiaomi_alarm_repeat
      from: 'off'
      to: 'on'
  action:
    - service: homeassistant.turn_on
      entity_id: script.xiaomi_gateway_alarm

You can also use something like I wrote above to keep it repeating and use the input_boolean as the trigger

1 Like

Umm, okay, so this is a new thing for me.

I put this inside scripts.yaml:
script:

  xiaomi_gateway_alarm:
    sequence:
      - service: homeassistant.turn_off
        entity_id: input_boolean.xiaomi_alarm_repeat
      - service: xiaomi_aqara.play_ringtone
        data:
          ringtone_id: 2
          ringtone_vol: 2
          gw_mac: 34CE009xxxxx
      - delay: 00:00:05
      - service: homeassistant.turn_on
        entity_id: input_boolean.xiaomi_alarm_repeat

And then put this inside configuration.yaml:

> input_boolean:
>   xiaomi_alarm_repeat:
>     name: "Xiaomi Alarm Repeat"
>     initial: off

And put this inside automations.yaml:
automation:>

- alias: "xiaomi gateway alarm 01"
  hide_entity: true
  trigger:
    - platform: state
      entity_id: input_boolean.xiaomi_alarm_repeat
      from: 'off'
      to: 'on'
  action:
    - service: homeassistant.turn_on
      entity_id: script.xiaomi_gateway_alarm

But it still plays ringtome 1 time. What did I miss? :slight_smile:

Change:

- alias: Alarm!
  hide_entity: true
  trigger:
    - platform: state
      entity_id: alarm_control_panel.haalarm
      to: 'triggered'
  action:
    - service: xiaomi_aqara.play_ringtone
      data:
        gw_mac: 34CE009xxxxx
        ringtone_id: 2
        ringtone_vol: 2

to:

- alias: Alarm!
  hide_entity: true
  trigger:
    - platform: state
      entity_id: alarm_control_panel.haalarm
      to: 'triggered'
  action:
    - service: homeassistant.turn_on
      entity_id: input_boolean.xiaomi_alarm_repeat

Also your Automation is hidden, better change to:

- alias: "xiaomi gateway alarm 01"
  initial_state: True
  hide_entity: true
  trigger:
    - platform: state
      entity_id: input_boolean.xiaomi_alarm_repeat
      from: 'off'
      to: 'on'
  action:
    - service: homeassistant.turn_on
      entity_id: script.xiaomi_gateway_alarm

Here is a workaround for the ringtones not natively looping on the gateway. It’s not perfect, but it works.

Following your instructions when the alarm calls the script i get this:
“Script script.xiaomi_gateway_alarm already running.”

this is the automation right now:

- alias: Alarm - Away
  trigger:
# alarm on fire
    - platform: state
      entity_id: binary_sensor.smoke_sensor_158d0001xxxxxx
      to: 'on'
# door opened
    - platform: state
      entity_id: binary_sensor.door_window_sensor_158d0001xxxxxx
      to: 'on'
# motion detected
    - platform: state
      entity_id: group.all_motion
      to: 'on'

  condition:
    - condition: state
      entity_id: alarm_control_panel.haalarm
      state: armed_away
  action:
    - service: alarm_control_panel.alarm_trigger
      entity_id: alarm_control_panel.haalarm



- alias: Alarm!
  hide_entity: true
  trigger:
    - platform: state
      entity_id: alarm_control_panel.haalarm
      to: 'triggered'
  action:
    - service: homeassistant.turn_on
      entity_id: input_boolean.xiaomi_alarm_repeat

####################################################################################################

- alias: Disarm switch
  trigger:
    - platform: event
      event_type: click
      event_data:
        entity_id: binary_sensor.switch_158d0001xxxxxx
        click_type: long_click_press
  action:
    - service: xiaomi_aqara.play_ringtone
      data:
        ringtone_id: 11
        ringtone_vol: 1
        gw_mac: 22330090AABB
    - service: alarm_control_panel.alarm_disarm
      entity_id: alarm_control_panel.haalarm



- alias: Disarm!
  hide_entity: true
  trigger:
    - platform: state
      entity_id: alarm_control_panel.haalarm
      to: 'disarmed'
  action:
    - service: xiaomi_aqara.stop_ringtone
      data:
        gw_mac: 22330090AABB

####################################################################################################

- alias: "xiaomi gateway alarm 01"
  initial_state: True
  hide_entity: true
  trigger:
    - platform: state
      entity_id: input_boolean.xiaomi_alarm_repeat
      from: 'off'
      to: 'on'
  action:
    - service: homeassistant.turn_on
      entity_id: script.xiaomi_gateway_alarm

Thank you! But that script is outdated and I’m pretty dumb for things like this to fix it, but I tried and failed miserably.

I think I will just put in sound 20 times in a row and cry in the corner.

Hi @BullFrog,

script:
  xiaomi_gateway_alarm:
    sequence:
      - service: homeassistant.turn_off
        entity_id: input_boolean.xiaomi_alarm_repeat
      - service: xiaomi_aqara.play_ringtone
        data:
          ringtone_id: 2
          ringtone_vol: 2
          gw_mac: 34CE009xxxxx
      - delay: 00:00:05
      - service: homeassistant.turn_on
        entity_id: input_boolean.xiaomi_alarm_repeat
      - delay: 00:00:01

input_boolean:
  xiaomi_alarm_repeat:
    name: "Xiaomi Alarm Repeat"
    initial: off

automation:
- alias: "xiaomi gateway alarm 01"
  hide_entity: false
  trigger:
    - platform: state
      entity_id: input_boolean.xiaomi_alarm_repeat
      from: 'off'
      to: 'on'
  action:
    - delay: 00:00:01
    - service: homeassistant.turn_on
      entity_id: script.xiaomi_gateway_alarm 

Try this and use the input_boolean as the alarm trigger.
I had a look at the post from @pplucky and looks great for this kind of things and if it is outdated shouldn’t be very hard to fix.

I don’t know why you say this is outdated, but in my 0.63.3 Hassbian installation works great, but your mileage may vary.

Good luck on your endeavour.

Thank you pplucky!
Now I have time for a round 2, most of the errors was easy to fix, like changing to service to xiaomi_aqara.play_ringtone and so on.
The other thing was you used a different script name in example than in automation, it was easy too after read the log files.

I will give it another shot, but I don’t get this error message.

Script script.play_sound already running.
8:55 components/script.py (WARNING)
Script script.play_sound already running.
8:55 components/script.py (WARNING)
Error executing service <ServiceCall script.play_sound: ringtone_vol=1, ringtone_id=10, delay=2>
8:55 helpers/template.py (ERROR)
Error rendering data template: UndefinedError: 'None' has no attribute 'state'
8:55 helpers/service.py (ERROR)
Forget the parts I use, attached below:

Config.yaml

alarm_control_panel:
  - platform: manual
    name: Riasztó
    trigger_time: 120
    pending_time: 2
    delay_time: 2


input_boolean:
  mute_gateway_sounds:
    name: Mute
    icon: mdi:volume-off

############################################

script.yaml

play_sound:
  alias: "Loop Play Sound"
  sequence:
    - condition: state
      entity_id: input_boolean.mute_gateway_sounds
      state: 'off'
    - service: xiaomi_aqara.play_ringtone
      data_template:
        gw_mac: !secret xiaomi_mac
        ringtone_id: "{{ states.input_select.gateway_sound.state.split('-')[0] }}"
        ringtone_vol: "{{ states.input_slider.gateway_volume.state|int }}"
    - delay: '00:00:{{ states.input_slider.loop_delay.state | int }}'
    - service: script.play_sound_loop

play_sound_loop:
  alias: "Play selected sound in Loop"
  sequence:
    - condition: state
      entity_id: input_boolean.mute_gateway_sounds
      state: 'off'
    - delay: '00:00:{{ states.input_slider.loop_delay.state | int }}'
    - service: script.play_sound

#################################################
automation.yaml

# Stop sound when alarm is disarmed or armed ###
- alias: "Alarm Turn off or Armed"
  trigger:
    - platform: state
      entity_id: alarm_control_panel.riaszto
      to: 'disarmed'
    - platform: state
      entity_id: alarm_control_panel.riaszto
      to: 'armed_away'
    - platform: state
      entity_id: alarm_control_panel.riaszto
      to: 'armed_home'
  action:
# Turn on gateway mute NOT to play sound ###
    - service: input_boolean.turn_on
      data:
        entity_id: input_boolean.mute_gateway_sounds
# Stop ringtone sound --> didn't seem to work due to loop ###
    - service: xiaomi_aqara.stop_ringtone
      data:
        gw_mac: !secret xiaomi_mac
# Wait 10 seconds & then turn off gateway mute ###
    - delay: 
        seconds: 10
    - service: input_boolean.turn_off
      data:
        entity_id: input_boolean.mute_gateway_sounds


# Alarm trigger while armed away ###
- alias: "Alarm trigger while armed away"
  trigger:
# Front Door opened ###
    - platform: state
      entity_id: binary_sensor.door_window_sensor_158d00011234
      to: 'on'
# Any movement detected ###
#    - platform: state
#      entity_id: group.all_motion
#      to: 'on'
# Alarm status is armed away ###
  condition:
    condition: state
    entity_id: alarm_control_panel.riaszto
    state: 'armed_away'
  action:
# Turn off gateway mute to play sound ###
    - service: input_boolean.turn_off
      data:
        entity_id: input_boolean.mute_gateway_sounds
# Trigger alarm ###
    - service: alarm_control_panel.alarm_trigger
      entity_id: alarm_control_panel.riaszto
# Play ringtone sound in loop ###
    - service: script.play_sound
      data:
        ringtone_id: 8
        ringtone_vol: 1
        delay: 6

This is all due to the fact that you did not copy the full code and did not include input_slider.gateway_volume, input_select.gateway_sound and input_slider.loop, which are used in the scripts called in automation.

If you don’t want those dynamic, you need to adapt your configuration a bit more.

Thans for the reply, tried it now, all the inputs are there, and I even changed ‘input_slider’ to ‘input_number’, because it said it changed.

The same errors are still there.

Error executing service <ServiceCall script.play_sound: delay=2, ringtone_vol=1, ringtone_id=10>

Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/template.py", line 131, in async_render
    return self._compiled.render(kwargs).strip()
  File "/srv/homeassistant/lib/python3.5/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/srv/homeassistant/lib/python3.5/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/srv/homeassistant/lib/python3.5/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
  File "/srv/homeassistant/lib/python3.5/site-packages/jinja2/filters.py", line 653, in do_int
    return int(value)
jinja2.exceptions.UndefinedError: 'None' has no attribute 'state'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/core.py", line 1010, in _event_to_service_call
    yield from service_handler.func(service_call)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/script.py", line 163, in service_handler
    yield from script.async_turn_on(variables=service.data)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/script.py", line 215, in async_turn_on
    yield from self.script.async_run(kwargs.get(ATTR_VARIABLES))
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/script.py", line 105, in async_run
    delay.async_render(variables))
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/template.py", line 133, in async_render
    raise TemplateError(err)
homeassistant.exceptions.TemplateError: UndefinedError: 'None' has no attribute 'state'

Try changing your scripts for:

### Play Provided sound in loop on Gateway ###
script:
  play_sound:
    alias: "Play Sound"
    sequence:
### Turn off gateway mute to play sound ###
      - condition: state
        entity_id: input_boolean.mute_gateway_sounds
        state: 'off'
### Play ringtone sound provided ###
      - service: xiaomi_aqara.play_ringtone
        data_template:
          gw_mac: !secret xiaomi_mac
          ringtone_id: "{{ ringtone_id }}"
          ringtone_vol: "{{ ringtone_vol }}"
### Wait & call another script that call this one ###
### in order to generate an infinite loop until gateway ###
### mute is turned on by some other action ###
      - delay: '00:00:{{ delay | int }}'
      - service: script.play_sound_loop
        data_template:
          ringtone_id: "{{ ringtone_id }}"
          ringtone_vol: "{{ ringtone_vol }}"
          delay: "{{ delay }}"

  play_sound_loop:
    alias: "Play Sound in Loop"
### Wait & call script that called this one ###
### in order to generate an infinite loop until ###
### gateway mute is turned on by some other action ###
    sequence:
      - delay: '00:00:{{ delay | int }}'
      - service: script.play_sound
        data_template:
          ringtone_id: "{{ ringtone_id }}"
          ringtone_vol: "{{ ringtone_vol }}"
          delay: "{{ delay }}"

With these scripts, you don’t actually need the input_slider.gateway_volume, input_select.gateway_sound and input_slider.loop, but you need to test it yourself.

1 Like

Thanks! It sorta works now, during pending is it a normal behavior that alarm starts to play?

I don’t know exactly how you have your automations set up, but if you’re using mine, it will play sound while it is pending and when it is triggered.

Copy pasted yours now, and simple replaced sensor and it is alarming when pending.
Do you use this code?

Yes I do, but I am not sure that my full alarm code is posted, as I remember that the purpose of the example was just to show that ringtone loop was somehow possible.

I can check it later and share, if you’re interested.

As I mentioned, it should play a sound while it is pending and then another if it is armed, just like a regular home alarm.

When you arm it, while it is not armed (=pending), it should play a sound for me to know I still have time to leave before it is armed.

When alarm is triggered, another alarm sound is to be played (isn’t that the purpose of an alarm?).

Nice! so now you can Arm/ DisArm the arm via HA?