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…
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.
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.