How to create custom service and call from button

I have two separate buttons that arm and disarm my alarm control. I am wondering if someone can point me in the right direction on how to do that create one button that will toggle the alarms state. Below is the code for my button that arms my alarm. How can I create a custom service that based on the state of my alarm, toggle it to either armed_home or disarmed? I have never created a script or a service and don’t even know where it would go, or what it would look like.

Thanks,

type: 'custom:button-card'
entity: alarm_control_panel.home_alarm
state:
  - value: disarmed
    icon: 'mdi:shield-home'
    color: red
    name: Alarm off
  - value: armed_away
    icon: 'mdi:shield-home'
    color: green
    name: Alarm On
tap_action:
  action: call-service
  service: alarm_control_panel.alarm_arm_home
  service_data:
    entity_id: alarm_control_panel.home_alarm
    code: 7777

hi there… to create a script for this, we need to goto configuration>script>add new script.
for now i have created a script for you, it should work but if not we may need make some changes to it.

alias: Alarm Toggle
sequence:
  - choose:
      - conditions:
          - condition: state
            entity_id: alarm_control_panel.home_alarm
            state: disarmed
        sequence:
          - service: alarm_control_panel.alarm_arm_home
            data:
              code: '7777'
              entity_id: alarm_control_panel.home_alarm
      - conditions:
          - condition: state
            entity_id: alarm_control_panel.home_alarm
            state: armed_home
        sequence:
          - service: alarm_control_panel.alarm_arm_away
            data:
              code: '7777'
              entity_id: alarm_control_panel.home_alarm
    default: []
mode: single

Please see the below gif to see how to copy this to script.
screen-capture
Now you can call this cript from your buttons card. The config should look like this.

type: 'custom:button-card'
entity: alarm_control_panel.home_alarm
state:
  - value: disarmed
    icon: 'mdi:shield-home'
    color: red
    name: Alarm off
  - value: armed_away
    icon: 'mdi:shield-home'
    color: green
    name: Alarm On
tap_action:
  action: call-service
  service: script.alarm_toggle

If anything do let us know.

1 Like

Thank you Very much, this was very helpful. I was able to get the script created with a bit of tinkering, and it works flawlessly as expected when I execute the script manually. However, when I call the script from my button I get an error that states “Failed to call service script/alarm_toggle. Service not found.” . I was a bit confused about the name of the script(Alarm Toggle) and why the call of the script was alarm_toggle. They don’t match, and I tried to tinker with them but no help at all.

Bryan

Script Code

alias: Alarm_Toggle
sequence:
  - choose:
      - conditions:
          - condition: state
            entity_id: alarm_control_panel.home_alarm
            state: disarmed
        sequence:
          - service: alarm_control_panel.alarm_arm_home
            data:
              code: '9164'
              entity_id: alarm_control_panel.home_alarm
      - conditions:
          - condition: state
            entity_id: alarm_control_panel.home_alarm
            state: armed_home
        sequence:
          - service: alarm_control_panel.alarm_disarm
            data:
              code: '9164'
              entity_id: alarm_control_panel.home_alarm
    default: []
mode: single

Button Code

type: 'custom:button-card'
entity: alarm_control_panel.home_alarm
state:
  - value: disarmed
    icon: 'mdi:shield-home'
    color: red
    name: Alarm off
  - value: armed_away
    icon: 'mdi:shield-home'
    color: green
    name: Alarm On
tap_action:
  action: call-service
  service: script.alarm_toggle
type: 'custom:button-card'
entity: alarm_control_panel.home_alarm
state:
  - value: disarmed
    icon: 'mdi:shield-home'
    color: red
    name: Alarm off
  - value: armed_away
    icon: 'mdi:shield-home'
    color: green
    name: Alarm On
tap_action:
  action: call-service
  service: script.turn_on
  target:
    entity_id: script.toggle_alarm

Give that a try

1 Like

Thanks, that changed brings up this error.
“Failed to call service script/turn_on. sequence item 0: expected str instance, Optional found”

Try swapping target: for data:

Thanks, that produces the same error

“Failed to call service script/turn_on. sequence item 0: expected str instance, Optional found”

Go to developer options and under states tab check if the script.alarm_toggle is available or not. Maybe the script has a different name.

It shows up as a numbered script. When I execute this service, it arms and disarms my alarm system just like iut should. Sadly though, I changed the script name in the button from and I still get the following message. I thought this would have been easy to do, you know calling a script from a button, had to have been done a thousand times. Maybe there us an alternative solution? I basically am looking to build ONE button that will arm/disarm my alarm_control_panel system.

“Failed to call service script/161728095085. Service not found.”

Service Script

Try this.

type: 'custom:button-card'
entity: alarm_control_panel.home_alarm
state:
  - value: disarmed
    icon: 'mdi:shield-home'
    color: red
    name: Alarm off
  - value: armed_away
    icon: 'mdi:shield-home'
    color: green
    name: Alarm On
tap_action:
  action: call-service
  service: script.161728095085

Thanks, but that change produces the same error, where it cannot find the service. I have tried many different formats, without success. Seems like this would be simple.

I have tried it with my system and it works fine. Do one thing, Please delete the script.161728095085 from your HA and create a new one like that and try. Also please remove the underscore from the alias and use it like this.

alias: Alarm Toggle
2 Likes

OMG! You are a lifesaver, it is working after deleting and re-adding the script! I am not even going to look back, that script must have been corrupt or quite possibly it was the alias name causing the issues. Either way, it is working now and I thank you very much.

1 Like

Just want to chime in here that this process got me to the result I was looking for was well. I have a hi-fi system that does not have remote power control. I bought some smart power strips to enable me to power on the system rather than having to reach around to the back of each piece. The sequence is important as well as sources power on when the amps are running can trigger some popping through the speakers. So, I was able to write 2 scripts one with the power on sequence, another with the power off. Then I added a button to call each on my dashboard. This worked but looked terrible.

I used this process to combine the 2 scripts with the ‘choose’ condition and the state of one of the outlets triggering either the on or off sequence. The custom button card works perfectly , changes from looking like a power on icon when the system is off to a power off icon when the system is on.

Thanks so much and I really gained some deeper understanding about how all of this stuff works. I cranked up some happy tunes starting with powering on my system with my new button thanks to you.

1 Like