Convert GPS coordinates

Hi Team,

Need some help with converting GPS coordinates to something that Home assistant maps can hopefully use.

ESPHome config:

gps:
  latitude:
    name: "${friendly_name} Latitude"
    id: "latitude"
  longitude:
    name: "${friendly_name} Longitude"
    id: "longitude"

The above outputs in the following format:
latitude: -12.345678
longitude: 123.456789

I would like to convert the latitude and longitude into a new string called “location” which should be in this format: -12.345678, 123.456789

Assume this will be in text_sensor: lamdba, but not sure how to write this.

Thanks in advance

This is the lambda code that you can use in the text template sensor:

text_sensor:
  - platform: template
    ...
    lambda: |-
        char buf[50];
        sprintf(buf, "%f, %f", id(latitude).state, id(longitude).state);
        return buf;
1 Like

Hi there,

Thank you for your reply.
This is the errors that I am receiving when compiling the code:

Compiling /data/test/.pioenvs/test/src/main.cpp.o
/config/esphome/test.yaml: In lambda function:
/config/esphome/test.yaml:67:14: error: could not convert 'buf' from 'char [50]' to 'esphome::optional<std::__cxx11::basic_string<char> >'
   67 |         return buf;
      |              ^~~
      |              |
      |              char [50]
*** [/data/test/.pioenvs/test/src/main.cpp.o] Error 1

Config used:

text_sensor:
- platform: template
    name: location
    lambda: |-
        char buf[50];
        sprintf(buf, "%f, %f", id(latitude).state, id(longitude).state);
        return buf;

Any ideas?

I am not at all a C++ expert, but try one of these:

return { buf };
return { buf.c_str() };

My bad, change the last line to

return std::string(buf);
1 Like

Thank you. This is working now.
Working GPS code:

substitutions:
  device_name: gps
  friendly_name: "GPS"

#################################
esphome:
  name: ${device_name}
  friendly_name: ${friendly_name}

esp8266:
  board: esp01_1m


# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: !secret api_key
  reboot_timeout: 0s

# Enable OTA
ota:
 safe_mode: true
 password: !secret ota_password

wifi:
  networks:
  - ssid: !secret wifi_ssid
    password: !secret wifi_password
  domain: !secret wifi_domain

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "${friendly_name} Fallback"
    password: !secret wifi_fallback

captive_portal:

# Enable Webserver
web_server:
 port: 80

text_sensor:
  - platform: wifi_info
    ip_address:
      name: ${friendly_name} IP
    ssid:
      name: ${friendly_name} SSID
    bssid:
      name: ${friendly_name} BSSID
    mac_address:
      name: ${friendly_name} MAC

  - platform: version
    name: ${friendly_name} Version

  - platform: template
    name: ${friendly_name} Location
    lambda: |-
        char buf[50];
        sprintf(buf, "%f, %f", id(latitude).state, id(longitude).state);
        return std::string(buf);


# Status LED
status_led:
  pin:
    number: GPIO2
    inverted: yes

# Reset Button
button:
  - platform: restart
    name: "${friendly_name} Restart"

# GPS Module configuration entry
uart:
  rx_pin: GPIO4 #D2
  tx_pin: GPIO5 #D1
  baud_rate: 9600

sensor:
#wifi signal Sensor
  - platform: wifi_signal
    name: "${friendly_name} WiFi Signal"

#Uptime Sensor
  - platform: uptime
    name: "${friendly_name} Uptime"

# Declare GPS module
gps:
  update_interval: 30s
  latitude:
    name: "${friendly_name} Latitude"
    id: "latitude"
  longitude:
    name: "${friendly_name} Longitude"
    id: "longitude"
  course:
    name: "${friendly_name} Course"
  altitude:
    name: "${friendly_name} Altitude"
  speed:
    name: "${friendly_name} Speed"
  satellites:
    name: "${friendly_name} Satellites"