MQTT Json Switch

Hi there :grinning:
Im trying to figure out, how i can get an switch with MQTT and Json to work.
The code on the ESP32 is from the example of Bruh Automation for an WS2812B LED Strip.
Im using Hass,io with Mosquitto Addon and ESPHome.

I changed the code to work with ESPHome and it works fine. I´ve got the Light Card working with all featueres and effects, but the Light is for my 5 years old daughter and she cant read :roll_eyes:

So i want to create picture cards or somithing like that for every lighteffect, so she can change things by herself. I thougt its the best way to send an MQTT message in Json format by pressing the switch and recieve a message for the state and effect of the light.

SAMPLE PAYLOAD(for switching the LEDs):

 {
    "brightness": 120,
    "effect": "rainbow",
    "state": "ON"
  }

SAMPLE PAYLOAD(coming back form the ESP to HASS.io):

 {
    "state": "ON",
    "color": {
      "r": 255,
      "g": 100,
      "b": 100
    },
    "brightness": 120,
    "effect": "rainbow",
  }

This is the code from my Configuration.yaml but it doesn work. Can someone give me an hint please?

switch:
  - platform: mqtt
    name: "Treppe Rainbow"
    state_topic: "hochbett/treppe"
    command_topic: "hochbett/treppe/set"
    value_template: "{{ value_json.state }}"
    payload_on: '{"state":true,"efecct":"rainbow"}'
    payload_off: '{"state":"false","effect":"solid"}'    
    state_on: "true"
    state_off: "false"

In your two payload examples (one for controlling the light and the other for the light’s status) they both use ‘“ON”’ to represent the state. However, your template uses true and false. Why?

Try this:

switch:
  - platform: mqtt
    name: "Treppe Rainbow"
    state_topic: "hochbett/treppe"
    command_topic: "hochbett/treppe/set"
    value_template: "{{ value_json.state }}"
    payload_on: '{"state":"ON","effect":"rainbow"}'
    payload_off: '{"state":"OFF","effect":"solid"}'    

Thanks a lot!!!

This does the trick. Now its working :heart_eyes:

switch:
  - platform: mqtt
    name: "Treppe Rainbow"
    state_topic: "hochbett/treppe"
    command_topic: "hochbett/treppe/set"
    value_template: "{{ value_json.state }}"
    payload_on: '{"state":"ON","effect":"rainbow"}'
    payload_off: '{"state":"OFF","effect":"solid"}'    
    state_on: "ON"
    state_off: "OFF"

Glad to hear it’s working.

According to the documentation, the default value for state_on is ON so it isn’t necessary to specify it in the switch’s configuration (similarly OFF is the default for state_off). That’s why I excluded state_on and state_off in my example.

Please mark my post with the ‘Solution’ tag to make it easier for others to find (I can’t do it; only you, the author of this topic, can mark a post as ‘Solution’). A checkmark will appear next to the topic’s title and a link to the post, containing the solution, will appear below the first post in this thread.

I’ve tested it withou the state_on and state_off but that doesnt work. It doesnt show the state of the light. But withe the states it works fine :smile:

That’s very strange because I just performed a simple test and confirmed it works as stated in the documentation.

Here is the test switch:

  - platform: mqtt
    name: "test"
    state_topic: "test/switch"
    command_topic: "test/switch/set"
    value_template: "{{ value_json.state }}"

I used MQTT Explorer to publish this payload:

 {
    "state": "ON",
    "color": {
      "r": 255,
      "g": 100,
      "b": 100
    },
    "brightness": 120,
    "effect": "rainbow",
  }

to this topic:
test/switch

and the test switch’s state changed to on. That’s how it is supposed to work. The default value for state_on is ON so there’s no need to explicitly specify it in the light’s configuration.

I don’t understand why it doesn’t work for your light. :man_shrugging:

So now i hav another question :sweat_smile:
I have some seperate switches working fine to call different light effect. But for some of these effects you can choose a color. At the moment i have to click on the three dots on the top right corner of the standard light control, so that the color wheel etc. window pops up.
Is there a way to show this color wheel window by a lonf press on my effect switch (hold_action)?

I tried different something like this in a picture-elements card, but without success :roll_eyes:

  - entity: switch.treppe_juggle
    type: image
    tap_action:
      action: toggle
    hold_action:
      action: call-service
      service: more-info
      service_data:
        entity_id: light.treppenlicht
    state_image:
      'off': /local/icons/treppe_juggle.png
      'on': /local/icons/treppe_juggle.png
    state_filter:
      'off': brightness(70%)
      'on': brightness(150%)
    style:
      left: 40%
      top: 14%
      width: 20%

I suggest creating a new topic because this second question is about configuring the Lovelace UI and is unrelated to your first question. By posting it as a new topic (with its own title and tagged with the ‘Lovelace & Frontend’ category) it is more likely to attract the attention of people who know the answer.

You can ask one of the moderators to move your last post to a new topic or just copy/paste it to a new topic and then delete the original one.