MQTT template light brightness control

Hi guys,

I have set up two mosfet controlled led strips with HA through Mysensors. I can switch the light on and off, so hardwarewise I am OK.
What I can not figure out for the life of me, is howto control the brightness of the lights!

  - platform: mqtt_template
    name: Nappali
    command_topic: "fonok/nappali/events"
    state_topic: "beosztottak/nappali/mqttstates/light/nappali_0_10/state"
    command_on_template: '{"event_data": {"domain": "light", "service": "turn_on", "service_data": {"entity_id": "light.nappali_0_10", "brightness_pct": "100"}}, "event_type": "call_service"}'
    command_off_template: '{"event_data": {"domain": "light", "service": "turn_on", "service_data": {"entity_id": "light.nappali_0_10", "brightness_pct": "0"}}, "event_type": "call_service"}'

The above works to switch the light on/off.
I have tried to add a brightness_template line to the config, but it does not seem to work.
These are the options I have tried to pass the brightness to the template:

(states.light.nappali.brightness)
{{states.light.nappali.brightness}}
"{{states.light.nappali.brightness}}"

"{{states.light.nappali.brightness|int}}"
"(states.light.nappali.brightness|int)"
"{{ (states.light.nappali.brightness|int) }}"
{{ (states.light.nappali.brightness|int) }}

None of them work and it is driving me crazy…

What am I screwing up?
tom

I think the problem is that the mqtt light template code has a problem. I was looking at that as part of fixing this bug and I found that the code only puts the brightness variable into the template inputs if the brightness is being changed. So you can’t use the on command as a brightness control. I think it should be fixed, and I posted a question in this thread but no on ever responded.

I don’t actually use these lights - I was just helping out to fix bug so it’s not really a priority for me. I’d suggest that you file a bug about it on github. It’s a pretty trivial fix but I didn’t want to make it without some user support for it.

Why are you making a separate MQTT light? The mysensors are support with their own component:

I can control the brightness indeed, because the turn on and the turn off command uses “brightness”:“100” and “brightness”:“0”. If I change these values to anything else, they work, the only thing I can’t do is change them dinamically.
So I don’t it is a bug, but something I do wrong

good question. This is what you get when you don’t tell everything about your setup. Sorry.
So the mysensors is connected to a Pi, and the problem I’m having is on another machine. I am trying to setup a master/slave environment, which seems to work, except the brightness setting.

That’s my point - it looks like you’re setting the values explicity to 0 and 100 in the template so that’s what it outputs (so it works). But if you want it the template to have the brightness you pass to the light (via automation or the web front end), then something like this is required (and it doesn’t work right now):

 command_on_template: "{{ brightness|d }}

The attributes like brightness are usually found at something like
{{ states.light.nappali.attributes.brightness }}

The attributes like brightness are usually found at something like
{{ states.light.nappali.attributes.brightness }}

Sure - but the docs for the light specifically say you can use “brightness” as a template argument but then it only makes that variable in some cases. IMO there is no reason that it can’t be available all the time - it’s there, it’s just not being set in the template dict during on/off commands.