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?
Wanna use some of the values to be visualized at my smart home dashboard.
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:
Create an Output (MQTT Publish: Value) and set all the MQTT server settings.
Create a Function (Conditional Controller).
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.
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:
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.
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.
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
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
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
I had some time to check, so i did the following steps:
Upgrade
Add a functional controller
copied your code as a conditional statement (plain and with “my” Output ID)
selected the measurement as a condition
selected the MQTT Hub with credentials etc as an action
But i get this notification;
pi/Mycodo/mycodo/user_python_code/conditional_85b8b59a-0395-4098-ae97-0570cd8a2b55.py:18:4: R0201: Method could be a function (no-self-use)
Error: Modify Conditional: pylint returned with status: 8
This is just a warning because you made no reference to “self” in the function. Warnings are not always an issue, such as this case. The pylint that runs on the code will show a number of warnings related to minor things such as this. What’s important is that the code runs.
Though, using the output_on() function is obsolete since there is now an MQTT Function Action that you can more easily use to publish numerical or string values.