Homeassistant.set_location

Hi again, another update. I’m trying to use the following to automatically set latitude and longitude values but when i’m testing it in the developer " services" screen i get a error " invalid latitude for dictionary value @ data[‘latitude’] "

The JSON data i’m using is
{
“latitude”: " {{ states.device_tracker.life360_caravan.attributes.latitude }}" ,
“longitude”: " {{ states.device_tracker.life360_caravan.attributes.longitude }}"
}

Am i on the right track here or am i just making bad guesses ? :slight_smile: and any help on the error please

regards

Your syntax is correct but the quotation marks around latitude and longitude are a different character set; & instead of ". Try this:

{
"latitude":"{{ states.device_tracker.life360_caravan.attributes.latitude }}",
"longitude":"{{ states.device_tracker.life360_caravan.attributes.longitude }}"
}

thanks for the reply. unfortunately same error though.

voluptuous.error.MultipleInvalid: invalid latitude for dictionary value @ data[‘latitude’]

It looks like the Service Dev Tool doesn’t support Templating.
The syntax should work in an Automation, like this…

- alias: Update Location
  initial_state: true
  trigger:
    - platform: state
      entity_id: device_tracker.life360_caravan
  action:
    - service: homeassistant.set_location
      data_template:
          {
          "latitude":"{{ states.device_tracker.life360_caravan.attributes.latitude }}",
          "longitude":"{{ states.device_tracker.life360_caravan.attributes.longitude }}"
          }

…but it seems redundant since your lat/lon source appears to be a device_tracker that’s already integrated into HA (should already update your location automatically).

2 Likes

Thanks for that, works good now.

now im off to get google maps working and i’ll be a happy man :slight_smile:

2 Likes

I am trying to get an automation to update my location.

I have the longitude and latitude in entities.

Here is my code and it is not passing the config test.

- id: updatemylocation1
  alias: Update MH Location
  trigger:
  - entity_id: binary_sensor.wyzesense
    from: 'off'
    platform: state
    to: 'on'
  action:
  - service_template:
     {     
     latitude: {{ states("sensor.nodered_d988b4f8_8db528") }},
     longitude: {{ states("sensor.nodered_37f79a7_16a4066") }}
     }    
    service: homeassistant.set_location
  initial_state: true
  mode: single
  action:
  - service: homeassistant.set_location 
    data_template:
      latitude: >
        {{ states("sensor.nodered_d988b4f8_8db528") }}
      longitude: > 
        {{ states("sensor.nodered_37f79a7_16a4066") }}
1 Like

Thanks tom_1!

Here is my working automation:

- id: '001456'
  alias: Update MH Location
  trigger:
  - entity_id: binary_sensor.wyzesense
    from: 'off'
    platform: state
    to: 'on'
  action:
  - service: homeassistant.set_location 
    data_template:
      latitude: >
        {{ states("sensor.nodered_d988b4f8_8db528") }}
      longitude: > 
        {{ states("sensor.nodered_37f79a7_16a4066") }}
    

I also have Home Assistant installed in my RV. I’m using the homeassistant.set_location to set my location based on GPS coordinates. That works as expected and the map on the Configuration->General page updates accordingly.

But the HA timezone does not update based on the new location. I have no time zone specified in the configuration.yaml so that HA would automatically computed the time zone based on my location. I have tried calling homeassistant.reload_core_config hoping this would update the time zone based on the new location, but no luck. I also don’t see a service that I can call to specify a new time zone.

Is there a way to update the time zone within an automation?

3 Likes

Mark, I have the same issue! - did you ever manage to figure out a solution?

Unfortunately I did not.

You should make a feature request for this service and for automatic detection. This was how the position updating was added.

1 Like

I’ve created a feature request for updating the timezone when the location is updated. Please provide your vote for this feature.

You forgot to vote for your own request.

Voted for the FR and have a question. What GPS trackers do you use connected to HA?

Using the gpsd integration and a generic USB GPS receiver.

1 Like

Just a update to my original post.

I got sick of using my phone GPS to trigger the home location of my caravan so i recently got a USB GPS receiver which i plugged into a different Raspberry Pi to what my Home assistant is running.
I couldn’t get the HA GPSD intergration to work so took a detour to that approach.

From the GPS RPI i then had to send that data to the HA RPI. I did that by installing gpsd on the GPS pi and then wrote the following python code which takes the gpsd data from one pi and sends it via MQTT to Home Assistant on the config / state and attributes topics.

The code i wrote can be found on my Github page if it’s of interest to anyone.

Grab the code here ---- GPSD Data over MQTT. Download the file to something like /home/pi/

The basics of the python code -

Details on usage are in the docstring (top of the file) outlining the basics of how to get it going. However the general idea is to download the gpsdata.py file to the RPI that has the GPS receiver on it.

On that RPi install…

sudo apt-get update

then

sudo apt install gpsd

and

sudo apt install gpsdclient

  • Then follow the info in the code such as setting mqtt username and password etc.
  • Edit your configuration.yaml as per instructions in file
  • create a automation in HA as per instructions in file
  • create a cron task as per instructions in file

All going well HA should now see your new device tracker and auto update the home location accordingly.

Hope that helps anyone that got stuck with using the GPSD integration on HA or is running the gps from a different device.

1 Like

On the same Raspberry Pi as HA?
If so how?
Thanks.

I see indication all over the place that the answer is “no”.
Now i wonder: Why isn’t this possible on the same RPi as HA is running on?

Perhaps a bit late for those who had asked installing gpsd (and GPS receiver) on the same RPi as HA. Yes you can do this. I’ve had this running for a few years.