MQTT Light | state | state_value_template

Hello tgether,

I need help to set up two different lights:

  • controlled by my own esp32 coding with MQTT messages
  • I defined messages for the commands “RF3C1” and also for the state “RF3S1”
  • of course I do not want to change my telegram in my coding for the esp32 :wink:

When I do it my Mqtt Switch everything works perfect (like my “VasenLicht”, but now I want to change it to Mqtt Light (like my “LeseLicht”. Here I got some problems with the state.
My template does not work, but I cant find the cause… Also after insert the state_value_template my commands does not work any more.


> 
> mqtt:
>   light:
>     - unique_id: leselicht
>       name: "LeseLicht"
>       command_topic: "home_sub"
>       payload_on: "RF4C1"
>       payload_off: "RF4C3"
>       state_topic: "home_pub/RF4S"
>       state_value_template: "{% if value == 'RF4S1' %}on{% endif %}{% if value == 'RF4S3'%}off{% else %}off{% endif %}"
>   switch:
>     - unique_id: vasenlicht
>       name: "VasenLicht"
>       command_topic: "home_sub"
>       payload_on: "RF2C1"
>       payload_off: "RF2C3"
>       state_topic: "home_pub/RF2S"
>       state_on: "RF2S1"
>       state_off: "RF2S3"

You have the equivalent of this:

state_value_template: >
  {% if value == 'RF4S1' %}
    on
  {% endif %}
  {% if value == 'RF4S3' %}
    off
  {% else %}
    off
  {% endif %}

What you want is this:

state_value_template: >
  {% if value == 'RF4S1' %}
    on
  {% elif value == 'RF4S3' %}
    off
  {% else %}
    off
  {% endif %}

thanks you…
I have tried your version… but unfortunately still no reaction.
there must be something else wrong, i cant even use the commands form the dashboard…

The value reported by state_value_template should match the value you specified for either payload_on or payload_off.

mqtt:
  light:
    - unique_id: leselicht
      name: "LeseLicht"
      command_topic: "home_sub"
      payload_on: "RF4C1"
      payload_off: "RF4C3"
      state_topic: "home_pub/RF4S"
      state_value_template: "{{ iif(value == 'RF4S1', 'RF4C1', 'RF4C3') }}"
1 Like

Thanks a lot. That works :wink:
I recently fall in love with home assistant… nice to know that also the community is very kind and willing to help :wink:

But about this issue:
Well (at least for me) it is pretty wired: Why should the statuses have the same messages as the commands…

From the point of view of the control system, it is doesn’t make any sense. It just causes confusions and causes ambiguity especially in combination with the MQTT protocol…
Of course with the help of topics and especially with the template there is a nice work around…