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
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
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
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!
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