Split response payload

I have a light which can be triggered with “On Brightness” and it will respond with “On Brightness”. What I’m trying to do is to split the “On” and “Brightness” and just return the “On” value for state_template and “Brightness” value for brightness_template. I’ve looked at the MQTT Template Light example which seems to do what I want but cannot seem to get it right. When I publish a “On 100”, I expect to see “On” but the state will not register in HA. It remains “Off”.

light:
  - platform: mqtt_template
    command_topic: "home/topic/set"
    state_topic: "home/topic/status"
    command_on_template: "On {{ brightness|d }}"
    command_off_template: "Off"
    state_template: "{{ value.split()[0] }}"             #not sure if I'm using split the right way
    brightness_template: "{{ value.split()[1] }}"

You have to declare the character you want to use to do the split.

value.split(' ')[0] should do the trick in your example.

Thanks for prompt response. I tried this but still dont work. The state still shows “Off” when I checked under Developer tools > States. For testing, I manually subscribed to the stat topic “home/topic/status” and when I manually publish an “ON 100”, I can see the response as “ON 100” but I get “[homeassistant.components.light.mqtt_template] Invalid state value received” in the logs. Same error for brightness.

- platform: mqtt_template
  name: "Test"
  command_topic: "home/topic/set"
  state_topic: "home/topic/status"
  command_on_template: "On {{ brightness|d }}"
  command_off_template: "Off"
  state_template: "{{ value.split(' ')[0] }}"
  brightness_template: "{{ value.split(' ')[1] }}"

It looks like I can’t help you on that part. I’ve took a quick look at the MQTT light component description and while you are using a configuration that seems to be valid I couldn’t even find some of the configuration variables you are using (e.g. state_template).

I’ve tested your configuration on my machine and subscribed to the command_topic. Home Assistant is sending On {{ brightness|d }} (not e.g. On 100) when activating the switch on the interface. It looks like you need to alter that.

I’m pretty sure you’ll find someone else here who already used the component and is able to guide you.

Thanks for testing this in your system. Finally found out the reason why its not working. It only accepts all lowercase on/off in command published eventhough its listed as “On/Off” in the command_template. Wont work with On/ON/Off/OFF. There must also be a bug in the code as when I send “off”, the command works and HA reflects the change in the status but I still get error in the log “Invalid brightness value received”. Thanks again!

light:
  - platform: mqtt_template
    command_topic: "home/topic/set"
    state_topic: "home/topic/status"
    command_on_template: "On {{ brightness|d }}"    
    command_off_template: "Off"
    state_template: "{{ value.split()[0] }}" 
    brightness_template: "{{ value.split()[1] }}"