Adding location ( latitude and longitude) attributes to an entitiy

Hey … we have added ’ attribute template’ … and it worked for sensor that are declared under platform template but it doesn’t work for sensor under platform_mqtt… here is the code we created for platform template sensor:

See code :

sensor:

  • platform: template
    sensors:
    analog_input_5:
    friendly_name: “Analog Input 5”
    value_template: ‘{{ states.sensor.hidden_current_state.attributes[“CurrentState”][“AnalogInput”][4][“Measure”].split(" ")[0] }}’
    unit_of_measurement: “°C”
    attribute_templates: ------------- THIS WORKS HERE! good!**
    latitude: ‘47.649328’**
    longitude: ‘-67.412194’**

but for sensor under platform_mqtt i can find the formulation to add the longitude and latitude attribute :

See code :

sensor temptrailer:
platform: mqtt
state_topic: ‘/devices/192.168.1.120/ai/0’
name: ‘Temperature Trailer’
attribute_templates: ----------------- ERROR!!!
longitude: ‘47.613777’
latitude: ‘-67.417687’
unit_of_measurement: ‘°C’
value_template: ‘{{ ((value_json * 50)/10000) | round(2) }}’

HOW DO WE ADD ATTRIBUTES FOR SENSOR DECLARED UNDER PLATFORM_MQTT??

Thank you!

Do you want this to also showup in HomeAssistant? Or you are only using it in NodeRed to have everything in one place?
In the latter case, you could just send the required message directly via MQTT. Just add your MQTT Broker and add any message you would like to.
I am not sure if the HA State can be appended like that as well, but worth a try.

Hey thanks man , but this is not the issue… there no nodered in the mix… I thought my question was clear but now i see that is is not.
My mqtt sensor only sends me the temperature value. That’s it. But i still would like to show this sensor on my HA map… i would like to be able to add some custom attribute to my sensor. Attributes that i add myself that dont come the mqtt message. Just adding some other attributes to a sensor. I was able to do it using template_attribute for the platform template sensor. But this line doesn’t work for a sensor declared under platform_mqtt… ??? I am looking for the right code to put there. Do you have an idea??

If you are not doing this with NodeRed, then I would suggest removing the NodeRed Category or to move the thread. For me this is shown in the NodeRed Subthread.

As for your problem, I dont know how to add the custom attributes to a MQTT sensor if these are not already in the mqtt message. I dont think thats currently possible. However, you could probably just turn it around and create a new template sensor and add the value from the MQTT Sensor to this with all the other custom attributes you desire.

1 Like

wow thanks man !! i worked perfectly !!! thousand thanks !

I tried to find this information many hours! Thank you brother.Now I will go to learn how to create template sensor! Many regards and thanks!!!

1 Like