MQTT Switch Payload Configuration Success Story

I recently had a fun challenge configuring an MQTT switch payload for my sprinkler system. Let me share my experience in case it helps others!

I wanted to automate my sprinkler valve using MQTT and Home Assistant. The goal was to set the watering time dynamically using an input number. I set up the switch with a payload that multiplies the input number by 60 to convert minutes to seconds. Here’s the configuration I used:

yaml
switch:

  • platform: mqtt
    unique_id: “sprinkler0”
    name: “Front Garden Sprinkler”
    state_topic: “outside/lawn/status”
    command_topic: “outside/lawn/action”
    payload_on: >
    {zone: 0, time: {{ states(‘input_number.how_long’) | multiply(60) }} }
    payload_off: “{zone: 0, time: 0}”
    state_on: “Zone 0 added”
    state_off: “Zone 0 off”
    qos: 0

At first, I was puzzled because the payload wasn’t being parsed correctly—it sent the template text instead of the calculated number. After some research, I realized I needed to ensure the template was properly rendered. Testing in the Template section of Developer Tools confirmed the template worked, but it wasn’t being processed in the payload. I discovered that wrapping the payload in a JSON object with proper formatting was the key.

Finally, I adjusted the payload formatting, and voilà! The sprinkler now runs for the exact time I set. It’s amazing how a small tweak can make such a difference. This experience taught me the importance of careful payload formatting and template testing in MQTT integrations.

If anyone else is tackling similar automation projects, feel free to reach out! I’d love to hear about your experiences and tips. Happy automating! :ear_of_rice::droplet: