Pass a variable from an AppDaemon app into an MQTT switch's "payload_on"

Hi all,

I’m updating my garden irrigation project now that it is in for the winter, trying to make it more robust. I would like to pass a variable (for duration) from an AppDaemon app into an HA MQTT switch’s “payload_on,” which controls an Arduino device.

What I do today (works perfectly)

In AppDaemon, I use “run_in” to run the On and Off functions in a defined number of seconds (valveOn and valveOff):

                self.handle = self.run_in(self.switchOn, valveOn, nextValve = nextValve)
                self.handle = self.run_in(self.switchOff, valveOff)

In MQTT I then send the messages to the device, e.g.:

    payload_on: '{"valve": "In2", "value": 1}'
    payload_off: '{"valve": "In2", "value": 0}'

What I would like to do

I would like the Arduino device to decide by itself when to turn a switch off, as then if the connection goes down the valve won’t stay open (I’ll tell you later :wink:). E.g., have MQTT send something like:

payload_on: '{"valve": "In2", "value": 1, "duration": VARIABLE}'

where VARIABLE is an integer passed from my AppDaemon’s “switchOn” function.

Any ideas would be appreciated.

cheers,
Sean

Can you show the switchOn and switchOff functions? Are you not sending the MQTT message from AppDaemon?

Sure:

    def switchOn (self, kwargs):
        allValves = str(self.args['valveGroup'])
        self.turn_off(allValves)
        thisValve = "switch.in"+str(kwargs['nextValve'])
        self.log("Valve {} ".format(thisValve))
        self.turn_on(thisValve)

    def switchOff (self, kwargs):
        allValves = str(self.args['valveGroup'])
        self.turn_off(allValves)

In my apps file, valveGroup is

valveGroup: group.irrigation_valves

where the group is defined in HA. I send the MQTT messages to the Arduino via payload_on and _off from HA MQTT switches.

cheers,
Sean

On a side note, thanks for the tip. I didn’t realize that you can send MQTT messages directly from AppDaemon. At this stage I would prefer my current method, but I’ll look into this.

cheers,
Sean

I can’t think of an easy solution of what you are trying to do without sending the message directly from AppDaemon. You could maybe write the value to a file, which you will then read from home assistant and incorporate somehow into your MQTT Switch, but I don’t know if this is even possible.

However I can help you with sending an MQTT message from AppDaemon, in case you decide to go this way.

Ok, thanks. I’ll try a few things and post again if I decide to go down that route. I can actually just hardcode it into the configuration.yaml file, but am hoping to keep all my irrigation variables in one place.

cheers,
Sean

The easiest way to do this (other than sending the MQTT messages from AppDaemon which you said you don’t want to do) is, instead of using self.turn_on() to turn_on the valve, fire an event (using self.fire_event()). Then, in HASS, trigger on the event and send the payload you’d like based on the event data.

Another option is to call HASS’s mqtt/publish service directly from appdaemon. This will go through HASS, so you won’t be using AD’s MQTT stuff, and then HASS will publish to the topic in the same way it normally would.

1 Like

Thanks, Daniel. I’ll have a look at those options. Can you give an example of how to call the service “directly from appdaemon?”

Edit: I think I see what you mean. I can call a service for light.turn_on(). I’ll check it out.

Daniel, the publish service option looks like the best for now. I’ve created a variable input_number.duration to hold the variable in HA. When I start irrigation, I run

self.set_value('input_number.duration', valveDuration)

I would prefer it to be part of payload_on inside the MQTT switch, but it doesn’t seem possible to include a template in payload_on. Thanks both of you for the help.

Hmm not sure if it is possible to template payload_on/off.

Anyway i think it would be asier to use HASS mqtt publish/sub as @swiftlyfalling suggested.

Yes, the problem with that is that I have multiple use cases: turn on from AppDeamon, from HA interface, and manual. I’m worried about race conditions, and making sure turning on from HA is exactly the same. I suspect I could do it that way, but with more complications.