diff --git a/homeassistant/components/mqtt/binary_sensor.py b/homeassistant/components/mqtt/binary_sensor.py index 4e335dda959..0ec196270a6 100644 --- a/homeassistant/components/mqtt/binary_sensor.py +++ b/homeassistant/components/mqtt/binary_sensor.py @@ -189,17 +189,30 @@ class MqttBinarySensor( payload = value_template.async_render_with_possible_json_value( payload, variables={"entity_id": self.entity_id} ) + if not payload.strip(): # No output from template, ignore + _LOGGER.debug( + "Empty template output for entity: %s with state topic: %s. Payload: '%s', with value template '%s'", + self._config[CONF_NAME], + self._config[CONF_STATE_TOPIC], + msg.payload, + value_template, + ) + return + if payload == self._config[CONF_PAYLOAD_ON]: self._state = True elif payload == self._config[CONF_PAYLOAD_OFF]: self._state = False else: # Payload is not for this entity - _LOGGER.warning( - "No matching payload found for entity: %s with state topic: %s. Payload: %s, with value template %s", + template_info = "" + if value_template is not None: + template_info = f", template output: '{payload}', with value template '{str(value_template)}'" + _LOGGER.info( + "No matching payload found for entity: %s with state topic: %s. Payload: '%s'%s", self._config[CONF_NAME], self._config[CONF_STATE_TOPIC], - payload, - value_template, + msg.payload, + template_info, ) return diff --git a/tests/components/mqtt/test_binary_sensor.py b/tests/components/mqtt/test_binary_sensor.py index ff1c5a35a11..b909a0592e0 100644 --- a/tests/components/mqtt/test_binary_sensor.py +++ b/tests/components/mqtt/test_binary_sensor.py @@ -259,6 +259,76 @@ async def test_setting_sensor_value_via_mqtt_message_and_template(hass, mqtt_moc assert state.state == STATE_OFF +async def test_setting_sensor_value_via_mqtt_message_and_template2( + hass, mqtt_mock, caplog +): + """Test the setting of the value via MQTT.""" + assert await async_setup_component( + hass, + binary_sensor.DOMAIN, + { + binary_sensor.DOMAIN: { + "platform": "mqtt", + "name": "test", + "state_topic": "test-topic", + "payload_on": "ON", + "payload_off": "OFF", + "value_template": "{{value | upper}}", + } + }, + ) + await hass.async_block_till_done() + + state = hass.states.get("binary_sensor.test") + assert state.state == STATE_OFF + + async_fire_mqtt_message(hass, "test-topic", "on") + state = hass.states.get("binary_sensor.test") + assert state.state == STATE_ON + + async_fire_mqtt_message(hass, "test-topic", "off") + state = hass.states.get("binary_sensor.test") + assert state.state == STATE_OFF + + async_fire_mqtt_message(hass, "test-topic", "illegal") + state = hass.states.get("binary_sensor.test") + assert state.state == STATE_OFF + assert "template output: 'ILLEGAL'" in caplog.text + + +async def test_setting_sensor_value_via_mqtt_message_empty_template( + hass, mqtt_mock, caplog +): + """Test the setting of the value via MQTT.""" + assert await async_setup_component( + hass, + binary_sensor.DOMAIN, + { + binary_sensor.DOMAIN: { + "platform": "mqtt", + "name": "test", + "state_topic": "test-topic", + "payload_on": "ON", + "payload_off": "OFF", + "value_template": '{%if value == "ABC"%}ON{%endif%}', + } + }, + ) + await hass.async_block_till_done() + + state = hass.states.get("binary_sensor.test") + assert state.state == STATE_OFF + + async_fire_mqtt_message(hass, "test-topic", "DEF") + state = hass.states.get("binary_sensor.test") + assert state.state == STATE_OFF + assert "Empty template output" in caplog.text + + async_fire_mqtt_message(hass, "test-topic", "ABC") + state = hass.states.get("binary_sensor.test") + assert state.state == STATE_ON + + async def test_valid_device_class(hass, mqtt_mock): """Test the setting of a valid sensor class.""" assert await async_setup_component(