Template switch: How to combine MQTT binary sensor and command line switch?

Hi there,
perhaps I missed something fundamental but I am not able to get a template switch showing the correct state of a LCD display. I try to combine a MQTT binary sensor and a command line switch. Both parts work fine on their own.
The MQTT binary sensor:

binary_sensor:
  - platform: mqtt
    state_topic: "tinkerforge/bricklet/lcd_20x4/ofH/backlight_on"
    name: "lcdstatus"
    payload_on: true
    payload_off: false
    value_template: '{{ value_json.backlight }}'

My switches (command line switch and template switch):

switch:
  - platform: command_line
    switches: 
      tinkamosq: 
        command_on: "mosquitto_pub -t tinkerforge/bricklet/lcd_20x4/ofH/backlight_on/set -m ''"
        command_off: "mosquitto_pub -t tinkerforge/bricklet/lcd_20x4/ofH/backlight_off/set -m ''"

  - platform: template
    switches:
      lcdtemp:
        friendly_name: 'LCD Display'
        value_template: "{{ is_state('sensor.lcdstatus.state', 'on') }}"
        turn_on:
          service: switch.turn_on
          entity_id: switch.tinkamosq
        turn_off:
          service: switch.turn_off
          entity_id: switch.tinkamosq

Though I could turn the LCD display on and off the state of the LCD display shows always off.
I think that I do something wrong with the value_template of the template switch but I run out of ideas.
Perhaps someone could offer me a hint?

Thanks in advance,
topi

well, your lcdstatus is a binary_sensor, not a sensorā€¦ So first thing that comes to mind is that it should be

value_template: "{{ is_state('binary_sensor.lcdstatus.state', 'on') }}"
On top of that, I think you should not use a condition but jsut the template
value_template: "{{ states.binary_sensor.lcdstatus.state }}"

Also, from what I understand your command_line switch is just posting something into a MQTT topic, so you could use a mqtt_switch instead.

1 Like

Thank you very much lambtho for the two hints (ā€œlcdstatus is a binary_sensor, not a sensorā€ and ā€œyou should not use a condition but just the templateā€). All is working now as intended :smile:

Please find the revised and working template switch configuration below:

switch:
          - platform: command_line
            switches: 
              tinkamosq: 
                command_on: "mosquitto_pub -t tinkerforge/bricklet/lcd_20x4/ofH/backlight_on/set -m ''"
                command_off: "mosquitto_pub -t tinkerforge/bricklet/lcd_20x4/ofH/backlight_off/set -m ''"

          - platform: template
            switches:
              lcdtemp:
                friendly_name: 'LCD Display'
                value_template: "{{ states.binary_sensor.lcdstatus.state }}"
                turn_on:
                  service: switch.turn_on
                  entity_id: switch.tinkamosq
                turn_off:
                  service: switch.turn_off
                  entity_id: switch.tinkamosq

Towards using a command_line switch instead of the mqtt_switch. Well, I tried but I have different command topics for turning the LCD on ā€œā€¦/lcd_20x4/ofH/backlight_on/setā€ and off ā€œlcd_20x4/ofH/backlight_off/setā€ which I am not able to fit in the mqtt_switch configuration which expects a different payload for turning the LCD on or off.

Thanks again for your help!

1 Like