MQTT Light Template - Help needed

I’ve modified my desk lamp with a wemos d1 mini running tasmota to make it “smart”. Since I need to toggle the physical button on my lamp, I use the tasmota “relay1” with “PulseTime 1”. The actual relay hence is always powered off. I have an analog read (ADC0) to see whether my lamp is currently turned on/off (since I can also use the original switch on it)

Now all I need to do is configure a light in Home Assistant that sends a Toggle command to tasmota when being turned on/off but that uses the analog read from my tasmota device as a state_template (>100 it’s turned on, <100 it’s turned off)

I read through the docs and also looked at a few examples, but I’m really not sure how to make this work. I could also extract the state as a sensor and then make it work but I would like not to go that route if not necessary.

I tried the following config:

light:
  - platform: mqtt
    schema: template
    name: Tischlampe
    command_topic: "cmnd/Tischlampe/POWER1"
    state_topic: "tele/Tischlampe/SENSOR"
    state_value_template: >
        {% if {{ value_json.ANALOG }} >= 100 %}
          on
        {% elif {{ value_json.ANALOG }} < 100 %}
          off
        {% endif %}
    availability_topic: "tele/Tischlampe/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "ON"
    payload_available: "Online"
    payload_not_available: "Offline"
    retain: false

I get the following error:

invalid config for [light.mqtt]: [state_value_template] is an invalid option for [light.mqtt]. Check: light.mqtt->state_value_template. (See ?, line ?).

Litlte progress, I need to use the default schema, not template. Also I forgot to remove the ‘{{’ and ‘}}’ for the json object:

light:
  - platform: mqtt
    name: Tischlampe
    command_topic: "cmnd/Tischlampe/POWER1"
    state_topic: "tele/Tischlampe/SENSOR"
    state_value_template: >
        {% if value_json.ANALOG >= 100 %}
          on
        {% elif value_json.ANALOG < 100 %}
          off
        {% endif %}
    availability_topic: "tele/Tischlampe/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "ON"
    payload_available: "Online"
    payload_not_available: "Offline"
    retain: false

Now I only need to test and see if it works.

The device is shown as “unavailable”

My device needs to send it’s tele data in order for HA to receive them.
So I added the rule that sends an tele update whenever my analog read changes more than 10%:

{"Rule3":"ON","Once":"OFF","StopOnError":"OFF","Length":54,"Free":457,"Rules":"ON ANALOG#A0div10 DO publish tele/%topic%/SENSOR ENDON"}

It kind of works, but for whatever reason the message that is published is empty:

17:22:21 RUL: ANALOG#A0DIV10 performs "publish tele/Tischlampe/SENSOR"
17:22:21 MQT: tele/Tischlampe/SENSOR =

Any ideas?

Getting closer:

I also need to publish a value:

Rule3 ON ANALOG#A0DIV10 DO publish tele/Tischlampe/SENSOR %value% ENDON

This publishes the outcome of A0DIV10, which is 0 for off and roughly above 60 for me when on.

I edited my rule:

Rule3 ON ANALOG#A0DIV10 DO publish tele/Tischlampe/SENSOR {"ANALOG":{"A0": %value%}} ENDON

and also my configuration.yaml:

light:
  - platform: mqtt
    name: Tischlampe
    command_topic: "cmnd/Tischlampe/POWER1"
    state_topic: "tele/Tischlampe/SENSOR"
    state_value_template: >
        {% if value_json.ANALOG.A0 >= 50 %}
          on
        {% elif value_json.ANALOG.A0 < 50 %}
          off
        {% endif %}
    availability_topic: "tele/Tischlampe/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "ON"
    payload_available: "Online"
    payload_not_available: "Offline"
    retain: false

But the light still does not show the correct on/off values (just shows as off). I’ll try adding a sensor now if no one else has another idea.

Got it working, switch to schema template:

light:
  - platform: mqtt
    schema: template
    name: Tischlampe
    command_topic: "cmnd/Tischlampe/POWER1"
    state_topic: "tele/Tischlampe/SENSOR"
    state_template: >
        {% if value_json.ANALOG.A0 >= 50 %}
          on
        {% elif value_json.ANALOG.A0 < 50 %}
          off
        {% endif %}
    availability_topic: "tele/Tischlampe/LWT"
    qos: 1
    command_on_template: "ON"
    command_off_template: "ON"
    payload_available: "Online"
    payload_not_available: "Offline"
    retain: false