Sensor config for a tracker device, please help!

Quite new to home assistant but truth to be told it’s a very hard learning curve with anything which cannot be done in the UI.

Use case:

  1. HA installed in a campervan and I have a tracker device which hasn’t got a unique ID and the latitude, longitude coordinates and the altitude are only available as attributes. I need to convert these attributes to sensors to use them in HA.
device_tracker:
  - platform: traccar
    host: localhost
    port: 18682
    username: myusername
    password: mypassword
    event: ['device_moving', 'device_stopped']
    new_device_defaults:
      track_new_devices: true
  1. I would like to update the Home (zone.home) location (ie the lat/lon/alt) in every 5 minutes based on said tracker device location after a trigger event (when above tracker left zone.home)

Achivements:

  1. none - see later
  2. I setup three number helpers input_number.home_latitude, input_number.home_longitude and input_number.home_altitude
    Then I managed to set up an automation which updates zone.home by calling the homeassistant.set_location service based on the coordinates I manually feed in to the helpers.
    That was two days ago.

Now it’s 7am, soon I’d need to get up but still here I am and this was my best shot at it (obviously rewritten and changed a 100 times)

sensor:
  - platform: template
    sensors:
      van_latitude:
        value_template:
          "{% set input_number.home_latitude = state_attr('device_tracker.crafter', 'latitude') | float %}" 
      van_longitude:
        value_template:
          "{% set input_number.home_longitude = state_attr('device_tracker.crafter', 'longitude') | float %}"
      van_altitude:
        value_template:
          "{% set input_number.home_altitude = state_attr('device_tracker.crafter', 'altitude') | float %}"

This latest version does not work (just like any previous ones) and also gives me a config error.

Please!

Edit: Obviously, I am open to suggestions as I have a feeling, that there must be a more streamlined and elegant solution…

What i don’t understand is why you set the input numbers in the sensor definition. To have the lat/long in a sensor i would do it the following way (already in the new template domain):

template:
  - sensor:
      - name: "van_latitude"
        state: "{{ state_attr('device_tracker.crafter', 'latitude') | float }}"
      - name: "van_longitude"
        state: "{{ state_attr('device_tracker.crafter', 'longitude') | float }}"
      - name: "van_altitude"
        state: "{{ state_attr('device_tracker.crafter', 'altitude') | float }}"

Then you should have 3 template sensors with latitude, longitude and altitude, depending on the attributes of your device tracker. Untested but should work

1 Like

@madface
Thank you Mike!! :sunny: I have the sensors, and the format seems quite obvious :smiley:

What i don’t understand is why you set the input numbers in the sensor definition.
Because I have no idea what I am doing…

Although, in the last hour I figured out how to get the states, but it was still a long way from setting up the sensors:

{{ state_attr('device_tracker.crafter', 'latitude') }}
{{ state_attr('device_tracker.crafter', 'longitude') }}
{{ state_attr('device_tracker.crafter', 'altitude') }}
{{ states('device_tracker.crafter') }}

Still some stuff to do as I replaced the helpers in the automation with the sensors and with them it does not work. Hmmmm

@madface
just a quick update @10am

I actually managed without the sensors as in the automation I used the “newly discovered” state_attr :smiley:
I leave the sensors in the config as I will definitely need them later on, as a matter of fact I will set up some more for this unit (speed, motion, status, etc)

service: homeassistant.set_location
data_template:
  latitude: |
    {{ state_attr('device_tracker.crafter', 'latitude') }}
  longitude: |
    {{ state_attr('device_tracker.crafter', 'longitude') }}

I will make a short writeup for idiots like me.

Mike, thanks a lot, you just made my day!!