History Stats help with switch Zigbee Unavailable

Hi There, Im trying to work around when a Zigbee switch becomes Unavailable so I can check that in an automation. I have the history_stats entity setup as follows:

  - platform: history_stats
    name: 'Upstairs Switch Unavailable Last 5 Mins'
    entity_id: switch.upstairs_switch_switch
    state: "unavailable"
    type: count
    duration: "00:05:00"
    end: "{{ now() }}"

But the history stats entity only shows above 0 when the switch becomes available again, I had thought it would update the count when the switch becomes unavailable. Am I not understanding something here?

That’s just how they work… it will stay 1 until 5 minutes after the entity becomes available again.

Do you need a sensor for multiple applications? If this is more of a 1-off thing, just use a State trigger with a duration. For a sensor, you could use a Template binary sensor with a 5 minute delay_on.

But bear in mind that the state of a Zigbee entity may initially be “unknown” for some time after a restart, and may not change to “unavailable” until some time after it has stopped responding.

How long depends on whether it is battery or mains powered, on which entity in a device you’re monitoring and on the network settings in whatever integration you are using. In ZHA by defailt it’s six hours before a battery-powered device as a whole is declared MIA.

OK thanks for the input here. Maybe I should step back bit…

I have these push button Schneider Electric mains powered Zigbee wall switches. They were chosen because they fit in with the design of all of our wall switches and are Zigbee! Toggling on/off they work really well, fast response etc etc. However, if they become unavailable for any reason, when they become available again they trigger an off state. We are using the switches as a “override”, e.g. standard light automations work nicely, but if you want it bright in the room, hit the switch. It creates a scene with the current lighting, turns on lights how we want them, turn off, the scene is returned to the previous lighting and the scene is deleted.

Actually I have been overthinking it. Maybe I should just use a simple toggle helper, set that to on and off when the switch is on/off. Then use that in the automation. E.g. if the toggle is off, and the switch is setting off, then dont do anything? Or maybe test for the scene to see if it’s there?

Post the automation you’re using for those switches here. It sounds like you don’t need a toggle helper - you simply need to either exclude unknown/unavailable states from the automation.

Something like this (stolen from @francisp’s post in a similar thread) should fix your issue:

condition:
  - condition: template
    value_template: "{{ trigger.from_state.state not in ['unknown', 'unavailable'] }}"

This seems like a classic case of an XY problem - don’t ask what to do to fix the way you think an issue should be solved. Tell people what your actual issue is, since there’s probably a more efficient way of solving it.
Or, in other words, fix the cause, not the symptom.