Populate MQTT Device Attributes from State

Greetings,

I’ve manually configured MQTT device. For one of the sensors, I’ve used the device attribute, that allows to add additional information about the device. Is there a way to populate device attributes (e.g. sw_version) using the state of another sensor?

In my case, I’d like to update the ‘sw_version’ using the ‘sensor.tesla_version’.

MQTT YAML:

binary_sensor:
  - name: Health Status
    unique_id: tesla_health
    state_topic: "teslamate/cars/1/healthy"
    payload_on: "true"
    payload_off: "false"
    icon: mdi:heart-pulse
    availability:
      - topic: "teslamate/cars/1/state"
    entity_category: diagnostic
    device:
      name: "Tesla"
      identifiers: tesla_starship
      model: "Model 3"
      manufacturer: "Tesla"
      hw_version: "Full Self-Driving Computer"
      suggested_area: "Outside"

sensor:
  - name: Version
    unique_id: version
    state_topic: "teslamate/cars/1/version"
    entity_category: diagnostic
    icon: mdi:numeric
    device:
      identifiers: tesla_starship

Thanks!

I can think of a hack to build a HASS Discovery block of JSON from the version sensor, then publish to MQTT from HASS, which then injests the auto-discovery config and configures the main Tesla entity replacing the local YAML config.

Here’s a mosquitto_pub example to get you started hacking together a proof of concept:

HASS changes immediately on receiving the publish.

Another example:

I’ve published data to MQTT from HASS automations but not discovery - should work as all are just MQTT payloads.

Don’t know if a more Python-esque API-based solution exists though.

If this helps, :heart: this post!

1 Like

Thanks @FloatingBoater!

I ended up creating an automation that did the trick:

alias: Update Tesla Attributes
trigger:
  - platform: state
    entity_id:
      - sensor.tesla_version
      - sensor.tesla_model
      - sensor.tesla_trim_badging
      - sensor.tesla_display_name
      - sensor.tesla_last_status_change
      - sensor.tesla_health_status

action:
  - service: mqtt.publish
    data:
      topic: homeassistant/binary_sensor/tesla_health_status/config
      retain: true
      payload: >-
        {
          "name":"Health Status",
          "state_topic":"teslamate/cars/1/healthy",
          "payload_on":"true",
          "payload_off":"false",
          "unique_id":"health_status",
          "icon":"mdi:heart-pulse",
          "entity_category": "diagnostic",
          "device_class": "connectivity",
          "device":{
            "identifiers":["tesla_starship"],
            "connections":[],
            "manufacturer":"Tesla",
            "model":"Model {{ states('sensor.tesla_model') }} ({{ states('sensor.tesla_trim_badging') }})",
            "name":"Tesla",
            "suggested_area": "Outside",
            "sw_version":"{{ states('sensor.tesla_version') }}"
          }
        }

Wow - that was a quick hack!

Nice :man_mage: