Multi-option MQTT switch

Is there any way to configure a multi-option MQTT switch?

I have a night light that I’ve built using a raspberry pi, a temperature sensor and some APA102 LEDs. At the moment it has 3 light modes - off, temperature (colour set depending on room temperature) and rainbow (slowly changing colours). It has a button which toggles between the modes and it also sends the temperature as an MQTT message.

I’m wondering if I can configure HA to send MQTT messages to set a particular state rather than a simple on/off?

If all your wanting HA to do is send an MQTT message to do what your switch is doing then I don’t see how this couldn’t be implemented similarly to how we use MQTT with our 433mhz devices. I would set up your pi device to listen for a topic/command in MQTT for each mode. It would be treated by HA as a momentary switch to send the MQTT to turn your device to one of the modes (off/temp color/rainbow). This would look like three switches in HA. Then you could script HA to change the mode how you would like.

We might need more info to answer more correctly. Like why are you sending the temp via MQTT? Are you wanting HA to react to the temp?

I would say an MQTT sensor to receive messages from the night light and an input select to set the mode combined with some automation to send the MQTT mesage back to the night light when the input select changes or change the input select when the night light mode changes could act as an MQTT switch with multiple modes.

1 Like

I have a similar configuration setting the state of an eTRV valve. I have this to choose the state in the gui


      input_select:
        bedroom_trv_state:
          name: Bedroom TRV Valve State
          options:
            - Closed
            - Normal
            - Open
          initial: Open

Then you have to trigger an automation to send the mqtt message with the payload.

I use appdaemon, so my (severely edited) code is

def initialize
        self.listen_state(self.select_changed, self.input_select)

def select_changed(self, entity, attribute, old, new, kwargs):
        if new == "Open":
            newstate = "0"
        elif new == "Closed":
            newstate = "1"
        elif new == "Normal":
            newstate = "2"
        else:
            self.error("Invalid new state {}".format(new))
            return

        self.log("Sending mqtt message topic {} with payload {}".format(
            self.valve_state_topic, newstate),
            level = "DEBUG")

        self.call_service("mqtt/publish", topic = self.valve_state_topic,
                payload = newstate, qos = "2", retain = "true" )


My YAML is very poor, but if you want to use that instead, I think you need a state trigger with an action to call the mqtt publish service, something like this

automation:
  trigger:
    platform: state
    entity_id: input_select.valve
  action:
        - service: mqtt/publish
      data:
         - topic: 
         - payload:

But my YAML is not up to filling in the blanks. You will probably need a template to fill in the payload. Hopefully this gives you a start.

Perfect stuff, I’ve put my solution below, however I seem to have to repeat the automations for each case. Is it possible to pull the payload data from an MQTT trigger and then use that in the action?

input_select:
  nightlight:
    name: Night Light
    options:
      - "Temperature"
      - "Rainbow"
      - "Off"
    initial: Temperature
    icon: mdi:weather-night

automation set_nightlight:
  alias: Set night light function
  trigger:
    platform: state
    entity_id: input_select.nightlight
  action:
    service: mqtt.publish
    data:
      topic: "home/nursery/nightlight/light/set"
      payload_template: "{{ states('input_select.nightlight') }}"

automation nightlight_input_select_temperature:
  alias: Get night light function
  trigger:
    platform: mqtt
    topic: "home/nursery/nightlight/light"
    payload: "Temperature"
  action:
    service: input_select.select_option
    data:
      entity_id: input_select.nightlight
      option: "Temperature"

automation nightlight_input_select_rainbow:
  alias: Get night light function
  trigger:
    platform: mqtt
    topic: "home/nursery/nightlight/light"
    payload: "Rainbow"
  action:
    service: input_select.select_option
    data:
      entity_id: input_select.nightlight
      option: "Rainbow"

automation nightlight_input_select_off:
  alias: Get night light function
  trigger:
    platform: mqtt
    topic: "home/nursery/nightlight/light"
    payload: "Off"
  action:
    service: input_select.select_option
    data:
      entity_id: input_select.nightlight
      option: "Off"

That is beyond my YAML knowledge. Hopefully, someone else may know.

It is.

Brilliant - completely missed that bit, so I’ve managed to simply things nicely…

input_select:
  nightlight:
    name: Night Light
    options:
      - Temperature
      - Rainbow
      - "Off"
    initial: Temperature
    icon: mdi:weather-night

automation set_nightlight:
  alias: Set night light function
  trigger:
    platform: state
    entity_id: input_select.nightlight
  action:
    service: mqtt.publish
    data:
      topic: "home/nursery/nightlight/light/set"
      payload_template: "{{ states('input_select.nightlight') }}"

automation set_nightlight_input_select_temperature:
  alias: Get night light function
  trigger:
    platform: mqtt
    topic: "home/nursery/nightlight/light"
    #payload: "Temperature"
  action:
    service: input_select.select_option
    data_template:
      entity_id: input_select.nightlight
      option: "{{ trigger.payload }}"