MQTT binary_sensor based on MQTT Explorer string

I need to configure binary_sensor for MQTT inputs on my Shelly Pro 4PM. I succesfully manage to to with outputs (relays) - New Shelly Pro 4PM - wifi & LAN & MQTT - First of Shelly Pro Series - #9 by kslb.

Now, after some time I get data from MQTT Explorer what I get when I trigger input.

Payload ON:
rpc = {“src”:“shellypro4pm-XXXX”,“dst”:“shellypro4pm-XXXX-kabinet/events”,“method”:“NotifyEvent”,“params”:{“ts”:1637522890.20,“events”:[{“component”:“input:0”, “id”:0, “event”:“long_push”, “ts”:1637522890.20}]}}

Payload ON:
rpc = {“src”:“shellypro4pm-XXXX”,“dst”:“shellypro4pm-XXXX-kabinet/events”,“method”:“NotifyEvent”,“params”:{“ts”:1637522925.78,“events”:[{“component”:“input:0”, “id”:0, “event”:“single_push”, “ts”:1637522925.78}]}}

Payload OFF:
rpc = {“src”:“shellypro4pm-XXXX”,“dst”:“shellypro4pm-XXXX-kabinet/events”,“method”:“NotifyEvent”,“params”:{“ts”:1637522913.64,“events”:[{“component”:“input:0”, “id”:0, “event”:“btn_up”, “ts”:1637522913.64}]}}

We must consider, that we have 4 inputs, so we must know which input is triggered, so I tried with:

Not working:

  - platform: mqtt
    name: "Shelly Pro 4PM Input 1"
    unique_id: "shellypro4pm-XXXX-kabinet-in1"
    state_topic: "shellypro4pm-XXXX-kabinet/input/0"
    payload_on: 1
    payload_off: 0
    qos: 0

Also not working:

  - platform: mqtt
    name: "Shelly Pro 4PM Input 1"
    unique_id: "shellypro4pm-XXXX-kabinet-in1"
    state_topic: 'shellypro4pm-XXXX-kabinet/input/0'
    value_template: >- 
      {% if '"event":"btn_up"' in value %}   
        {{'OFF'}}
      {% else %}
        {{'ON'}}
      {% endif %}

Last try:

  - platform: mqtt
    name: "Shelly Pro 4PM Input 1"
    unique_id: "shellypro4pm-XXXX-kabinet-in1"
    state_topic: 'shellypro4pm-XXXX-kabinet/events'
    value_template: >- 
      {% if '"component":"input:0"' and '"event":"btn_up"' in value %}   
        {{'OFF'}}
      {% elif '"component":"input:0"' and '"event":"single_push"' in value %}
        {{'ON'}}
      {% elif '"component":"input:0"' and '"event":"long_push"' in value %}
        {{'ON'}}
      {% endif %}

So, do anybody know how to achieve this?

OK, so to start with - you aren’t telling Home Assistant that it is dealing with JSON, so it is expecting plain text / numbers.

Next, the fields you are looking for aren’t the right ones anyway:
paste the JSON in here http://json.parser.online.fr/ and you will see that you are actually looking for:
And then we use the function json_value to tell Home Assistant to treat the received payload as JSON.

json_value.params.events.component

for the Input.

Now the question is - the rpc = bit, is that actually in the MQTT message? Because if so that is going to be a problem, not a major one, but still a problem.

value_template: >- 
      {% if json_value.params.events.component == "input:0" and json_value.params.events.event == "btn_up" %}   
        'off'
      {% elif if json_value.params.events.component == "input:0" and json_value.params.events.event == "single_push" %}
        'on'
      {% elif json_value.params.events.component == "input:0" and json_value.params.events.event == "long_push" %}
        'on'
      {% else %}
       {{ states('sensor.shelly_pro_4pm_input_1') }}
      {% endif %}

You need the else, because if it’s a different input you pressed, Home Assistant will still receive the payload for every switch / sensor you configure to listen to the same topic, and when none of the if elseif conditions match, it will complain. In this case, you just output the existing state of the switch / sensor.

Also looking at the screenshot - I realise, rpc is actually part of the topic, not the payload. Your state topic should be:

shellypro4pm-XXXX-kabinet/events/rpc

Thank you…

But I got error:

So, if you can help me regarding that…

elif if
should obviously just be elif

Additionally because it’s a binary sensor, then where I wrote sensor.shelly_pro_4pm_input_1 you should obviously write binary_sensor.shelly_pro_4pm_input_1

Tried with:

  - platform: mqtt
    name: "Shelly Pro 4PM Input 1"
    unique_id: "shellypro4pm-XXXX-kabinet-in1"
    state_topic: 'shellypro4pm-XXXX-kabinet/events/rpc'
    value_template: >- 
      {% if json_value.params.events.component == "input:0" and json_value.params.events.event == "btn_up" %}   
        'off'
      {% elif json_value.params.events.component == "input:0" and json_value.params.events.event == "single_push" %}
        'on'
      {% elif json_value.params.events.component == "input:0" and json_value.params.events.event == "long_push" %}
        'on'
      {% else %}
       {{ states('binary_sensor.shelly_pro_4pm_input_1') }}
      {% endif %}

But no success… a little lost, where is the problem…

Then I tried also with other state topic, but also no success:
state_topic: 'shellypro4pm-XXXX-kabinet/events'

Any clue?

Input event is not detected… That I forgot to write…

So, maybe you or anybody have any idea, what is the next step to solve the problem with input… don’t know why is not detected properly…

Correct binary_sensor for MQTT is (and that is working):

  - platform: mqtt
    name: "Shelly Pro 4PM Input 1"
    state_topic: 'shellypro4pm-XXXX-kabinet/events/rpc'
    value_template: >- 
      {% if ('input:0') and ('long_push') in value %}   
        {{'ON'}}
      {% elif ('input:0') and ('single_push') in value %}   
        {{'ON'}}
      {% elif ('input:0') and ('btn_up') in value %}   
        {{'OFF'}}
      {% else %}
        {{ states('binary_sensor.shelly_pro_4pm_input_1') }}
      {% endif %}
1 Like

Out of interest, just for a test - if you want you can try (with a second switch, just to see if it works) - replacing the .dot. way of accessing the values with the array method of accessing the values:

json_value['params']['events']['component'] == "input:0"
json_value['params']['events']['event'] == "btn_up"

etc
Just to see if it works like that?

Nop,

I got on verification error:


  - platform: mqtt
    name: "Shelly Pro 4PM Input 1"
    state_topic: 'shellypro4pm-XXXX-kabinet/events/rpc'
    value_template: >-
      {% if json_value['params']['events']['component'] == "input:0" and json_value['params']['events']['event'] == "btn_up" %}
        'off'
      {% elif json_value['params']['events']['component'] == "input:0" and json_value['params']['events']['event'] == "single_push" %}
        'on'
      {% elif json_value['params']['events']['component'] == "input:0" and and json_value['params']['events']['event'] == "long_push" %}
        'on'
      {% else %}
       {{ states('binary_sensor.shelly_pro_4pm_input_1') }}
      {% endif %}
  - platform: mqtt
    name: "Shelly Pro 4PM Input 2"
    state_topic: 'shellypro4pm-XXXX-kabinet/events/rpc'
    value_template: >-
      {% if json_value['params']['events']['component'] == "input:1" and json_value['params']['events']['event'] == "btn_up" %}
        'off'
      {% elif json_value['params']['events']['component'] == "input:1" and json_value['params']['events']['event'] == "single_push" %}
        'on'
      {% elif json_value['params']['events']['component'] == "input:1" and and json_value['params']['events']['event'] == "long_push" %}
        'on'
      {% else %}
       {{ states('binary_sensor.shelly_pro_4pm_input_2') }}
      {% endif %}
  - platform: mqtt
    name: "Shelly Pro 4PM Input 3"
    state_topic: 'shellypro4pm-XXXX-kabinet/events/rpc'
    value_template: >-
      {% if json_value['params']['events']['component'] == "input:2" and json_value['params']['events']['event'] == "btn_up" %}
        'off'
      {% elif json_value['params']['events']['component'] == "input:2" and json_value['params']['events']['event'] == "single_push" %}
        'on'
      {% elif json_value['params']['events']['component'] == "input:2" and and json_value['params']['events']['event'] == "long_push" %}
        'on'
      {% else %}
       {{ states('binary_sensor.shelly_pro_4pm_input_3') }}
      {% endif %}
  - platform: mqtt
    name: "Shelly Pro 4PM Input 4"
    state_topic: 'shellypro4pm-XXXX-kabinet/events/rpc'
    value_template: >-
      {% if json_value['params']['events']['component'] == "input:3" and json_value['params']['events']['event'] == "btn_up" %}
        'off'
      {% elif json_value['params']['events']['component'] == "input:3" and json_value['params']['events']['event'] == "single_push" %}
        'on'
      {% elif json_value['params']['events']['component'] == "input:3" and and json_value['params']['events']['event'] == "long_push" %}
        'on'
      {% else %}
       {{ states('binary_sensor.shelly_pro_4pm_input_4') }}
      {% endif %}