Multiline on comman_on_template, not behaving like I expect it!

Hi all

I’m trying to achieve a Shelly Vintage integration directly in the light.yaml without HACS. Actually, 95% of the integration is working properly. The only thing, if I press the Bulb-Button in LoveLance it tells me that the brightness is not defined. So if the bulp is off and I just slide the brightness from 0% to lets say 30%, the bulp turns on with the proper brightness.

With that knowing, I tried to adjust the configuration on “command_on_template” to handle the brightness atttribute if not defined and set it directly. If it is there just go on.

Unfortunately, as soon as I put multiline on command_on_template the brightness slider does not work at all. I only can turn the light off. Configuration doesn’t bring any error.

Does anybody has any idea what I’m doing wrong here?

Here is the whole configuration:

NOT WORKING

- platform: mqtt
    schema: template
    name: "Test Light"
    state_topic: "shellies/ShellyVintage-6F73CB/light/0/status"
    state_template: >-
      {% set bulb = value_json.ison %}
      {% if bulb == true %}
        on
      {% else %}
        off
      {% endif %}
    brightness_template: "{{ value_json.brightness | float | multiply(2.55) | round(0) }}"
    command_topic: "shellies/ShellyVintage-6F73CB/light/0/set"
    command_on_template: >-
      {% if brightness is defined %}
      '{"turn":"on", "brightness":{{brightness | float | multiply(0.3922) | round(0)}}}'
      {% else %}
      '{"turn":"on", "brightness":30}'
      {% endif %}
    command_off_template: '{"turn":"off", "brightness":0}'

Thank you a lot

Grafphi

I received three of these lights today to go over my dining table and all of them subscribe to the same MQTT topic. Under “Advanced - Developer Settings”, I changed the “Use custom MQTT prefix” on each of the lights to “ShellyVintage-Dining”.

Using your configuration as a starting point, and debugging with the software “MQTT explorer”, I got he following to work.

- platform: mqtt
  schema: template
  name: "Dining Table Lights"
  state_topic: "shellies/ShellyVintage-Dining/light/0/status"
  state_template: >
    {% set are_on = value_json.ison %}
    {% if are_on == true %}
      on
    {% else %}
      off
    {% endif %}
  brightness_template: '{{ (value_json.brightness | float | multiply(2.55)) | round(0) }}'
  command_topic: "shellies/ShellyVintage-Dining/light/0/set"
  command_on_template: >
      {"turn": "on", "brightness":
      {%- if brightness is defined -%}
      {{(brightness | float | multiply(0.3922)) | round(0)}}
      {%- else -%}
      33
      {%- endif -%} 
      }
  command_off_template: '{"turn":"off"}'

Thank you for your original code. It pointed me in the right direction after struggling with schema: json. It is possible to have the lights come on at the same setting as they were turned off by changing the command_on_template to…

  command_on_template: >
      {"turn": "on"
      {%- if brightness is defined -%}
      , "brightness":{{(brightness | float | multiply(0.3922)) | round(0)}}
      {%- endif -%} 
      }

Now they all turn on and off and change to the same brightness at the same time. :+1:

1 Like