MQTT - how to filter incoming data? value_template?

I want to transfer status data of my switches from fhem to hassio using MQTT.
My issue: On each status change I get not only one message, but two. These are:

set_on
on

set_off
off

set_toogle
on or off, depending

I used the MQTT Switch - Home Assistant

switch:
  - platform: mqtt
    name: "Licht Aussen"
    icon: mdi:lightbulb
    state_topic: "fhem/licht/aussen/reading/state"
    command_topic: "fhem/licht/aussen/state/set"
    #availability_topic: "fhem/licht/aussen/available"
    payload_on: "on"
    payload_off: "off"
    state_on: "on"
    state_off: "off"
    optimistic: false
    qos: 0
    retain: true

But it looks like each time hassio gets a content which is not “on” it will be interpreted as off.
Anyway the result is, that the switch in hassio is switching finaly to off, regardless, what I am doing.
I used the MQTT.fx to monitor the content of MQTT messages. It looks like the command_topic should work. In MQTT.fx I can put on or off into the topic and it works.

In the documentation of switch.mqtt there is a
value_template
(string)(Optional)Defines a template to extract a value from the payload.

Maybe I can use this to filter out only on and off and to ignore all other content?
But I am completely new to hassio and I am a little bit overwelmed from the

Is this the right way?
And if yes, how I can use value_template to filter out only on and off?
there is something with value and value_jason, but I don’t understand, how to start with this. Even this starting point from

is not clear for me:

This means that if the incoming values looks like the sample below:

{
  "on": "true",
  "temp": 21
}

The template for on would be:

'{{value_json.on}}'

what shoul I use to reference the incomming state?
‘{{value_json.state}}’
‘{{value_json.state_topic}}’
???

My logic would be:
CASE WHEN topic_value IN (on, off) THEN use it ELSE ignore it END
or
CASE WHEN topic_value = on then on WHEN topic_value = off then off ELSE ignore it END

1 Like

The first part of your post seems to indicate that you are not receiving json in your incoming messages, but then you start talking about decoding json data, so I’m not sure exactly what is in the payload of your messages.

Can you paste the output of MQTT.FX (or better still mosquitto_sub as its easier) to avoid any confusion?

What I get is not json, but only these 5 different string values. And I want to use only “on” and “off” and ignore all others.

set_on
on
set_off
off
set_toogle

Ok, what you need to do is check that payload equals on or off, and use that if it does, otherwise use the existing state of the sensor.

Just doing it from memory, something like

value_template: {% if value == "on" or value == "off" %} {{ value }} {% else %} {{ states('switch.licht_aussen') }}

should work, but you can try it out using the template page under developer tools.

I tried a little bit and found that the following is the right syntax. I will learn, how to use this script language and the developer tools. This is only my second day with hassio :slight_smile:

value_template: >
  {% if value == "on" or value == "off" %}
    {{ value }} 
  {% else %} 
    {{ states('switch.licht_aussen') }} 
  {% endif %}

But it looks like that this is not the reason for the issue:
I disabled the connection from fhem to MQTT, and the issue is inside hassio.
When I change the status of the switch from “off” to “on” then it switches allways back to off. Something is wrong in my switch definition.

Is there a way to see, what happens inside hassio, when I change the switch from “off” to “on” and why it is switching back to off?

After sending the command, HA expects a status message back from the switch indicating the new state of the switch. If this doesn’t happen, or it doesn’t decode the message correctly, it assumes the state didn’t change and returns to the original state.

If your switch doesn’t send a status message after it changes state, you can set the optimistic flag in the HA switch definition, but obviously this isn’t as reliable.

1 Like

OK, I got the idea how MQTT works in this case. And finaly it works!!!
MQTT ist also new for me, but I learned something this weekend about hassio and about MQTT, which I never used before.
Thank you very much for your support! It’s a great community here. Here are my settings.
I don’t know if I realy need the payload_on and payload_off. I don’t understand the difference between state and payload in the configuration.

switch:
  - platform: mqtt
    name: "Licht Aussen"
    icon: mdi:lightbulb
    state_topic: "/SmartHome/licht_aussen/state"
    command_topic: "/SmartHome/licht_aussen/state/value"
    #availability_topic: "fhem/licht/aussen/available"
    payload_on: "on"
    payload_off: "off"
    state_on: "on"
    state_off: "off"
    optimistic: false
    qos: 0
    retain: true
    # value_template: >
      # {% if value == "on" or value == "off" %}
        # {{ value }} 
      # {% else %} 
        # {{ states('switch.licht_aussen') }} 
      # {% endif %}

BTW, is there a way to test different settings for the switch without the need to restart hassio? It takes every time several minutes. But only to reaload the “core” is not enough.

You are so right - How the heck do one see the options of a json payload. I simply cannot follow the logic behind this if there is any.