Mosquitto and Owntracks
-
Cheers
I'm only trying to add this bit:
- id: mqtt name: MQTT enabled: true implementation: MQTTController config: # Replace IP with that of your MQTT broker below source: "mqtt://127.0.0.1:8883/" user: something password: something
Although the user and password are correct, I'm not clear if I need to quote them or not. Restarting reactor with that in my (otherwise working) reactor.yaml: Reactor stops and just sits there, logging nothing. (Not checked the Mosquitto logs yet)
If I swap reactor.yaml back to a working version reactor starts fine. So it's got to be something real easy.....C
-
@catmanv2 I am no expert but try this:
Change user: to username: Also, I believe both username: and password: should be the same indenting as source. -
@sweetgenius said in Mosquitto and Owntracks:
@catmanv2 I am no expert but try this:
Change user: to username: Also, I believe both username: and password: should be the same indenting as source.Thanks. username makes no difference. Does the indenting really matter? If so I shall have to get some code editor as vi isn't letting me get it right
C
-
@catmanv2 said in Mosquitto and Owntracks:
Does the indenting really matter?
Yes, indenting matters, like @therealdb said previously.
Is port 8883 the correct port for your setup? -
@sweetgenius said in Mosquitto and Owntracks:
@catmanv2 said in Mosquitto and Owntracks:
Does the indenting really matter?
Yes, indenting matters, like @therealdb said previously.
Is port 8883 the correct port for your setup?Lovely, ta. I shall fire up VS code! 8883 is correct as well
C
-
@catmanv2 said in Mosquitto and Owntracks:
Does the indenting really matter?
Yes! YAML is highly dependent on indenting! And you must use spaces, not tabs. Here are a few good YAML tools on line to help:
- https://yamllint.com - Check YAML (paste and click)
- https://yamlchecker.com - Another checker
- https://onlineyamltools.com/edit-yaml - A handy web-based syntax-driven editor
-
Wow. Thanks ever so. Binned the sample code for the Owntracks manual and 'handcrafted it'. First time
<edit: re reading, it's not clear that at this point Reactor happily started and now shows the MQTT entity, so problem solved>
C
-
@therealdb VS code saw nothing. Yamlchecker picked up an issue, but even with that corrected, it didn't work <shrug>
Now does anyone have an Owntracks template?
Otherwise this is going to be a steep curve I suspect
C -
toggledbitsreplied to CatmanV2 on Sep 16, 2022, 8:18 PM last edited by toggledbits Sep 16, 2022, 4:23 PM
@catmanv2 said in Mosquitto and Owntracks:
it didn't work
Three words that should never be written into a post asking for help. At least, as I've often said, not as the entire summary of where you are. I suggest a form more like this (the numbering is just to separate component parts of your question/post):
"OK, It didn't work. Here's where I am with this darned thing..."
-
Describe what part/thing you are trying to do; if part of multiple step process, tell us both what you are trying to do overall, and what step of that process you are working on that is going wrong
"I'm trying to get my MQTTController instance in MSR to connect to my broker, and I need to use a username/password for authentication. I have configured the username and password in my
reactor.yaml
file." -
Describe what you expected to see.
"When I restarted MSR and hard-refreshed my browser, I thought I would see the MQTT system entity in the Entities list, or the controller listed as "up" on the Controller Status widget on the Status page.
-
Describe what you actually see (e.g. errors or exceptions) or what you don't see that you expected. If there's an error on the screen, make sure your narrative includes the full and complete text of it, and describe where it came up and what you were doing when it did. If it's a corrupted display or something better shown with a screen shot, add that behind your narrative.
"The MQTT controller doesn't show up at all in either the Entities list or Controller Status widget."
... or maybe it's worse than that?
"Reactor doesn't even start now."
-
Show your work. Give the problem context.
"Here's what the configuration section for my MQTTController instance looks like when it fails:". Paste the text of the configuration from first line to last without redaction or modification except passwords/access tokens/private keys[1].
-
Show that you dug for an answer yourself:
"I looked in the logs, and found these messages:" — follow with snippet of log, with one or two dozen lines of context before what you found. Never post just an error message without context.[1]
In many cases, this last step will lead you to answering your own question. If you can't interpret what you are seeing, that's perfectly fine, but we need to see it to help you, and there's no point in making us ask for it. Go look for it yourself, and give it up front.
Give us something to go on. Messaging back and forth in a forum like this is the worst way to play "Twenty Questions". The less detail you give, the longer it takes to get a solution. And the old Internet trope of the 2000s is still true in the current decade: "People will put as much effort into answering your question as you put into asking it."
IMO, the posting guidelines I wrote for the Reactor category here (which are based on posted guidelines created for Home Assistant's forums, which are based on guidelines created elsewhere) should be applied everywhere. It's in your interest.
---
[1] Formatting code and log snippets should be done using the ``` sequence on a line by itself, followed by the code or log snippet, followed by the ``` sequence on a line by itself again.
```
controllers:
- id: mqtt
name: MQTT
implementation: MQTTController
enabled: true
```
looks like this when your post is later displayed (which is helpful/necessary for readability and finding errors in YAML in particular):controllers: - id: mqtt name: MQTT implementation: MQTTController enabled: true
(@CatmanV2 , you clearly know how to do this formatting, I'm just adding this for completeness/benefit of future readers).
-
-
CatmanV2replied to toggledbits on Sep 16, 2022, 8:25 PM last edited by CatmanV2 Sep 16, 2022, 4:29 PM
@toggledbits said in Mosquitto and Owntracks:
@catmanv2 said in Mosquitto and Owntracks:
it didn't work
Three words that should never be written into a post asking for help.
OK, fair shout but it was more a comment of 'It didn't work then'. (Not withstanding the rest of your valued guidance) I am no longer asking for help (on that issue), nor was I when I made that post. Because It was, buy that point, working.
C
-
OK. That wasn't clear to me. It seemed like you were still having issues. I am, in any case, glad to hear you made progress.
What step are you on now? Have you made your broker accessible on a public IP and port?
-
Yes, thanks.
I can see Owntracks connecting, and also Reactor is happily showing the MQTT Entitiy. So I think that all the connections are there. The next step for me would be to get the correct configuration for Reactor to, well, react to an Owntracks message.
My only request is if anyone has an Owntracks / YAML template for reactor (which is what I think I need next) that would save me from trying to create a new one....
....which I will cheerfully do once I've read a lot more about documentation and so on.Cheers
C
-
toggledbitswrote on Sep 16, 2022, 9:10 PM last edited by toggledbits Sep 17, 2022, 7:10 PM
Here's a simple config for a binary sensor that is true when I am home (i.e. in the region called "Home" in Owntracks):
entities: owntracks_patrick_home: name: "Patrick Home" capabilities: [ binary_sensor ] primary_attribute: binary_sensor.state type: BinarySensor events: "owntracks/user/phrs21": "binary_sensor.state": json_payload: true if_expr: "payload._type == 'location'" expr: > ( first region in ( payload.inregions ?? [] ) with region == "Home": true ) ?? false
The
capabilities
andprimary_attribute
set up the entity in Reactor to hold the interpreted payload data from Owntracks' location updates. In this case, we're just going to keep a binary in/out state for the one region, nothing else.The event configured here reacts to an incoming "owntracks/user/phrs21" topic/message (
phrs21
is set in the Identification part of the connection configuration in the Owntracks app). When that topic comes in, we declare that the attributebinary_sensor.state
is going to be modified according to the expression given. Thejson_payload: true
line tells MQTTController to interpret that payload as JSON data and make a proper object out of it that the expression can then pick through easily. Theif_expr
expression makes sure that the assignment to the attribute is done only if the payload type islocation
(there are several message types that may be sent, not all of which contain region data). Theexpr
expression itself accesses and loops through theinregions
array of the payload (now object) to see ifHome
is an element of the array, and returns true if so. Otherwise, the expression returns false (i.e. not home). That result is assigned to thebinary_sensor.state
attribute..Some details:
- The expression fragment
( payload.inregions ?? [] )
is using the coalesce operator??
. Ifinregions
does not exist in the payload (i.e. you are not in any region), thepayload.inregions
reference would result in null, so the coalesce replaces that with an empty array ([]
). This prevents the expression from throwing an error ifinregions
is missing/null. - The ending fragment
?? false
is a similar use of the coalesce operator because, if "Home" is not found ininregions
, thefirst..in
statement will return a null (which is what it does when nothing matches). Using the coalesce converts that null into a real false, indicating that we didn't find "Home" listed and we are therefore surely not Home.
Edit: added
if_expr
in the config, and explanation in the narrative. - The expression fragment
-
Thankyou, sir!
C
-
Also, I've just added this as a template called
owntracks_in_region
to build 22260 of MQTTController. -
FYI, I revised the config and the narrative to make sure it doesn't try to update the sensor for message types that would never contain
inregions
(and thus could cause the binary sensor to spuriously go false). -
I'd like to add further regions for Owntracks. Would the reactor.yaml look like this:
rachel_iphone: name: "Rachel Phone" topic: racheliphone uses_template: owntracks_in_region regionName: Home user: xxx
Would I need to duplicate everything in the above, with the different regionName? or just the last two (or three) lines?
TIA
C
-
Wow. Apparently yes with a different entity name (durr)
Quite pleased with myself that I also managed to not screw the .yaml!
C
-
I'm sorry, I read this thread yesterday and got busy with (non-HA) stuff all day and never got back to it...
You are correct... new entity ID, new entity name, change the region name as needed, good to go.