I want HA to remember brightness of my lights when I turn them back on. How?

My lights are controlled through MQTT and they accept the following commands: ON, OFF, LASTSTATE or a value between 0-100 for the brightness. “LASTSTATE” will turn them on at the last brightness and will report back the brightness too.

Now, how can I make HA send the “LASTSTATE” command when I hit the button in HA? It seems like it’s always sending “100”

If I can configure HA to remember the last brightness, that’s fine too. In that case it will nog be using the “LASTSTATE” mqtt command.

I now have this:

on_command_type: first
payload_on: "LASTSTATE"
payload_off: "OFF"
state_value_template: "{% if value_json.state|int > 0 %}LASTSTATE{% else %}OFF{% endif %}"

When I turn the light on it sends LASTSTATE and when I turn it off it sends OFF. So far, so good.
The problem is, when I change brightness in HA it sends LASTSTATE followed by the brightness.

It looks like on_command_type only has 3 options: last, first or brightness.

None of these options fit my needs? Because I only need it to send the payload_on (LASTSTATE) if the light is off but not at all when the light is on.

I saw mqtt light default schema has ability to change order os sending brightness and on command. not sure it fits your needs though. refer docs and try

We can change the order of the commands with on_command_type (which can be “first” , “last” or “brightness”.)

Both “first” and “last” will still send both commands but that’s not what I need. “brightness” will only send the brightness but HA does not remember the last state so it will always send 100 if the light is off.

What I need is:

  • If the light is off, and turned on: send “LASTSTATE”
  • If brightness is set: send brightness only (without sending LASTSTATE)

It appears that you need fine-grained control of what is published. I suggest you consider using a Template Light instead of an MQTT Light. You have complete control over what the turn_on, turn_off and set_level options should do (i.e. use the mqtt.publish service call to publish exactly what you want).

I’m working with mqtt auto discovery so that would require a big change in the whole setup :frowning:

I do wonder, is there a way to make HA remember the last brightness state? I was looking for that initially but didn’t find a good solution.

Not with MQTT Light; none of the three types of on_command_type you tried worked the way you want. Plus you don’t want to use a Template Light because you also want the solution to use MQTT Discovery.