About state of sleeping entities

I just switched from deconz/phoscon to ZHA and one difference in particular hit me, both as food for thought and as a need to reconfigure my automations. The latter I’ve not yet found a satisfying solution to.

Using deconz my battery driven motion sensors had two states, binary, on or off, true/false.
Using ZHA they have three - on, off or unavailable.

Using deconz the battery sensor/attribute showed last known state.
Using ZHA the battery sensor is 90% in state unavailable.

This affected me in two primary ways - in lovelace using nothing but a glance-card and show_last_changed: true attribute I was able to see time since last actual movement in a certain room or rather for me more interestingly the period of no movement. Now with this same config the actual scenario is:

08:00 motion detected
08:05 no motion
11:00 device unavailable

If I then open HA at 11:30 it tells me last change was 30 minutes ago, which technically is true (off -> unavailable) but what I’m interested in is the span of 08:05 -> 11.30.

Secondly I have automations/alerts to warn me when batteries go low or bad/empty. This was an attempt at an amateur DIY home security system. And it worked quite well but now with ZHA I can’t differentiate a sleeping entity from one who’s batteries stopped working.

I realize I could dabble with template sensors to somehow emulate the deconz behavior but I’ve got so many entities it’d be a real chore to both implement and more importantly maintain.

I’m interested in knowing how other people in similar predicaments solves it and also if possible what the intended way is.

What I’ve tried to look for and was hoping to find is either a way to differentiate the state ‘unavailable’ as either sleeping or … hmm… not responding (?). Maybe that’s technically not feasible but I hope you get the gist of what I’m trying to convey. Secondly I was hoping to find some kind of “last_known_state” method. Maybe I’m stuck in my way of thinking, trying to force the ZHA-implementation to work in a way it wasn’t intended to. I understand why devices show up as ‘unavailable’ since they practically are unavailable to HA. But since they still work - as in, if the sensor is triggered it wakes up and sends an update, they, for me as a person, aren’t actually ‘unavailable’.

I’m not quite sure that’t the way it should be. Maybe something in your sensor’s configuration. Unavailable means there is no communication, no state updates etc…

exactly. so I’d say it’s a config error a a bug in ZHA integration.
But it’s just my thought, have no idea what ZHA is :wink:

Same issue here.
Ikea Tradfri Motion Sensor with ZHA integration is showing “Unavailable”. In the meantime my other Xiaomi Motion Sensors are just reporting “No Motion” as they should.

Sounds like it’s the sensor thats causing the confusion…?

I have this issue of unavailable Tradfri motion sensors and last night completely by accident I ran into a setting that may be the cause of this. Under the ZHA dashboard ( http://homeassistant.local:8123/config/zha/dashboard ) there are two settings called “Consider [battery|mains] powered devices unavailable after (seconds).” It defaults to 6 hours for battery which makes sense why my motion sensor would go unavailable over night. After setting it to 86400 (24h), the motion sensor works as I expect.

The downside is that if you have a sensor that is not trigger in a 24 hour period it will still go unavailable. I haven’t tried to test for the value’s upper limit. I tested it with a zero value and that doesn’t disable the setting. I’d really rather disable this setting.

Since I am new to HA and ZHA I am not certain how to file a bug report.

HASS uses a bit different logic.
If there is no communication with device HASS considers it unavailable, and this makes perfect sense to me.

I had a same problem and my workaround was simple, create sensor in configuration.yaml and use if else logic to set its value from original sensor.
Then simply use your new sensor in automations.

template:
  - sensor:
      - name: Robot needs cleaning
        unique_id: sensor.robot_needs_cleaning
        state: >
          {% if is_state('switch.mqtt_switch', 'on') %}
            yes
          {% else %}
            no
          {% endif %}
  - binary_sensor:
      - name: TV Dnevni Status
        device_class: running
        state: >
          {% if is_state('remote.55oled706_12_remote', 'on') %}
            on
          {% else %}
            off
          {% endif %}
  - binary_sensor:
      - name: Dnevni Motion Detected
        device_class: occupancy
        state: >
          {% if is_state('binary_sensor.0x00158d00083a5cdf_occupancy', 'on') %}
            on
          {% else %}
            off
          {% endif %}