I’m working on a mobile HA install and have location coming to MQTT as below:
I’d appreciate some help with two items:
How can I get Lattitude and Longitude from the JSON above into separate sensors
(It’s the ‘VR: "[54.xxxxxx, -1.xxxxxx]"
’ from above.
Secondly, how can I update HAs location using the created sensors?
Any pointers to the right direction will be much appreciated.
I have created a sensor (of sorts):
mqtt:
sensor:
- name: "GPS Coordinates"
unique_id: gps_coordinates
state_topic: "cipgps"
value_template: "{{ value_json.VR }}"
json_attributes_topic: "cipgps"
json_attributes_template: "{{ value_json.VR | tojson }}"
I’m trying to pull out just the VR data but it’s not returning anything when viewed in Dev Tools bar the friendly name and icon.
When I run the JSON through JSONLint it comes back as valid JSON.
I’m not sure where to go from here.
I have now resolved this.
For anyone with a similar issue, this may help.
I changed the received MQTT message being sent by my router to:
{
"GPS": "[54.xxxxxx,-1.xxxxxx]"
}
This makes it easier to handle.
My MQTT sensors are now:
- sensor:
- name: Latitude
unique_id: latitude
state: >
{% set lat = states('sensor.gps_coordinates') %}
{{ lat[+1:10] }}
- sensor:
- name: Longitude
unique_id: longitude
state: >
{% set long = states('sensor.gps_coordinates') %}
{{ long[-10:-1] }}
Then I made an automation:
description: Sets HA location using GPS data from Teltonika
mode: single
trigger:
- platform: time_pattern
minutes: /1
condition: []
action:
- service: homeassistant.set_location
data_template:
latitude: '{{ states('sensor.latitude') }}'
longitude: '{{ states('sensor.longitude') }}'
alias: Set location
HA location now gets updated every 5 minutes.
I hope this helps someone out.
Sorry really new all of this so please forgive my ignorance.
I am attempting to do what you have done here but running into an issue. Maybe you can help.
I am confused about what all should go in the configuration yaml to support the sensors you talked about. Can you provide the whole section with that data?
I will have a script running on a host inside my mobile rv.
That grabs gps data from my router/gps api.
It will then publish a mqtt message in this format like I think you have it.
{
"GPS": "[4x.xxxxxx,-90.xxxxx]"
}
This one seems to work as expected like this in the configuration.yaml
mqtt:
sensor:
- name: "GPS Coordinates"
unique_id: gps_coordinates
state_topic: "titan1c_location"
value_template: "{{ value_json.GPS }}"
json_attributes_topic: "tiitan1c_location"
json_attributes_template: "{{ value_json.GPS | tojson }}"
Which captures data
[4x.xxxxxx,-90.xxxxx]
I can’t get your other section to work. It keeps throwing errors for me.
- sensor:
- name: Latitude
unique_id: latitude
state: >
{% set lat = states('sensor.gps_coordinates') %}
{{ lat[+1:10] }}
- sensor:
- name: Longitude
unique_id: longitude
state: >
{% set long = states('sensor.gps_coordinates') %}
{{ long[-10:-1] }}
What errors are you seeing?
Where have you put the sensors?
I’m assuming from your post that you are still using configuration.yaml and have not split out your config. If so, they need to be under template:
template:
- sensor:
- name:
etc, etc.
Sorry like I said super noob over here…
Ok I broke out the mqtt and sensor to their own. The url they provivded isn’t helping me much for this dictionary stuff
configuration.yaml
sensor: !include sensors.yaml
mqtt: !include mqtt.yaml
mqtt.yaml seems to still be working after migration
sensor:
- name: "GPS Coordinates"
unique_id: gps_coordinates
state_topic: "titan1c_location"
value_template: "{{ value_json.GPS }}"
json_attributes_topic: "titan1c_location"
json_attributes_template: "{{ value_json.GPS | tojson }}"
sensors.yaml
- platform: template
sensors:
name: Latitude
unique_id: latitude
state: >
{% set lat = states('sensor.gps_coordinates') %}
{{ lat[+1:10] }}
- platform: template
sensors:
name: Longitude
unique_id: longitude
state: >
{% set long = states('sensor.gps_coordinates') %}
{{ long[-10:-1] }}
Errors
2024-05-12 07:45:58.177 WARNING (SyncWorker_0) [homeassistant.loader] We found a custom integration hacs which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2024-05-12 07:45:58.177 WARNING (SyncWorker_0) [homeassistant.loader] We found a custom integration frigate which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2024-05-12 07:45:58.845 ERROR (MainThread) [homeassistant.config] Invalid config for 'sensor' from integration 'template' at sensors.yaml, line 3: expected dictionary for dictionary value 'sensors->name', got 'Latitude', please check the docs at https://www.home-assistant.io/integrations/template
Invalid config for 'sensor' from integration 'template' at sensors.yaml, line 4: expected dictionary for dictionary value 'sensors->unique_id', got 'latitude', please check the docs at https://www.home-assistant.io/integrations/template
Invalid config for 'sensor' from integration 'template' at sensors.yaml, line 5: expected dictionary for dictionary value 'sensors->state', got "{% set lat = states('sensor.gps_coordinates') %} {{ lat[+1:10] }}\n", please check the docs at https://www.home-assistant.io/integrations/template
2024-05-12 07:45:58.845 ERROR (MainThread) [homeassistant.config] Invalid config for 'sensor' from integration 'template' at sensors.yaml, line 10: expected dictionary for dictionary value 'sensors->name', got 'Longitude', please check the docs at https://www.home-assistant.io/integrations/template
Invalid config for 'sensor' from integration 'template' at sensors.yaml, line 11: expected dictionary for dictionary value 'sensors->unique_id', got 'longitude', please check the docs at https://www.home-assistant.io/integrations/template
Invalid config for 'sensor' from integration 'template' at sensors.yaml, line 12: expected dictionary for dictionary value 'sensors->state', got "{% set long = states('sensor.gps_coordinates') %} {{ long[-10:-1] }}\n", please check the docs at https://www.home-assistant.io/integrations/template
2024-05-12 07:45:59.497 WARNING (MainThread) [homeassistant.helpers.frame] Detected that custom integration 'hacs' accesses hass.components.frontend. This is deprecated and will stop working in Home Assistant 2024.9, it should be updated to import functions used from frontend directly at custom_components/hacs/frontend.py, line 68: hass.components.frontend.async_register_built_in_panel(, please create a bug report at https://github.com/hacs/integration/issues
The Latittude and Longitude sensors need to be under template: NOT sensors.
Confusing, I know.
You need an include for template and a template.yaml file.
1 Like
Did you get this working?