custom-components / remote_homeassistant

Links multiple home-assistant instances together
Apache License 2.0
933 stars 81 forks source link

remote device_tracker local zone handling #128

Open pejotigrek opened 3 years ago

pejotigrek commented 3 years ago

I have two HA instances connected via remote_homeassistant component - let's call them LOCAL and REMOTE. LOCAL = main instance REMOTE = additional instance

there are device_tracker entities [people's phones], that are REMOTE only, but I want to see them in LOCAL, so I am "forwarding" them with the component. the issue is, that what's "home" for REMOTE is at least "not_home" for LOCAL ["not_home" or other, named zone] but the forwarded entity doesn't know that.

this is messing up with some automations and groups in LOCAL instance obviously.

is there any chance for configuring the device_tracker "forwarding" in a way that it compares it's location with LOCAL zones and displays it accordingly, instead just displaying zone with REMOTE definitions?

geez, I really hope I wrote it all clear enough.. it's much easier to tell about it, than write, especially when english isn't my first language ;)

LazyTarget commented 2 years ago

Hi @pejotigrek , I understand your explanation and am in a similar spot. One way to "workaround" it would be to have the person entities in LOCAL and refer the device_trackers that are imported from REMOTE

That is what I am playing around with now, though one limitation I have noticed is that if the connection is broken all REMOTE entities are removed until reconnected, which means that a person can simply disappear from the map, without any history of where the person was last seen

That is the burning point for me, as for me it doesn't matter which instance that has the person or zone domain


UPDATE: Perhaps if the device_trackers where persisted. #70

pejotigrek commented 2 years ago

@LazyTarget, there has to be some kind of telepathic communication going on :D - yesterday I did some workaround that I'm testing now and seems it is fulfilling my needs, and it sounds similar to your idea, but as for now - without any dissapearing problems. so here's what I did a day ago:

  1. for starters: let's say the "troublesome" zone is named "The Office". so on LOCAL the name is "The Office", but on REMOTE the place is just "home".
  2. if needed - on REMOTE: configured same zones as on LOCAL. not all, but those that I'm interested in [minus "The Office" - it remains "home" on REMOTE]
  3. along with the "normal" google maps device trackers on REMOTE, I made mqtt device trackers - as with some tricks it can be used as a _template device tracker_t:
device_tracker:
- platform: mqtt
  devices:
    phone_a: "device_tracker/phone_a"
    phone_b: "device_tracker/phone_b"
  qos: 1
  payload_home: "on"
  payload_not_home: "off"

then, I wrote a small automation which is setting the REMOTE location [zone] properly, automation on REMOTE:

automation:
- id: mqtt_tracking
  alias: mqtt_tracking
  initial_state: on
  mode: restart
  trigger:
  - platform: state
    entity_id: device_tracker.google_maps_XXX
    id: person_a
  - platform: state
    entity_id: device_tracker.google_maps_YYY
    id: person_b
  action:
  - service: mqtt.publish
    data:
      topic: "device_tracker/{{ trigger.id }}"
      payload_template: "{{ 'The Office' if trigger.to_state.state == 'home' else trigger.to_state.state }}"
  1. configured remote connection on LOCAL, so the imported entities are prefixed with office - so device_tracker.google_maps_XXX becames device_tracker.office_google_maps_XXX etc.
  2. in the person's settings on LOCAL I chose the corresponding device_tracker.office_* entity.

that's all. and because in the end - those persons are somehow "watched" in LOCAL instance too, every person is defined by few device_trackers. the location/zone of the person is taken from the most recent one, and it is kept on LOCAL during all reboots etc. thanks to being set in recorder's config.

broyuken commented 2 years ago

This still the only way to handle this? Or is there a better way to have one person represented in multiple HA’s?

pejotigrek commented 2 years ago

This still the only way to handle this? Or is there a better way to have one person represented in multiple HA’s?

I'm afraid so :( about a month ago I did tried to tie the google maps' device tracker from remote to local and play something with that but it still gave the outcom said in the issue. so I'm back on mqtt again.

Bagunda commented 1 year ago

2. я сделал трекеры устройств mqtt

How to create another mqtt client in home assistant that would connect to a broker on the Internet and subscribe to a specific topic?

TheOneOgre commented 5 months ago

Struggling with this currently. I have automations in both places that rely on a not_home state for people. I would just add both servers to the device, but I found the updates are half as often as they are split between the 2 servers, which causes issues on automations that rely on GPS data (delayed HVAC/light routines)

~I tried only importing device_trackers and not person, but that doesn't seem to work.~

~Does HA support not_* for something like not_farm like home and not_home?~

See my comment below for a workaround

TheOneOgre commented 5 months ago

So as an update, I figured out a workaround that works pretty much perfectly for me. I created a valid setup using the RESTful api but I found a better way that still utilizes this integration.

On the remote instance, you need to create a sensor using the gps attributes from the device tracker you want. Which looks like this

sensor:
  - platform: template
    sensors:
      dad_gps:
       value_template: none
       attribute_templates:
        latitude: "{{states.person.dad.attributes.latitude}}"
        longitude: "{{states.person.dad.attributes.longitude}}"
      mom_gps:
       value_template: none
       attribute_templates:
        latitude: "{{states.person.mom.attributes.latitude}}"
        longitude: "{{states.person.mom.attributes.longitude}}"

This will create 2 new sensors, dad_gps and mom_gps. Now, on your local instance, import those entities via remote_homeassistant

include:
      entities:
      - sensor.dad_gps
      - sensor.mom_gps

After that, we're going to set up an automation on the local instance to create the device_trackers. You will need an automation for each sensor so edit accordingly. Replace PREFIX with your instance prefix if you are using it. This also incorporates a trigger when HA is started so the tracker is updated immediately instead of remaining "Unknown" until the sensor updates.

alias: Mom Tracker
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.mom_gps
  - platform: homeassistant
    event: start
condition: []
action:
  - service: device_tracker.see
    data:
      dev_id: mom_tracker
      gps:
        - "{{ states.sensor.PREFIX_mom_gps.attributes.latitude }}"
        - "{{ states.sensor.PREFIX_mom_gps.attributes.longitude }}"
mode: single

Annnnd that's pretty much it! This works perfectly for what I was trying to achieve. Zones are updated using the local instance, as that is where the device_trackers are created. Now you can assign them to a person if you wish! You can also edit the friendly names of these trackers via known_devices.yaml.

Hope this helps someone!

Heisenbergnas commented 1 week ago

TheOneOgre....This works perfectly! Been trying to figure this out for months and came across your post today. Thanks much!

TheOneOgre commented 1 week ago

TheOneOgre....This works perfectly! Been trying to figure this out for months and came across your post today. Thanks much!

Glad it works for you! I've been running it since my comment it's been flawless! Surely there's a way for this kind of implementation to be baked into the integration?

Heisenbergnas commented 1 week ago

TheOneOgre....This works perfectly! Been trying to figure this out for months and came across your post today. Thanks much!

Glad it works for you! I've been running it since my comment it's been flawless! Surely there's a way for this kind of implementation to be baked into the integration?

One can only hope...... Thanks again.