Motorized Waterspray project

Hi, I am on the way to build a motorized waterspray device, to control the spray on a plant remotely via homeassistant and mqtt.

What I want to do is let the NodeMCU device to subscribe to the mqtt message published via HA, so that for example, if the device get the payload as “spray” the motor sprays water.

What I am stuck now is the way I publish a mqtt on HA in a timely and a correct manner.

The device will go deep sleep to save battery and will only connect to Wifi, say every 10minutes and can be set to be active for 1minute, meant for the duration for the spray, and disconnects from Wifi, and goes deep sleep again, and so the cycle continues.

During the above 1 minute timeframe, I want to have the device to check for a mqtt payload, and determine whether the water should be sprayed or not, accordingly
to the published message from HA. To accomplish this, the arduino sketch on the device I have a clue, but for the HA part, I am kind of lost.

Ultimately, I want to use a switch on ios’s Home app, so that when I press a Spray button,
the device will spray during the next 10 minute interval, and the Spray button automatically, grays out after finishing the spray.

This is basically my first project on Mqtt. I understand the concept of Mqtt,
the above device I made is equipped with a temperature sensor, and is publishing
a temperature payload so that HA can monitor.

What I have problem now is controlling from HA, so If someone can point me
to a good source of publishing mqtt from HA, that will be appreciative.

Thank you in advance.

This is the action I use in one of my automations to publish. Hope it helps to figure out what you need.

  action:
  - data:
      payload: 380
      topic: cmnd/tasmota_7569D8/PulseTime1
    service: mqtt.publish

Hi Maarten,

Thank you for your guidance, appreciate that.

After some refined google search, I was able to find,

as a good reference of what I want to achieve.

It is suggesting to use MQTT switch from HA, and is probably more a complete solution,
but your way seems more simple to implement as I can trigger an automation with a
button from Home.app.
In your example though, I am thinking how to grey out the button automatically after
the completing the pulsetime of 380. Otherwise in my case, it will keep waterspraying
until I manually turn off the automation…

Thanks a lot!

If you publish a message with the retained flag set, then the device will receive it as soon as it wakes and connects to the broker.

Thank you tom for the insight.

For a trial, I have in configuration.yaml wrote a switch as,

  - platform: mqtt
    unique_id: waterspray_switch
    name: "Waterspray Switch"
    state_topic: "waterspray/switch"
    command_topic: "waterspray/switch/set"
    payload_on: "ON"
    payload_off: "OFF"
    state_on: "ON"
    state_off: "OFF"
    optimistic: true
    qos: 0
    retain: true

This will give me a switch on HA like: Waterspray Off / On.
(Dont know the way to correctly attach screenshot JPG/PNG images, sorry…)

If I press Off, listening to waterspray/switch/set is “OFF”,
and for On, waterspray/switch/set is “ON”.
However retain is seen false, even it is set true in the above config.
Am I doing anything wrong here?

Thank you.

Resize the image to a sensible size then drag and drop it into the message box (right here).

Please read point 11 here in this link then edit your post so we can read the config you have posted: How to help us help you - or How to ask a good question

But it looks like you have it correct. How are you viewing the messages in the broker?

Thank you for your suggestion. I have edited the code to appear correctly.

I am viewing the messages through the configure screen in the MQTT integration of HA.
(which was once in the developer tools tab on earlier versions of HA.)

Edit: And it seems the message is retaining correctly, if I start/stop listening to the message at the right timing.

So understood the way it works to publish on HA side, now it looks like I can implement the code on the device side , to accomplish what I am planning for.

Thank you very much!