Basic Home Alarm Setup with Zipato Keypad

Going back to the syncing issue discussed by @soul and @skptic.

I am the original author of the SmartThings device handler for the Zipato keypad, which @soul and @skptic referenced earlier. I recently migrated to Home Assistant, and had to solve the syncing issue (i.e., not being able to disarm the Manual Alarm using the keypad if it had been armed via the app, because the state of the keypad is already ‘home’, and vice versa). I want to contribute my solution here, based on what worked in SmartThings and the work done by other contributors above. I think it was pretty straight-forward in the end, but it took me a while to understand the problem. If you just want my solution, jump to the end. The following paragraphs explain a bit about how this device works and my thought process.

@skptic mentioned that he didn’t feel like dealing with 500 lines of Groovy from the SmartThings device handler. Neither did I, and I wrote that Groovy code! Groovy is horrible, but I took one for the team :slight_smile: and read back over the my Zipato SmartThings code, and also re-read the keypad’s Z-Wave manual.

The first thing to point out is that the keypad hardware does not know whether it is in ‘home’ or ‘away’ mode. It does not have a corresponding internal state that we need to try to sync with the armed_away / disarmed states of Home Assistant’s Manual Alarm integration, and therefore we don’t need to worry about waking the device up to try to sync it with HA. The ‘home’ / ‘away’ state exists in HA only, and is represented by the value of the “access control” entity associated with the device in HA, which is either 6 for ‘home’ or 5 for ‘away’ (which corresponds to the values emitted by the keypad in the Z-Wave ALARM_REPORT_V2 command).
I note that there is also an “alarm level” entity that is identical to the “access control” entity, but gets the value of either 0 or 255 instead of 5 or 6.

The problem is that if the value of the “access control” entity in HA is already ‘6’ when you tap your keyfob and press ‘home’ on the keypad, then it won’t get updated in HA, and it’s impossible to trigger an automation to arm or disarm the alarm.

The Zipato manual is a bit dense, but its normal operation is quite simple: every time someone presses the ‘home’ or ‘away’ button and taps a registered RFID tag (or enters a registered user code), then it will wake up and send a ALARM_REPORT_V2 Z-Wave command on the Z-Wave network that contains:

  1. an ‘event’ value of 6 if the ‘home’ button was pressed or 5 if the ‘away’ button was pressed
  2. a number representing which registered user code or RFID was used

That’s the end of the zipato’s job. The rest is up to us. In my original SmartThing device handler I dealt with the “alarmv2.AlarmReport” z-wave command (in around line 454) by firing off the SmartThings “switch” event with the value of “on” or “off” depending on the contents of the z-wave command. The SmartThings device handler was declared with the “switch” capability, which meant that SmartThings would see the Zipato as a switch, and it would turn on or off when I fired off those events. In addition, any automation could control the state of that switch, e.g., turning it on if the user armed the alarm remotely (remember that this switch is virtual and internal to SmartThings, and doesn’t requite the keypad to wake up or anything). I think this is why the syncing issue didn’t exist in SmartThings.

I think the key difference in Home Assistant is the reduced level of access to the z-wave commands that are received by the z-wave controller. With SmartThings, a community-provided device handler could receive the raw z-wave commands, and do something useful like set corresponding state in a virtual switch, against which 2-way automations could be configured.

In contrast, in Home Assistant, we don’t seem to have this level of access. I can see the ALARM_REPORT_V2 commands zipping by in the z-wave log, but I can’t find a way to respond in an automation or in code. According to the HA Z-wave docs, the closest event that can be listened for is the zwave.node_event, which is only works for the Z-Wave BASIC_V2 command (i.e., not for ALARM_REPORT_V2). In any case, I can’t seem to find any specimens of BASIC_V2 commands on my z-wave network, and I notice some community members think zwave.node_event might currently be broken (in this post).

The key workaround that @skptic identified above is to modify the value of the access_control / alarm_level entities when the state of the manual alarm is changed (his v1 uses a python script, and v2 uses the API from a shell command). This means that the next time a Z-wave ALARM_REPORT_V2 command is sent by the keypad, HA would actually change the value of the entity, which would cause HA events to fire and automations to trigger.

In other words, although we can’t get access to the ALARM_REPORT_V2 Z-wave command, we can make sure HA processes it internally, generating appropriate state change events to trigger our automations.

My solution

Instead of using a bespoke python script or shell commands, I installed the generic Python set_state script by @rodpayne discussed in this post. I then added the automations pasted below, which trigger when my alarm is armed via the app, and sets the zipato’s “access control” entity appropriately. And vice versa when the alarm is disarmed from the app. This fixes the problem, and I can now arm the alarm via the app and immediately disarm via the keypad, plus every other permutation.

- alias: 'Alarm: Arming updates RFID keypad'
  description: 'Update value of RFID keypad entity if alarm is armed via the app,
    to workaround the problem of disarming via the keypad not updating entity state
    and therefore not triggering any automations. '
  trigger:
  - platform: state
    entity_id: alarm_control_panel.home_alarm
    to: armed_away
  condition:
  - condition: state
    entity_id: sensor.rfid_alarm_keypad_access_control
    state: '6'
  action:
  - service: python_script.set_state
    data:
      entity_id: sensor.rfid_alarm_keypad_access_control
      state: 5

- alias: 'Alarm: Disarming updates RFID keypad'
  description: 'Update value of RFID keypad entity if alarm is disarmed via the app,
    to workaround the problem of arming via the keypad not updating entity state and
    therefore not triggering any automations. '
  trigger:
  - platform: state
    entity_id: alarm_control_panel.home_alarm
    to: disarmed
  condition:
  - condition: state
    entity_id: sensor.rfid_alarm_keypad_access_control
    state: '5'
  action:
  - service: python_script.set_state
    data:
      entity_id: sensor.rfid_alarm_keypad_access_control
      state: 6