Best practices for Zwave Door Sensors & their state

Since HA doesn’t yet maintain states across restarts, it assumes that my Zwave door sensor is open when it restarts. I assume that it’ll eventually poll and figure out that the sensor is closed.

But all of my “door is open” actions are triggering each time I restart HA.

Is there a way to debounce these? I imagine that there’s a way I can test for how recently HA started - should I use that to filter actions on startup? But then what happens when (if?) the polling happens later and I’d get another false trigger?

How are y’all handling this?

I’ve not had the greatest luck myself with triggering on zwave device states either, so I’ve been looking into triggering off of template sensors that I’ve created where I publish the state in MQTT and then using the MQTT state to trigger automation. I’m not really quite there yet but it does seem like it could be a decent solution.

If there is anyone out there doing anything differently I too would be interested in knowing.

1 Like

Until the zwave.network_ready event is fired, Home Assistant can’t actually communicate with the devices, so, for your automations, you could try checking if the sensor is accessible before running any actions:

condition:
  condition: template
  value_template: '{{ states.sensor.ecolink_doorwindow_sensor_sourcenodeid_28.state }}'

That should result in a false, non-passing condition if the zwave network is not yet ready, just make sure you are checking against the physical device sensor and not a template sensor.

Of course I have not tested this myself, but as I use something similar when checking battery states of all of my sensors, it correctly returns false when the zwave network is not ready. I use this to prevent errors in my startup when it can not access the value of a non-existant device.

Here is the code I use to check for battery state:

batt_kitchen_window:
  value_template: >-
    {%- if states.sensor.ecolink_doorwindow_sensor_sourcenodeid_28.state -%}
      {{ states.sensor.ecolink_doorwindow_sensor_sourcenodeid_28.attributes.battery_level }}
    {%- else -%}
      --
    {% endif %}
  friendly_name: 'Kitchen Window'
  unit_of_measurement: '%'

With this, at startup, it displays a double dash (-- %) instead of the battery level, until everything is booted up.

Or, it may be as simple as checking both a from and to in the trigger state, so:

trigger:
  platform: state
  entity_id: sensor.living_room_window
  from: "off"
  to: "on"

or if you are using a template sensor

  from: "Closed"
  to: "Open"

instead of checking just a specific state.

That may be just enough to not fire the actions at startup, because Home Assistant may be setting it to “on” or “Open” directly, therefore there would not be a “from” state, and hopefully that will stop it from firing the action, but again, you’d have to test it.

5 Likes

Thanks. This is very helpful.

I noticed yesterday that the state of my door sensor never updated on its own. I don’t know if that’s a function of it being a battery-powered device or if I need to set up Zwave polling on my own.

It’d be nice if I could customize the state of this device like you can a switch:

assumed_state: false

Having HA assume that they’re all tripped is getting more annoying the more battery-powered Zwave sensors I move to HA.

I’m solving this by adding a condition to my automation rules that ignores events from battery powered sensors unless i’ve already seen the zwave.network_ready event.

This effectively ignores “on” events that are created during bootup.

Note the “zwave_up” boolean:

input_boolean:
  got_mail:
    name: Post arrived today
    initial: 'off'
    icon: mdi:email-open
  zwave_up:
    name: ZWave network is ready
    initial: 'off'

…and an automation rule to set it:

automation:
  - alias: Mark zwave as up
    trigger:
      platform: event
      event_type: zwave.network_ready
    action:
      service: homeassistant.turn_on
      entity_id:
        - input_boolean.zwave_up

My real automation rule that does stuff when my sensor turns on has a condition, so it doesn’t fire before zwave_up:

automation:
  - alias: Detect new mail
    trigger:
      platform: state
      entity_id: binary_sensor.mailbox_sensor_19_0
      to: 'on'
    condition:
      condition: state
      entity_id: input_boolean.zwave_up
      state: 'on'
    action:
      service: homeassistant.turn_on
      entity_id: input_boolean.got_mail

This does the job for me. Not sure if there’s a better way. Let me know :slight_smile:

2 Likes