Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
Mqtt binary sensor expire after #26058
First PR, so go easy on me. Not quite sure about the tasks in the below template that I haven't checked/ticked yet. Can someone please have a look and let me know what else I need to do?
Added expire_after functionality to mqtt binary_sensor. Basically copied the functionality from the standard mqtt sensor, except that we se availability, instead of just clearing out the state.
Example entry for Zigbee2MQTT
I have a Xiaomi PIR, and a Xiaomi door open/close sensor. These are paired to a CC2530/ESP8266 which is linked to a zigbee2mqtt instance.
Each of these sensors have a heartbeat (I believe it's every 51 minutes) which sends a 'I'm here' back to the mqtt (and thus Home Assistant).
My problem was that if I lose the link between CC2530/ESP8266 and zigbee2mqtt, Home Assistant keeps displaying the same state that it has been, e.g. no motion, or door closed.
The workaround for this on the zigbee2mqtt website is to send an
The problem I faced is this attribute/setting is only available on the mqtt sensor, and not on the mqtt binary_sensor. That is what this implementation is doing.
So the way you'd use this is, you would set the timeout to say 110 minutes (two hearbeat cycles). If Home Assistant hasn't heard anything in that time, the state of the sensor is set to 'unavailable' which would show up on your dashboard, and you can then do something about it.
Without this, it will always just show 'no motion' and the only way you'd find out that something is funky, is to walk up to the PIR, and realise, oh, it's not triggering.
I don't believe the off_delay you mentioned will help, because it will still show a state of 'off' which means 'no motion' or 'door closed'. We need a third state which attacts attention.
This is what my PR is implementing:
If these is another way to handle this, without this PR, I'd also be quite happy, but this seems to be pretty simple and means people can easily set this up without much faffing around.
Here is the original feature request for this too:
Thanks for taking a look
If I could have my word, I'd rather use automation for such simple things as disconnect instead of adding something like expire to binary sensor...
Thanks for the ideas. I can try those out. Can you let me know how you set up that automation. I'm a little confused by that part. What are the trigger/conditions/action parts?
As for the functionality, I thought it is a very clean way of doing it, since the MQTT sensor already supports this, and the zigbee2mqtt program also supports this. It bring consistency, and a very clean way of setting this up. I think it makes things much simpler, especially for newcomers.
Maybe use 2 automations.
You might use as well Binary sensor template.
If you need more help ping me on community forum or contact on discord as issues and PR are not so much good place for help
@KiLLeRRaT OK, your explanation for the functionality makes sense.
For the CI errors, it's due to failing MQTT test cases after your changes.
Instead of waiting for CI, you can run the tests locally, follow instructions here:
EDIT: Ignore this post, FINALLY got it working after getting latest dev, installing all the py requirements again, and comitting. Phew.
Hmm sorted my functionality/tests but an unrelated test is failing.
Anyone know about:
I guess I will try to update my branch from the latest to see if I can get it going.
This use case would be to specify an availability topic (even though it won't be used) to get the
If we have the
@KiLLeRRaT Yeah, this is better! Still a few additional things:
Edit: Sorry for the slow feedback, please feel free to ping me on Discord instead
Hey @emontnemery ,
I have tried what you suggested, but it seems like there is something fishy with the available property you suggested. I think there is possibly something else interfering. To test this, I reverted all my changes out, then I run the tests, everything passed. I then simply add in a super simple wrapper for the available property you gave me, less the expired stuff:
I'd expect all the tests to still pass, but now they fail.... To me that is saying that there is another available property somewhere causing issues?
You can test this by grabbing any of the latest branches (without anything I did).
They all pass.
Then add the below to
Now the tests fail.
Any ideas or pointers?
Sorry to make this so painful for you, it's the first time I've ever touched any Python, so I'm figuring it out as I'm going.
Excellent! Thanks for the help @emontnemery , I think I got it this time around :)
Let me know if there is anything else to be done.