Sending a value to the MQTT hub

Hi everyone,

I would like to send a value (calculated via Math) to a MQTT server.
So i add an “Output: MQTT Publish: Value” → But what do I have to include in the value field?

Or is there another way to publish values to the MQTT hub? :thinking:

Wanna use some of the values to be visualized at my smart home dashboard. :slight_smile:

1 Like

You bring up a use case that hasn’t been requested until now. So you want to calculate a value (presumably using some Input or other controller), then publish that value via MQTT? For all the possible combinations that Mycodo can be configured, I can think of only one method and it requires us to get a little creative with some Python code and a Conditional Controller Function. Actually, there are multiple if we’re going to use code, but I’ll show you the easiest.

I do think there is a lot of value in adding an MQTT Publish Action. And indeed, this has been discussed for Inputs, but it really can be applied to Function Actions as well:

Here’s how (theoretically) it would work:

  1. Create an Output (MQTT Publish: Value) and set all the MQTT server settings.
  2. Create a Function (Conditional Controller).
  3. With Python code, perform the calculations you were doing with the Math controller you mentioned. If you used Input measurements, for example, use any of the available Conditions to pull measurements to use with your calculation.
  4. Add the following code to the Conditional Statement, which will pass a value to the Output created earlier that will then pass the value to the MQTT server:
output_id = "a86e7f09-c7ce-47ac-9f36-38a8e943e4b6"
publish_value = 100
control.output_on(output_id, amount=publish_value)

Where output_id is set to the ID of your MQTT Value Output and publish_value is set to the value you want to publish. You can copy the ID of your Output to your clipboard by clicking the button next to the name of your Output on the Output page. This button will show a tool tip with your Output ID when hovered over the button.

Now, considering that this is not an easy way to use Mycodo, since it involves Python code, I see a great amount of value in adding an MQTT Action that can be easily used with Functions to pass a value. Considering the two topics, above, show a lot of interest in such a feature, It’s already high on my priority list and I hope to have it added for the next Mycodo release.

1 Like

I just added a new Function Action: MQTT Publish.

This will allow you to create a Conditional Controller and add from the Actions dropdown the MQTT Publish action. This isn’t currently released, so if you want to test the new feature, you’ll have to upgrade to master.

1 Like

Hi Kyle,

thanks for the fast reply :+1:

In my case, my smart home system is collecting data from all “subsystems” and provide it to two dashboards.
The Mycodo pi will get an own display at the aquaponic system, but it would be great to have the values also at one dashboard :slight_smile:

Yes, I’m using a cheap aliexpress pH-probe + ADS1115 device. So i have to calculate the correct pH value. Altas scientific ist hard to find in Germany :confused:

I’ll add a turbidity sensor and a flow meter in the next weeks. Turbidity should give me a hint, if algae starts to grow in the system and the flow meter if the pumps are working correctly.

Hope to have some time during next week, to give your solution a try. I’ll let you know :slight_smile:

1 Like