MQTT template for switches

Hi All. I have a UniPi neuron device in my home that I would like to control using MQTT from HASSIO. Since UniPi does not have native MQTT capabilities I am writing a python script to translate MQTT coming from HASSIO to REST and Websocket call on the UniPi. That works OK now for an absolute python and MQTT beginner. I’ll share when I have a working and stable script.

Since I want to keep the config at the UniPi to a minimum and use HASSIO to set everything I send the UniPi circuit (example; “circuit”: “1_02”) that needs to be switch in the MQTT payload in JSON format. That works great, and my lights (analogue output to switch leds with 1-10v) get switched. I have configured the python script on the UniPi to confirm the light went on by sending back a MQTT message with the original payload to the state topic.

When I configure a MQTT template in HASSIO and use command_on_template to configure a “light” that works. My payload is send and when I return a message with that payload my light is switched / dimmed in the HASSIO GUI.

The UniPi also has a lot of Digital Outputs / Relay Outputs. I tried to configure an MQTT template for those switches, but that does not work. I read here (https://github.com/home-assistant/home-assistant/issues/6623, a bit to the bottom) that mqtt_template does not work for switches as such in the hassio config.

The MQTT return message for the relay that is expected by HASSIO is “ON”, not ‘{“state”: “on”, “circuit”: “1_01”, “dev”: “relay”}’ . The MQTT template for the lights does expect this return message.

I copied the mqtt_template to the lights part of my config and that does work.

Now the question, is this per design, not yet implemented or a bug? I can plan building my script based on this answer. Personally I think it would be great if all MQTT config would work in the same way (so templates work on all types of devices).

Thanks for all the hard work all, keep it up!

1 Like

I got the above fixed and I kinda build a framework that sends MQTT messages based on UniPi Neuron inputs and also switches UniPI Neuron outputs based on MQTT received messages.

It’s a python script that you can run on the UniPi with Evok Software installed (or somewhere else for that matter).

Warning, I’m not a real programmer / scripter so you might find some code that is not all to neat. Happy to receive updates / tip / enhancements. I’ll be extending and improving this myself too.

Code is here; https://github.com/matthijsberg/unipi-mqtt/

Hi Matthijsberg

How are you getting on with the unipi in your HA, have you progressed any further on this since your last post? I’m looking into getting a unipi plc for my new house I’m building but it doesn’t look that simple to integrate it into HA. I was hoping to be able to use the Modbus component but I really am shooting in the dark here as I’m an absolute newbie plc’er.
I’m thinking I may have to bite the bullet, buy one and play with it to see if it is suitable but it may be an expensive experiment…

Hi Dieter,

I have it working to my needs pretty well. I updated the github a few times and the script now stays running continuously and sends and receives MQTT messages to my MQTT broker running next to HA. I’m sure there are more use cases that can be done with the functions implemented of with new functions, but here is what I do with it;

  • PIR sensors via a digital in (Esylux PD-C3601/8 etc.) that work on 12-36 volt and give me a pulse signal every 10 seconds to trigger motion and presence.
  • LUX / light intensity via the above Esylux sensor via Anlog In
  • Magnetic contacts in doors and windows via a Digital In
  • Led Lights via Analog out (there are 0-10v led drivers like from LTech that you can feed here, make sure you use the 0-10 and not the 1-10). Nice thing is that all lights on the same 0-10 feed trigger at the same time
  • Bell, I use a bell that I use a Relay for to trigger. It’s a Friedland and it rings on a On and Off 1 time. So I can specify the amount of rings, so rear door is 2 rings, front door is 3, etc.
  • Bell presser and light switch; just a simple presser that on a push triggers a Digital In (pulse switch, not make)
  • 1 wire sensors for temperature, humidity, light intensity. Nice thing of 1-wire is that it’s a bus that can have multiple sensors on 1 input, so it’s a bit cheaper to use.
  • Roof window via digital out (Velux) though there is an API now too. I have a normal wall switch instead of the Velux thing, it’s too big to fit my other switch materials from Jung.

I can help you out a bit if you need some changes, but I’m not a coder, so no promises here :wink:

It should work on the smaller boards too, so you can build a POC if you want to. I use the dinn rail mounted stuff, with dinn rail bases led drivers, fuses, power supplies, etc.

Hope this helps, cheers!