MQTT Switch value_template question

Hi all,
I’m working on a sprinkler project controlled by HA.
The ESP Device Listens to the topic

home/Sprinkler/Valve/set

and publishes the status to

home/Sprinkler/Valve

So I have four switches to call the valves. Putting a number from 1 to 4 to the set-topic the appropiate valve is opened. The ESP sets all the other valves to close. This is because there’s only waterpressure for one valve at a time.
By sending an 0 all valves are closed.

So getting the status of 3 all the switches for 1, 2 and 4 should go to “Off”, but they are only “Off”, when getting the “0”.

Can anybody help me with this, so that the switches get the OFF-state, when receiving another value than their own On-value?

Here’s the configuration of the switches:

switch:
  • platform: mqtt
    name: “Valve1”
    state_topic: “home/Sprinkler/Valve”
    command_topic: “home/Sprinkler/Valve/set”
    payload_on: ‘1’
    payload_off: ‘0’
    state_template: >-
    {% if value != “1” %}
    “0”
    {% else %}
    “1”
    {% endif %}
    optimistic: false
    qos: 0
    retain: true
  • platform: mqtt
    name: “Valve2”
    state_topic: “home/Sprinkler/Valve”
    command_topic: “home/Sprinkler/Valve/set”
    payload_on: ‘2’
    payload_off: ‘0’
    state_template: >-
    {% if value != “2” %}
    “0”
    {% else %}
    “2”
    {% endif %}
    optimistic: false
    qos: 0
    retain: true
  • platform: mqtt
    name: “Valve3”
    state_topic: “home/Sprinkler/Valve”
    command_topic: “home/Sprinkler/Valve/set”
    payload_on: ‘3’
    payload_off: ‘0’
    state_template: >-
    {% if value != “3” %}
    “0”
    {% else %}
    “3”
    {% endif %}
    optimistic: false
    qos: 0
    retain: true
  • platform: mqtt
    name: “Valve4”
    state_topic: “home/Sprinkler/Valve”
    command_topic: “home/Sprinkler/Valve/set”
    payload_on: ‘4’
    payload_off: ‘0’
    state_template: >-
    {% if value != “4” %}
    “0”
    {% else %}
    “4”
    {% endif %}
    optimistic: false
    qos: 0
    retain: true

Thanks in advance!

Mario

Hey @strehlma, I wouldn’t use a value_template in your mqtt-switches. I think that it’d be a lot smarter to just use automations which turn all the other sprinklers off if the other sprinkler is turned on if that makes sense. If you need any help with that automation you’re more than welcome to ask :slight_smile:

state_template isn’t a valid keyword for mqtt switches:

Your payload on and payload off should properly turnon/turn off the devices with the correct payload

If you want to see the correct state for your switch, you need to use the keyword value_template.

This would work for all switches:

value_template: “{{ value | int != 0 }}”