Mosquitto and Owntracks
-
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
-
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
-
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
-
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.
-
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.
@toggledbits Never a problem. Good learning experience
What I was afraid of was spending hours troubleshooting the wrong syntax, or the right syntax with the broken .yaml
C
-
Yeah, I
hatedislike YAML. But the alternatives were basically JSON (even more strict and harder to read) or the Windows INI format (too brain-dead and would require a lot of contrivances to represent arrays and structures), or something else I came up with myself that would have no outside support. At least most programming editors can highlight and check YAML, and there are some good web sites for checking and editing it as well. But relief is coming...Good online YAML editor: https://onlineyamltools.com/edit-yaml