Ring Alarm - MQTT Discovery Alarm integration

@acolytec3 i sometimes noticed that behavior as well when initially loading the script. instances where the sensors would come online but the alarm panel would show up unknown. killing and reloading the script again resolved it for me.

I too have to stop and start the script 2 times everytime ha restarts. anyone found a way to fix that? or know how to create an automation to do it?

This is a great time saver as I was looking to write something similar. I’m having the same issue with having to restart the script multiple times before it works. It seems like a timing issue with how the HA MQTT discovery option works and I’m still trying to wrap my head around it. I believe it may have something to do with the timing and perhaps the fact that message aren’t set to be retained.

Looking at some of the other projects that use this MQTT discovery, such as zigbee2mqtt, they appear to use a combination of message retain and a combination of birth/last will to no when HASS restarts and republish state. Some even seem to simply introduce an arbitrary delay between their MQTT connect and publishing of state.

My initial reaction is to just disable MQTT discovery, and manually configure the MQTT sensors, since I’ve overall had inconsistent results with auto discovery anyway. But, I’m also interested in wrapping my head around how this MQTT discovery stuff works in HA, so, if I can find the time, I’ll dig into it a little.

Regardless, great work getting this far, outside of this simple discovery issue, which I hacked around for now, everything with this works great and I’ve already created some simple automations for turning on lights when it’s dark and I open the front/back doors, etc and to automatically disarm the alarm in the morning before we leave. Good stuff!

@tsightler I’ve tried a few different methods to get the availability topic to stick in initial start up and no luck so far. I may just get rid of the availability parameter and see if the values stick during the initial discovery/setup process. More to come.

I did a very crude hack to modify every single MQTT publish with {retain: true, qos: 2} and, predictably, that solved this issue of the objects not registering during boot or initial start of HA or the service, but of course it led to the case where the items are discovered even if the service is not running. Still, the goal was to help me understand the discovery process and it’s slowly coming together.

Some of the other tools that support homeassistant discovery use a birth/last will published to hass/state to determine when to republish objects rather than publishing all objects as retained. This seems to be what zigbee2mqtt does.

I’m seeing lots of issues where all devices report intermittent offline status, for example during arming/disarming, etc. or even when idle (perhaps losing connection to the Ring API for a short period). As is probably obvious, I’m a complete newbie to NodeJS, and even newer to HA, so it’s a learning curve for me, but I’m starting to make sense of it all.

@tsightler I’ll have to look at the zigbee2mqtt code and see how they handle it. I’m not super knowledgeable on all the various ways MQTT messages can be posted (QoS, retain, etc) so some tweaking there is my next goal.

With regard to the intermittent offline status issue you noted, I’m not sure what’s causing it but the mqtt client running in my script is always disconnecting and reconnecting to the broker. It seems to happen almost every time it gets a status update from Ring (i.e. whenever a sensor pushes a status change) and sometimes when I trigger the alarm mode changes. I’ve got a last will message set that hits the availability topic and says it’s offline whenever the script’s mqtt client disconnects. I was thinking this would be helpful since you’d know if the script was working or not though I may just get rid of that since since it seems so unreliable. It’s not related to the script’s connection to Ring and I still have to work through monitoring that connection since it uses a Node JS library that I don’t know a lot about (and I’m a complete newb at JS in general).

I’ll work on it some more and see if I can come up with a more coherent implementation of of the status’availability setup as I have time to do some more coding over the next week or two.

Yeah, I’ve hacked up my local copy of the script somewhat, and I removed the last will message that was setting the status to OFF. What I saw was that there was a short period of “OFF” status each time the alarm was armed/disarmed, which you could see by subscribing to the topic. I didn’t see this for simple events like contact open/close, but every alarm event for sure. Removing that seems to have made the overall operation more stable, but, looking at the code, I don’t really understand why arming/disarming causes a new connection, but I’m still learning this stuff so I’m probably just missing something obvious.

Great work guys. There will be a big demand for this when it is fully functioning and when there is a hass.io version we can just load up!
Thanks again for all your efforts and keep us all up to date.

2 Likes

@acolytec3

46%20PM

I also have the same issue with setting up the alarm state. Shows unknown. But otherwise all other connected sensors do show their status when I click on them.

This project has a huge potential! I’m now considering how to use the status of these sensors and use MQTT to connect to other devices. Like if I had a Rasperry Pi Zero connected to a speaker and connected to the MQTT broker, then I could let’s say play a tune when there is motion detected from one of the Ring PIR sensors.

I’m sure that I’m doing something wrong. I keep getting an error
module.js:550
throw err;
^

Error: Cannot find module ‘mqtt’
When I try to run the mqttalarm.sh

This is what I have in my mqttAlarm.sh file
RING_USERNAME=‘[email protected]’ RING_PASSPHRASE=‘PASSWORD’ MQTT=‘mqtt://IPADDRESS:PORT’ node mqttAlarm.js

I have the ( ’ ) on either side of my username, password and mqtt address. I’m sure the formatting is where the issue is, any help?

Maybe this is a dumb question but did you install the mqtt module?

From the command line in the directory where you have the repo cloned, make sure you’ve run this command. This should grab all the required modules from npm.
npm install mqtt async ring-alarm

Well color me stupid…

I thought I had installed it but after i did the npm install it is not getting the error. MIght have to hard code the sensors but at least I can move onto the next hurdle.

Thank you acolytec3!

Ok followup question, you all seem to have been discussing this earlier to some extent. I’m relatively new to most of this, but I’m trying.

In the github readme it says the following

chmod a+x mqttAlarm.sh
./mqttAlarm.sh

which I assume is what I type in the CLI. I have been able to do so and get the script running, get the discovery to pick up the devices, get the updated statuses, etc. But when I cloned the git it put it in a file “ring-alarm-HA-plugin” so first i have to cd to that file then run the two commmands.

My issue seems to be as soon as I ^C out or close the cli the status updates stop, and the alarm becomes “unknown”. I can get it back up if i reopen a cmd window cd to the folder and run the .sh script.

Where am I going wrong? Also I have rebooted several times, doesn’t seem to stick.

Don’t know if this was correct but I typed

sudo nohup ./mqttAlarm.sh & 

it allowed me to close and disconnect the terminal window and the process is still running
Although I have not tried my luck with a reboot yet

You have to set it up as a system service so that it restarts across reboots. There are a number of different tutorials across the internets you can follow. I don’t feel qualified to provide a step by step guide but I can fish out my service file if you need a baseline for what mine looks like.

If you could pull your init.d or system.d file and throw it up here that would help. I can’t seem to get it running under system.d

I think it’s due to me having everything in the “ring-alarm-HA-plugin” but I don’t know how to correctly point the service to the folder or path or whatnot. Keep getting errors when I view the log, and it’s the same error I get when I try to manually start the script without first cd-ing to the folder.

Here’s my service file in /etc/systemd/service

[Unit]
Description=Mqtt Alarm Service

[Service]
ExecStart=/home/pi/ring-alarm-HA-plugin/ring.sh
Restart=always
User=nobody
Group=nogroup
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/home/pi/ring-alarm-HA-plugin

[Install]
WantedBy=multi-user.target

As an FYI to those watching this, I’m working on rebuilding the script from the ground up. The guy who built the API integration with ring has a new version out that has some nice additions to it but it’s slow going with my limited JS skills so may be a little bit before I get a new version up and running. I’m hoping the additions he made to the API will solve some of these goofy unavailability issues once I figure out how to incorporate them into the script.

6 Likes

Thanks for the update. Interestingly, I’m also working on doing the same thing as I trying to use this as motivation to learn enough JS to be dangerous. I’m using the API available at https://github.com/dgreif/ring-api for my work so I’m curious what API you are working with. I’m definitely struggling with my overall JS skills as, before this point I had written at most a few small functions. Combine that with a very limited amount of time available right now and it could be quite a while before I manage to get my script to a working state. You’ve already written a working script so you are way ahead of me.

In the interim, the slightly tweaked version of your script that I’m not using is working amazingly well. Removing the last will message and using retain to publish messages works around the disconnect issues, but, obviously, it’s not a proper fix as they still happen, it’s just that HA is unaware of them. The only issue in the last week was when I had a small blip with my local internet connection, the service didn’t reconnect, which isn’t completely surprising since I don’t see anything in the script to deal with disconnect.

So at least I have something that works while we both take our separate approaches to creating a new script. I have no doubt you’ll beat me in that goal, but I’m at least learning a lot during this journey. Thanks for all your work!

If you look at my repo, there are links to the repos I forked. It all started with the doorbot one and this version is about 3 steps removed.