One click to enable and disable alarm

Hi everyone,

I am kind of new with Home Assistant. I managed to created 2 pieces of automation with my Xiaomi wireless switch. A single click enables my alarm, double click disables the alarm. It all works fine. However I want to use only the single click. The basic flow is:

  • Single click
  • If alarm is enabled disable it.
  • Single click
  • If alarm is disabled enable it.

I thought of different ways to go about but settle on an if else kind of setup with the last bit changing a boolean (not sure if it is the best approach, suggestions welcome)

This is what I got so far:

My automations.yaml:

- alias: enable disable alarm
  trigger:
    platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0002107de0
      click_type: single
  action:
    service_template: >
      {% if states.input_boolean.turn_alarm == "off" %}
        action:
        - data:
            message: The alarm has been enabled. Have a nice day!.
          entity_id: media_player.dining_room_speaker
          service: tts.google_say
        - data:
            entity_id: automation.balcony
           service: automation.turn_on
      {% else %}
         action:
         - data:
             message: The alarm has been disabled. Welcome home!
           entity_id: media_player.dining_room_speaker
           service: tts.google_say
         - data:
             entity_id: automation.balcony
         - service: automation.turn_off
         - service: input_boolean.toggle
             entity_id: input_boolean.turn_alarm
      {% endif %}

My boolean setup in configuration.yaml

input_boolean:
  turn_alarm:
    name: Turn Alarm
    initial: off

And this is the error that comes up in the logs:

2018-09-04 19:21:35 INFO (MainThread) [homeassistant.core] Bus:Handling <Event click[L]: entity_id=binary_sensor.switch_158d0002107de0, click_type=single>
2018-09-04 19:21:35 INFO (MainThread) [homeassistant.components.automation] Executing enable disable alarm
2018-09-04 19:21:35 INFO (MainThread) [homeassistant.core] Bus:Handling <Event logbook_entry[L]: name=enable disable alarm, message=has been triggered, domain=automation, entity_id=automation.enable_disable_alarm>
2018-09-04 19:21:35 INFO (MainThread) [homeassistant.helpers.script] Script enable disable alarm: Running script
2018-09-04 19:21:35 INFO (MainThread) [homeassistant.helpers.script] Script enable disable alarm: Executing step call service
2018-09-04 19:21:35 ERROR (MainThread) [homeassistant.helpers.service] Template rendered invalid service: action:
   - data:
       message: The alarm has been disabled. Welcome home!
     entity_id: media_player.dining_room_speaker
     service: tts.google_say
   - data:
       entity_id: automation.balcony
   - service: automation.turn_off
   - service: input_boolean.toggle
       entity_id: input_boolean.turn_alarm
2018-09-04 19:21:35 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=automation.enable_disable_alarm, old_state=<state automation.enable_disable_alarm=on; last_triggered=None, friendly_name=enable disable alarm @ 2018-09-04T19:21:22.957309+01:00>, new_state=<state automation.enable_disable_alarm=on; last_triggered=2018-09-04T19:21:35.560757+01:00, friendly_name=enable disable alarm @ 2018-09-04T19:21:22.957309+01:00>>

The bit inside the if block is exactly what I had on my automation that was working (single click and double click)

I never used templates, so I am not even sure what I trying to achieve is possible. Any ideas would be highly appreciated.

Raf

Hey Guys,

I actually managed to come up with difference solution. 2 automations which at the end change a boolean which my automations base on what to do. This is what they look like now:

- alias: disable balcony alarm
  trigger:
    platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0002107de0
      click_type: single
  condition:
    - condition: state
      entity_id: input_boolean.turn_alarm
      state: 'off'
  action:
  - data:
      message: The alarm has been disabled. Welcome home!
    entity_id: media_player.dining_room_speaker
    service: tts.google_say
  - data:
      entity_id: automation.balcony
    service: automation.turn_off
    service: input_boolean.toggle
    data:
      entity_id: input_boolean.turn_alarm

- alias: enable balcony alarm 2
  trigger:
    platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0002107de0
      click_type: single
  condition:
    - condition: state
      entity_id: input_boolean.turn_alarm
      state: 'off'
  action:
  - data:
      message: The alarm has been enabled. Have a nice day!
    entity_id: media_player.dining_room_speaker
    service: tts.google_say
  - data:
      entity_id: automation.balcony
    service: automation.turn_on
    service: input_boolean.toggle
    data:
      entity_id: input_boolean.turn_alarm

Thanks anyway!!

Sorry man, must have missed this post yesterday. Anyways, here is a single automation solution (To be used with your input boolean):

- alias: Balcony Alarm
  trigger:
    platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0002107de0
      click_type: single
  action:
    - service: tts.google_say
      entity_id: media_player.dining_room_speaker
      data_template: >
        {% if is_state('input_boolean.turn_alarm','on') %}
          The alarm has been disabled. Welcome home!
        {% else %}
          The alarm has been enabled. Have a nice day!
        {% endif %}
    - service_template: >
        {% if is_state('input_boolean.turn_alarm','on') %}
          automation.turn_off
        {% else %}
          automation.turn_on
        {% endif %}
      entity_id: automation.balcony
    - service: input_boolean.toggle
      entity_id: input_boolean.turn_alarm

Hmm, I missed it, too. :slight_smile:

If the “alarm” is simply the balcony automation, and it’s “armed” when that automation is on, and it’s “disarmed” when that automation is off, then I don’t think you need the input_boolean. You could just do it this way (taking from what @petro provided):

- alias: Balcony Alarm
  trigger:
    platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0002107de0
      click_type: single
  action:
    - service: tts.google_say
      entity_id: media_player.dining_room_speaker
      data_template: >
        {% if is_state('automation.balcony','on') %}
          The alarm has been disabled. Welcome home!
        {% else %}
          The alarm has been enabled. Have a nice day!
        {% endif %}
    - service: automation.toggle
      entity_id: automation.balcony
1 Like

Hey @petro and @pnbruckner,

Thanks for the suggestions. I learned about the toggle service, it makes the automation a lot simpler indeed. I have used for my wireless switch to turn the lights on and off. Much appreciated guys. I will give a go tonight!

Cheers,
Massaox

Hey @pnbruckner and @petro,

Thanks for your help. I tried both solutions and it did not work, I got the following error:

Configuration invalidCHECK CONFIG

Invalid config for [automation]: expected a dictionary for dictionary value @ data['action'][0]['data_template']. Got None. (See /hass/config/configuration.yaml, line 140). Please check the docs at https://home-assistant.io/components/automation/

Did I bit of research and notice the message for my google home had to be passed with the “message” directive so I changed to the follow and it worked perfect:

- alias: Enable/Disable Alarm
  trigger:
    platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0002107de0
      click_type: single
  action:
    - service: tts.google_say
      entity_id: media_player.dining_room_speaker
      data_template:
        message: >
          {% if is_state('automation.balcony','on') %}
             The alarm has been disabled. Welcome home!
          {% else %}
             The alarm has been enabled. Have a nice day!
          {% endif %}
    - service: automation.toggle
      entity_id: automation.balcony

All good now! Thanks!

Cheers,
MassaoX

2 Likes