Location Tracking - MQTT Sensor to Tracker

Hey all,

Having issues using device_tracker.see in an automation to grab MQTT sensor latitude and longitude to create a device tracker.

Basically i’ve got some data flowing in from MQTT from the below in the configuration yaml

  - platform: mqtt
    name: "Lora Sensor GPS Latitude"
    state_topic: "application/2/device/deviceid/rx"
    value_template: "{{ value_json.object.latitude  | float | round(7) }}"
  - platform: mqtt
    name: "Lora Sensor GPS Longitude"
    state_topic: "application/2/device/deviceid/rx"
    value_template: "{{ value_json.object.longitude  | float | round(7) }}"

I then use this in an automation to update a tracker as below

alias: Lora GPS Tracker
description: ''
trigger:
  - platform: state
    entity_id: sensor.lora_sensor_gps_longitude
  - platform: state
    entity_id: sensor.lora_sensor_gps_latitude
condition: []
action:
  - service: device_tracker.see
    data_template:
      mac: 'FF:FF:FF:FF:FF:EE'
      dev_id: loragpstest
      host_name: lora
      location_name: home
      gps:
        - '{{states(''sensor.lora_sensor_gps_latitude'')}}'
        - '{{states(''sensor.lora_sensor_gps_longitude'')}}'
      gps_accuracy: 80
      battery: 100
mode: single

but i get this error

2021-03-01 17:07:09 ERROR (MainThread) [homeassistant.components.automation.lora_gps_tracker] Error while executing automation automation.lora_gps_tracker: invalid latitude for dictionary value @ data['gps']

I’ve searched high and low, and found other forum posts, but nothing has helped.

If i run this through developer tools and change from a template to raw numbers, it works fine. e.g. below

      mac: 'FF:FF:FF:FF:FF:EE'
      dev_id: loragpstest
      host_name: lora
      location_name: home
      gps:
        - 10
        - 20
      gps_accuracy: 80
      battery: 100

If i check the template in dev tools, it seems fine.

{{states('sensor.lora_sensor_gps_latitude')}} 
Result type: number
-32.xxxxx

Any help would be great… really doing my head in

1 Like

use double quotes outside your brackets

gps:
  - "{{states('sensor.lora_sensor_gps_latitude')}}"
  - "{{states('sensor.lora_sensor_gps_longitude')}}"

Try casting it as a float in the gps template as well.

gps:
  - "{{states('sensor.lora_sensor_gps_latitude') | float }}"
  - "{{states('sensor.lora_sensor_gps_longitude') | float }}"
1 Like

Ooh, you’re right.

I missed that.

Thanks for the replies. I’ve tried a bunch of different formatting and casting, but no dice.

Just tried this in dev tools and had the same error.

I’m wondering if its the way i’m pulling in the sensor data, but the template seems to say its a number… also casting should fix that… stumped

mac: 'FF:FF:FF:FF:FF:EE'
dev_id: loragpstest
host_name: lora
location_name: home
gps:
  - "{{states('sensor.lora_sensor_gps_latitude')}}"
  - "{{states('sensor.lora_sensor_gps_longitude')}}"
gps_accuracy: 80
battery: 100

Update :

I tried pulling data from another existing tracker, and it didnt work either

mac: 'FF:FF:FF:FF:FF:EE'
dev_id: loragpstest
host_name: lora
location_name: home
gps:
  - "{{state_attr('device_tracker.xxxx','latitude')}}"
  - "{{state_attr('device_tracker.xxxx','longitude')}}"
gps_accuracy: 80
battery: 100

This won’t work from developer tools as developer tools doesn’t accept templates.

Please create a script, thus:

script:
  test:
    sequence:
      - service: device_tracker.see
        data:
          dev_id: loragpstest
          location_name: home
          gps:
            - "{{ states('sensor.lora_sensor_gps_latitude')|float }}" 
            - "{{ states('sensor.lora_sensor_gps_longitude')|float }}"

Do not use the ui, copy and paste the script exactly (forgoing the script: key as appropriate depending on your configuration).

Then ensure that both sensor.lora_sensor_gps_longitude and sensor.lora_sensor_gps_latitude have a usable value and run the script.

Let us know if this is successful, and if not what results you observe and the full contents of any log entries that are generated.

From this test we can narrow down what the problem is.

2 Likes

Didnt realise that on the dev tools - feature request? :wink:

The script worked!

Ran it off the existing tracker state_attr, as my lora device is offline at the moment, and 2 hours away.

I was testing it purely in automation for a while, so not sure why it wasn’t working then. Any reason why it wouldnt work in an automation, instead of having an automation triggering a script?

Maybe the lora sensor value isn’t usable, buts just a float value so I must have stuffed something up in the config. When its back online, i’ll check using it and loop back tomorrow.

Thanks!

1 Like

If it works in the script it will work in an automation. Copy and paste the service and data back into your automation.

I can’t say why it wasn’t working before, only to say that there must have been a syntax error somewhere that none of us were spotting, which is why I suggested using some code that I freshly wrote so I knew it was syntactically correct, and putting it in a script to make sure you didn’t ‘cheat’ and try editing your old automation rather than importing the code I wrote.

Yeap, seems to be working now. Thanks all!

Thanks for this! Is there a way to set the tracker itself to unavailable (instead of home/away) in case the lora device is offline (not sending position updates for a while)?