#Template for a MQTT Binary_Sensor with payload value of a number

1 messages · Page 1 of 1 (latest)

fiery marlin
#

Hi! Trying to setup an MQTT binary_sensor that reports a state of 'on' when value received over mqtt is anything thats not "0" and a value of "off" when value received is "0". This is the code I have put together so far:

  • platform: mqtt
    name: "Sink Leak Sensor"
    state_topic: "homeassistant/binary_sensor/tasmoleak01/water_leak"
    device_class: moisture
    qos: 0
    value_template: "{{ 'off' if value == 0 else 'on' }}"
    payload_on: 'on'
    payload_off: 'off'
    force_update: true

The binary_sensor reports as wet when value changes from 0 to anything higher but when the value returns to 0 (after I dry the sensor) it does not return to 'off' (or Dry). What am I doing wrong? Any Help would be much appreciated?

restive tiger
#

Is the payload an integer or a string?

fiery marlin
#

this is how it looks from the sensors console via tasmota:
19.034 RUL: ANALOG#A0DIV10 performs "publish homeassistant/binary_sensor/tasmoleak01/water_leak 23"
01:38:19.039 MQT: homeassistant/binary_sensor/tasmoleak01/water_leak = 23

#

not sure on how to tell the difference between one or the other

fiery marlin
#

yeap! just had to represent the value as an integer. This did the trick:

#
  • platform: mqtt
    name: "Sink Leak Sensor"
    state_topic: "homeassistant/binary_sensor/tasmoleak01/water_leak"
    device_class: moisture
    qos: 0
    value_template: "{{ 'off' if value == '0' else 'on' }}"
    payload_on: 'on'
    payload_off: 'off'
    force_update: true