Data analysis with Mqtt

Hello everyone,

I am new to home assistant after doing basic integration I would like to go further
and I started on a communication project between an arduino uno and Home Assistant which is on a Raspberry pi 4

I manage to communicate between the two perfectly but now I would like to know how to analyze my data and put some conditions.
Here is my script
mqtt:
sensor:
- name: “TankHeight”
state_topic: “my/topic”
unit_of_measurement: “m³”
icon: mdi:water
value_template: “{{ value_json.mytopic }}”

My problem has the line value_template I can’t get my value it tells me unknown
the error comes from this line because when I remove it I recover my value.

THANKS

Then leave it removed.

Are you sending json encoded data?

Show us the mqtt data message.

here is the code on my arduino

// MQTT client config
void callback(char* topic, byte* payload, unsigned int length) {
// Handle incoming messages
}

EthernetClient ethClient;
PubSubClient mqttClient(ethClient);

void setup() {
// Start serial communication
Serial.begin(9600);

// Initialize Ethernet Shield
Ethernet. begin(mac, ip);
Serial. print(“Connecting to Ethernet…”);

// Wait for Ethernet connection
delay(1000); // Wait for Ethernet Shield to initialize
Serial.println(“Ethernet Shield initialized”);

// Initialize MQTT client
mqttClient.setServer(server, 1883);
mqttClient.setCallback(callback);

// Connect to MQTT broker
while (!mqttClient.connected()) {
Serial.println(“Connecting to MQTT broker…”);
if (mqttClient.connect(“arduino-client”, “waterlevel”, “test”)) {
Serial.println(“MQTT broker connected”);
} else {
Serial. print(“MQTT connection failed, rc=”);
Serial.print(mqttClient.state());
Serial.println(“retrying in 5 seconds”);
delay(5000);
}
}
}

void loop() {
float value = 10.5;
char message[10];
sprintf(message, “%.1f”, value); // Convert float to string with one decimal place

// Publish message to MQTT broker
mqttClient.loop();
mqttClient. publish(“my/topic”, “105”);

// Wait before publishing next message
delay(10000);
}

No json there. Why are you wanting to use value_json?

As for the issue with the value_template line, it’s hard to say without more information. One thing you could try is to double-check that the value_json.mytopic path is correct and matches the JSON structure of your data. If you’re interested in diving deeper into data analysis, Power BI is a powerful tool that can help you visualize and analyze your data in new ways. It might be worth checking out some Power BI data analyst courses or tutorials to see if it’s something that could be useful for your projects. Hope that helps, and good luck with your project!

They aren’t sending json encoded data.