How to template from MQTT?

Hi there,

I have some MQTT data that comes in from a single topic: “Sofar2mqtt/state” and looks like this if I use MQTT fx to subscribe to the topic from my PC:

{"running_state":2,"grid_voltage":2485,"grid_current":530,"grid_freq":5000,"grid_power":0,"battery_power":125,"battery_voltage":5070,"battery_current":2454,"batterySOC":93,"battery_temp":29,"battery_cycles":107,"consumption":401,"solarPV":520,"solarPVAmps":2108,"today_generation":1077,"today_exported":67,"today_purchase":259,"today_consumption":680,"inverter_temp":50,"inverter_HStemp":45}

How do I set up sensors to take the relevant data? I’m thinking along some kind of value template but I dont know how to go about that.

Thanx for any help

e.g.

mqtt:
  sensor:
    - name: "Battery Current"
      state_topic: "Sofar2mqtt/state"
      unit_of_measurement: "Amps"
      icon: "mdi:current-dc"
      value_template: "{{ value_json.battery_current }}"
1 Like

Thanx @tom_l

How do you make it look so easy! I read the ‘MQTT Sensor’ instruction but even with the solution I couldn’t see how to get there