MQTT sensor value adjustment

TL;DR: Is it possible to change the value template of an auto discovered mqtt sensor?

I have a Blitzwolf SHP13 and using Zigbee2mqtt and auto discovery it’s added as a device in home assistant (so no entry in my configuration.yaml).

Sadly the sensor reports power way too high so I want to adjust this sensor.

I’ve been able to create an adjusted sensor with the following yaml:

  - platform: "mqtt"
    name: "0x842e14fffe1269ac_power_adjusted"
	state_topic: "home/BW-SHP13-Solar"
	availability_topic: "home/bridge/state"
	value_template: "{{value_json.power *0.84}}"
	unit_of_measurement: "W"

But this results in 2 sensors, the original 0x842e14fffe1269ac_power and the newly added 0x842e14fffe1269ac_power_adjusted.
If I leave out the _adjusted part and just name the new sensor 0x842e14fffe1269ac_power it’s automatically renamed to 0x842e14fffe1269ac_power_2.

Is there a way to adjust the original sensor without adding a new one? This would be much cleaner and also won’t mess up my historical charts in Grafana too much :slight_smile:

Use an MQTT client, like MQTT Explorer, to inspect the discovery topic for your power sensor. The topic may look something like:

homeassistant/sensor/0x842e14fffe1269ac_power/config

Once you find the topic, copy its discovery payload because you will use it to create a script.

Here’s a script designed to create a sensor via MQTT Discovery:

#script:
  revised_power_sensor:
    alias: "Overwrite default MQTT Discovery configuration for power sensor"
    sequence:
      - service: mqtt.publish
        data:
          topic: homeassistant/sensor/0x842e14fffe1269ac_power/config
          retain: true
          payload: >
            {
              "name": "0x842e14fffe1269ac_power",
              "state_topic": "home/BW-SHP13-Solar",
              "value_template": "{{value_json.power * 0.84}}",
              "availability_topic": "home/bridge/state",
              "unit_of_measurement": "W",
              "unique_id": "something_goes_here"
            }

What’s important is that you modify this script to contain the same information you copied from the discovery payload. The topic must be exactly the same as well as the name and especially unique_id otherwise this script will create an entirely new sensor (and fail to overwrite your existing one).

Basically, you want this script to use the same information to create a sensor like what zigbee2mqtt does except with a different value_template.

After you execute the script, it will publish its payload to the discovery topic and Home Assistant, via MQTT Discovery, will update the existing sensor’s configuration. Your revision will persist unless zigbee2MQTT re-publishes its original discovery payload (thereby undoing your changes).

… or just continue to use two sensors like you currently do.