How do i pass light name or something in json message

Hello,
I’m trying to pass the device name or “light”:1 in the json message.

#dimmer1
- platform: mqtt
  name: "dimmer one Light"
  schema: json
  command_topic: "backarea/set/light"
  state_topic: "backarea/set"
  brightness: true
  qos: 1
  command_on_template: '{"light":1, "state": "ON", "brightness": {{ brightness }}}'
  command_off_template: '{"brightness": "0"}'
  retain: false

When I try turn on the light it is not passing the light:1 message.
If I cant pass that through, can I pass the device name ?

I dont have MQTT lights anymore but shouldn’t you be using schema: template to use command_on_template?

That made it work!
But now the icons are not sliders.
How can I put it back?

Add this:

homeassistant:
  customize_domain:
    light:
      assumed_state: false

What @DavidFW1960 will work but that wont stop your lights going out of sync. e.g. if they are turned on/off outside of HA the state will not be updated in HA. This is why it’s an on and off button by default so you can send a on command to something HA thinks is on even though it is really off.

The better solution would be to spend the time adding state_topic and state_template to your light configuration that way if the light changes state it is always in sync with HA. This will then display as a toggle on the interface instead of the two buttons.

The code didn’t work, did I do it correctly?
I will have a look in that method… I’m still coding the nodemcu to work perfectly.
Also it seems that the brightness is not working.
It needs to pass via JSON message between 0-100… and atm it needs to have the light number with it… “light”:1

brightness_template is wrong. This should extract the brightness value from the state_topic.

command_on_template is wrong too. when you change the brightness though HA it is basically sending a on command with the brightness value. check the example here…

#dimmer1
- platform: mqtt
  name: "dimmer Light"
  schema: template
  command_topic: "backarea/set/light"
  state_topic: "backarea/set"
  qos: 1
#  command_on_template: '{"state": "ON", "light":0, "brightness": {{brightness | float | multiply(0.3922) | round(0)}}"}'
  command_on_template: >
    {"state": "ON", "light":0
    {%- if brightness is defined -%}
    , "brightness": {{brightness | float | multiply(0.3922) | round(0)}}
    {%- endif -%}
    }
  command_off_template: '{"state": "OFF", "light":0}'
  state_template: "{% if value_json.ison %}ON{% else %}OFF{% endif %}"
  #state_template: "{{ value.split(',')[0] }}"  # must return `on` or `off`
  brightness_template: "{{ value_json.brightness | float | multiply(2.55) | round(0) }}"

im using this code… is this correct?
atm when i toggle it on, no brightness gets sent via JSON,
if i move the brightness slider it turns on with the brightness amount ?

Looks good to me. If it works for you and operates in the way you want it to then yes

Unsure but going off all other lights I have used in HA that would be correct. Try installing https://mqttfx.jensd.de/ and connect to your MQTT server and subscribe to the topics and see what’s happening.
if you are looking for different brightnesses at different times checkout

atm when i toggle it on, no brightness gets sent via JSON,
if i move the brightness slider it turns on with the brightness amount ?

what i meant was, if i just turn the light on without defining brightness i want it to either go to 100% or last state. ATM if i just toggle the switch the JSON message only sends this : {“state”: “OFF”, “light”:1}, as you can see there is no brightness defined.
and within the switch itself, the brightness toggle is always at 0%

Also, Can you make the brightness toggle just adjust the brightness without sending the json message if its on off state. I prefer that it only sends the message when its actually on.

image
image

All my lights remember their last state so I guess not sending brightness with the toggle is deliberate. You could just put a else statement in on the on command like this;
Note: You posted an image, which i cant copy, so I copied the example from the HA docs to save trying to retype yours

command_on_template: >
    {"state": "on"
  {%- if brightness is defined -%}
    , "brightness": {{ brightness }}
  {%- else -%}
    , "brightness": 100
  {%- endif -%}
  }

My understanding is if its in a off state and you adjust the brightness you will get a on command_on_template with brightness defined. Perhaps it would help to know why you need this to help you workout the best way round it.