I’ve been diving into the world of smart gardening lately, and one challenge I wanted to tackle was getting accurate soil moisture readings from my Zigbee soil sensor. The sensor reads 0% when the soil is dry and 65% when it’s completely wet, but I wanted to map these readings to a more intuitive 0-100% scale for easier automation in Home Assistant.
At first, I was a bit stuck on how to approach this. I knew I needed to create a template sensor to handle the calculations, but I wasn’t entirely sure how to structure the math. After some research and trial and error, I figured out a way to scale the readings properly.
Here’s what I came up with:
yaml
sensor:
- platform: template
sensors:
soil_moisture_scaled:
friendly_name: “Soil Moisture (Scaled)”
unit_of_measurement: “%”
value_template: >
{% if state_attr(‘sensor.soil_sensor_1_humidity’, ‘value’) is not none %}
{{ (state_attr(‘sensor.soil_sensor_1_humidity’, ‘value’) / 65) * 100 }}
{% else %}
0
{% endif %}
icon_template: mdi:water-percent
This template takes the raw sensor reading, divides it by the maximum value (65%), and then multiplies by 100 to get a percentage. Now, my automation can trigger watering when the scaled reading drops below a certain threshold, making it much easier to manage my plants.
It’s been a great learning experience, and I’m excited to see how this setup performs over time. If anyone has tips for further optimizing this or other sensor integrations, I’d love to hear them!