Create sensor from MQTT

Hi all,
I am trying to create a sensor from an MQTT entity from the topic in the screenshot:

I have tried the following:

sensor:
  - platform: mqtt
    name: my_grid_export_today
    state_topic: "SunGather/A200506072"
    value_template: "{{ value_json.daily_export_energy }}"
    unit_of_measurement: "kwh"

but that is just giving me an error.
Could anyone be so kind as to enlighten me with the correct syntax please?

See here:

Note top-level mqtt: these days. Always refer to the official documentation rather than old posts / tutorials etc. HA is a moving target :slight_smile: .

Your config (mqtt as a switch platform) was deprecated in 2022.6 and removed in 2022.12.

Also note that it’s kWh with a capital W, and if this is your first top-level use of mqtt in your config, you will need to do a full restart to load it in.

Thanks Troon,
I did look at the official documentation and searched through the previous posts which is how I came up with the syntax. Not sure why I wasn’t getting an error about invalid config if the syntax was completely wrong.
So are you saying that the below should work? Do I have the right state_topic? Is that the correct way to get the value using value_json (I’ve seen value.json as well)?

mqtt:
  sensor:
    - name: my_grid_export_today
      state_topic: "SunGather/A200506072"
      value_template: "{{ value_json.daily_export_energy }}"
      unit_of_measurement: "kWh"

When it’s such a moving target, I’m not sure you appreciate how difficult this is for someone who loves HA but is not a developer.

I do appreciate your help though!!

Thanks!

It’s been almost two years since the official documentation showed the syntax you’ve used. I’m fully aware how misleading it can be to consider old forum posts as a source of information, though.

The syntax wasn’t invalid, you’re just referencing a sensor platform that doesn’t exist. I’d imagine there will be an error in your logs about this.

Looks good to me. Try it!

Ahh, makes sense then. Just waiting for the sun to come up to test it out - it’s a solar generation sensor :slight_smile:
Thanks for the info.

Hi Troon,

Thanks for the advice - that sensor worked perfectly!

1 Like