Alarm Automation - Service Template Help Needed :)

Hi am trying to make a Amazon Dash Button arm and disarm the alarm over MQTT.

I have the Dash Button working over MQTT, and can get the Alarm to disarm…

- alias: 'Alarm Disarm Using Amazon Dash DadsRootBear'
  initial_state: 'on'
  trigger:
  - platform: state
    entity_id: switch.dadsrootbeer_dash_switch
    to: 'on'
  action:
    service: alarm_control_panel.alarm_disarm
    data:  
      entity_id: alarm_control_panel.house
      code: 9999

however I have 2 issues i cant get working or dont know where to start.

Issue 1

I cannot make the code work for Armed_Away

- alias: 'Alarm Disarm Using Amazon Dash Olay''
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: switch.olay_dash_switch
    to: 'on'
  action:
    service: alarm_control_panel.alarm_armed_away
    entity_id: alarm_control_panel.house

Issue 2

How can I make both automations work using the same dash, for example, if the alarm is disarmed then the dash button arms the alarm, if the alarm is armed the dash button disarms the alarm ?

Any help would be most welcome, thanks

Use a template to determine which service to call:

Great thanks @treno, so the template condition would work like this, first time doing this

{% if is_state(‘alarm_control_panel.house’, ‘disarmed’) %}

Also any one know why this would error / not work ?

- alias: 'Alarm Disarm Using Amazon Dash Olay''
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: switch.olay_dash_switch
    to: 'on'
  action:
    service: alarm_control_panel.alarm_armed_away
    entity_id: alarm_control_panel.house

Not sure what alarm system you use. I use dasher along with SimpliSafe and have this in the config.json, works great for me.

{
“name”: “Amazon Dash 2”,
“address”: “mac id”,
“interface”: “eth0”,
“url”: “http://0.0.0.0:8123/api/events/toggle_simplisafe_alarm”,
“method”: “POST”,
“headers”: {“x-ha-access”: “blahblah!”},
“body”: “{“entity_id”: “alarm_control_panel.alarm_arm_home”}”
}

HI

I have tried creating the Service Call / Template, but I dont think I have understood it… First Time. Can any one help with the syntax

- alias: 'Alarm Arm and Disarm Using Amazon Dash Olay'
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: switch.olay_dash_switch
    to: 'on'
  action:
    service: alarm_control_panel.house
    data_template:
      entity_id: >
  	{% if is_state(‘alarm_control_panel.house’, ‘disarmed’) %}
      service: alarm_control_panel.alarm_disarm
      data:  
        entity_id: alarm_control_panel.house
        code: 9999
    {% else %}
       service: alarm_control_panel.alarm_arm_away
       entity_id: alarm_control_panel.house
    {% endif %}

Thanks

Any advice to help me, as I am totally stuck …

This will get you close. I can’t guarantee that it works, but it does at least pass config check…

- alias: 'Alarm Arm and Disarm Using Amazon Dash Olay'
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: switch.olay_dash_switch
    to: 'on'
  action:
    service_template: >
     {% if is_state('alarm_control_panel.house', 'disarmed') %}
       alarm_control_panel.alarm_disarm
     {% else %}
        alarm_control_panel.alarm_arm_away
     {% endif %}
    data_template:
      entity_id:  alarm_control_panel.house
      data: >
       {% if is_state('alarm_control_panel.house', 'disarmed') %}
         code: 9999
       {% endif %}

@treno Thanks so much for the help, I have tried the code, and i get the following error

2017-12-01 20:19:36 ERROR (MainThread) [homeassistant.core] Invalid service data for alarm_control_panel.alarm_disarm: extra keys not allowed @ data[‘data’]. Got ‘code: 9999’