Cannot figure out device_tracker.see service [Solved]

Hello,
I am trying to integrate my Nissan Leaf position as provided to MQTT by the OVMS device in it.
What I have is the latitude and longitude of it written in two sensors.

This is what I am trying to do:
Sensors for coordinates extracted from MQTT by my Leaf OVMS:

   - platform: mqtt
     name: leaf position lat
     state_topic: "ovms/nissanleaf/xxx/metric/v/p/latitude"
     qos: 1
   - platform: mqtt
     name: leaf position lon
     state_topic: "ovms/nissanleaf/xxx/metric/v/p/longitude"
     qos: 1

Automation to update the position/create the tracker:

- id: '1603005245535'
  alias: 'Update Leaf position'
  description: ''
  trigger:
  - platform: state
    entity_id: sensor.leaf_position_lat
  - platform: state
    entity_id: sensor.leaf_position_lon
  condition: []
  action:
  - service: device_tracker.see
    data:
      data_template:
        dev_id: Leaf
        gps:
         - "{{ states('sensor.leaf_position_lat') }}"
         - "{{ states('sensor.leaf_position_lon') }}"
  mode: single

This is actually triggered (I’ve checked) but no tracker is ever created or updated. I created the tracker manually by triggering the method (without gps data) and it works. But as soon as I try to use templates to insert either gps data or battery level it complains about the data_template not being a valid keyword or that I am missing dev_id (depending on how I build up the yaml).

Any suggestion?

Maybe you need to convert them to float first as templates always output a string. You could try with

"{{ states('sensor.leaf_position_lat') | float | round(7) }}"

Edit: I just realized you have data and then data_template. You only need one. If you run 0.115 or higher remove the data_template otherwise remove data

- service: device_tracker.see
  data:
    source_type: gps
    dev_id: leaf 
    gps:
       - "{{ states('sensor.leaf_position_lat') }}"
       - "{{ states('sensor.leaf_position_lon') }}"
1 Like

Tried that too, I always get something on the lines of must contain at least one of mac, dev_id.

I am starting to think that templates are not allowed in that call?

Oh, more: if I actually make the call with hard-coded data in the right format ( [xx.xxx,yy.yyy]) the update is done just fine.
It seems that templating is the issue, I cannot find a format it will accept.

Use Marc’s code, you have data twice in your code.

Use the same format then…
gps:"[{{ states('sensor.leaf_position_lat') }}, {{ states('sensor.leaf_position_lon') }}]"

I’m quite sure that is how I did it in node red.

But won’t this run constantly when you use the car?
Isn’t it better to figure out a different trigger?

Tried with conversion to float, tried inline like suggested by @Hellis81, none worked.
I still get errors either it’s not finding the dev_id or not accepting the value for gps (invalid latitude for dictionary value).

@Hellis81 I know it will trigger often (configurable on OVMS) but that’s the same as any other device tracker using gps like phone or such.

I think you need to add location_name, that is not optional according to here:

Apparently not, removed from my automations

I looked in my node red and here is my setup.

So I create an array with [lat, lon] and save to msg gps.
I then have that array as the input in the json.

I managed to get it! Problem was I was using the automation editor, it does not want you to put “data:” in the field (it’s implicit, apparently). @burningstone 's reply gave me the right hint! So this is what I used and worked:

source_type: gps
dev_id: leaf
gps:
  - '{{ states(''sensor.leaf_position_lat'') | float | round(7) }}'
  - '{{ states(''sensor.leaf_position_lon'') | float | round(7) }}'

I will now try to add the battery attribute. But thank you all for helping out.

Really?

That’s the exact code I posted in the second reply to this thread :roll_eyes:

2 Likes

Indeed, and that is what I used the whole day yesterday! Problem is, I was pasting the “data:” line too.

I pasted this:

  data:
    source_type: gps
    dev_id: leaf 
    gps:
       - "{{ states('sensor.leaf_position_lat') }}"
       - "{{ states('sensor.leaf_position_lon') }}"

instead of this:

    source_type: gps
    dev_id: leaf 
    gps:
       - "{{ states('sensor.leaf_position_lat') }}"
       - "{{ states('sensor.leaf_position_lon') }}"

No, you had an extra data_template line in yours, you didn’t have source_type, and you had a capital letter on Leaf.

@anon43302295, I don’t want to start a polemic thread but I did numerous tries and trust me, the problem was I was adding “data” in the “service data” field of the editor.
I tried to simplify the problem in the description to try to avoid confusion but I spent a whole day yesterday before coming here.
The capital letter is entirely disregarded (works either way), source_type is not mandatory (but I did test with it) and the data_template was indeed in some of my tests but not all of them.
What sorted it was sticking to the editor instead of mixing editor/direct calls/files and filling the right data.
But indeed thank you for answering and the support.

I’m just trying to say that if you came here for help, I provided you with the exact code you needed in the second reply to this thread, all you needed to do was copy and paste it into your automation and you would have been sorted.

Every extra struggle you’ve had from that moment on was unnecessary.

1 Like

Just for the next one to stumble upon the same issue here the complete code as I had some issue to combine all the given code in this thread.

- id: update_ovms_position 
  alias: Update ovms position
  description: ''
  trigger:
  - platform: state
    entity_id: sensor.ovms_v_pos_latitude
  - platform: state
    entity_id: sensor.ovms_v_pos_longitude
  condition: []
  action:
  - service: device_tracker.see
    data:
      mac: FF:FF:FF:FF:FF:F7
      dev_id: ovms
      source_type: gps
      host_name: ovms
      location_name: '{{ states(''sensor.ovms_v_pos_location'') }}'
      gps:
      - '{{ states(''sensor.ovms_v_pos_latitude'') }}'
      - '{{ states(''sensor.ovms_v_pos_longitude'') }}'
      gps_accuracy: 20
      battery: '{{ states(''sensor.ovms_soc'') | round(0) }}'
  mode: single

Hi Andy,
I have been trying to replicate your solution to device tracking. I am receiving the gps co-ordinates and the automation is being triggered when gps co-ordinates are received. However I cannot find the device anywhere (dev-id: Leaf in your case). Where is this set up or is the automation supposed to create it when triggered?

Hi Riseley, did you search among the states in “developer tools” ?
This is my final code for the action in the automation:

mac: 'FF:FF:FF:FF:FF:F7'
host_name: leaf2016
source_type: gps
dev_id: leaf2016
gps:
  - '{{ states(''sensor.leaf_position_lat'') | float | round(7) }}'
  - '{{ states(''sensor.leaf_position_lon'') | float | round(7) }}'
gps_accuracy: 20
battery: '{{ states(''sensor.leaf_soc'') | round(0) }}'

Now I don’t remember if I did other stuff, but one of the alternatives I tested was calling the device_tracker.see manually in the developer’s tools.
What is the code you’re using?

Hi Andy,
Thanks to you, I am a little more further due to the fact that I had not set up mac, host name and gps accuracy.
I have called the device_tracker.see service by inserting the mac as well. I can now see the entity and mac but no plot on the map yet. Look forward to any suggestions.

Here is my configuration.yaml:
device_tracker:

  • platform: mqtt
    devices:
    civic_position_latitude: ‘civic/position/latitude’
    civic_position_longitude: ‘civic/position/longitude’
  • platform: mqtt
    devices:
    audi_position_latitude: ‘audi/position/latitude’
    audi_position_longitude: ‘audi/position/longitude’
  • platform: mqtt
    devices:
    crv_position_latitude: ‘crv/position/latitude’
    crv_position_longitude: ‘crv/position/longitude’
  • platform: mqtt
    devices:
    civic_position_latitude2: ‘civic/position/latitude2’
    civic_position_longitude2: ‘civic/position/longitude2’

Automations.yaml -

  • id: ‘1610996729565’
    alias: Update Civic position
    description: ‘’
    trigger:
    • platform: state
      entity_id: device_tracker.civic_position_latitude
    • platform: state
      entity_id: device_tracker.civic_position_longitude
      condition: []
      action:
    • service: device_tracker.see
      data:
      data:
      mac: FF:FF:EE:FF:EE:FE
      host_name: civic20152
      source_type: gps
      dev_id: civic20152
      gps:
      - ‘{{ states(’‘sensor.civic_position_latitude’’) }}’
      - ‘{{ states(’‘sensor.civic_position_longitude’’) }}’
      gps_accuracy: 20
      mode: single
  • id: ‘1611080901457’
    alias: Update Civic T2 Position
    description: ‘’
    trigger:
    • platform: state
      entity_id: device_tracker.civic_position_latitude2
    • platform: state
      entity_id: device_tracker.civic_position_longitude2
      condition: []
      action:
    • service: device_tracker.see
      data:
      data:
      mac: EE:EE:EE:FF:EE:21
      host_name: civic20153
      source_type: gps
      dev_id: civic20153
      gps:
      - ‘{{ states(’‘sensor.civic_position_latitude2’’) | float | round(7) }}’
      - ‘{{ states(’‘sensor.civic_position_longitude2’’) | float | round(7) }}’
      gps_accuracy: 20
      mode: single