Create sensor or binary_sensor from switch (unavailable)

Hello,
I need a binary sensor that tell the state of my sonoff switch if ON or OFF.
I create a following but in home assistant is always “unavailable”

  - platform: mqtt
    state_topic: "stat/sonoff4pro2/POWER1"
    name: "Tapparella_stato"
    availability_topic: "tele/sonoff4pro2/LWT"
    payload_on: ON
    payload_off: OFF
    value_template: "{{ value_json.state }}"
    device_class: "door"

The result of the sensor in the home assistant is “unavailable”
Perhaps there is something wrong in value_json and in the device_class.
Can someone help me?

Please post an example of the payload received by:

stat/sonoff4pro2/POWER1

Also, you may want to try adding double quotes to these two options:

    payload_on: "ON"
    payload_off: "OFF"

Hello Taras,

{
"Time":"2019-11-14T13:22:01","
Uptime":"8T01:07:18",
"Vcc":3.237,
"SleepMode":"Dynamic",
"Sleep":50,
"LoadAvg":19,
"POWER1":"ON",
"RSSI":72,
"LinkCount":1,
"Downtime":"0T00:08:07"
}
   }
'stat/sonoff4pro2/RESULT', ... (15 bytes))
{"POWER1":"ON"}

There is no key called state in that payload so value_json.state will not work. However there is a key called POWER1 so the template should use that.

Try this:

  - platform: mqtt
    state_topic: "stat/sonoff4pro2/POWER1"
    name: "Tapparella_stato"
    availability_topic: "tele/sonoff4pro2/LWT"
    payload_on: "ON"
    payload_off: "OFF"
    value_template: "{{ value_json.POWER1 }}"
    device_class: "door"

I tried but show message

"Error parsing value: 'value_json' is undefined (value: OFF, template: {{ value_json.POWER1 }})"

and

"Error parsing value: 'value_json' is undefined (value: ON, template: {{ value_json.POWER1 }})"

Is that second closing brace } actually part of the payload or is that a copy-paste error?

{
"Time":"2019-11-14T13:22:01","
Uptime":"8T01:07:18",
"Vcc":3.237,
"SleepMode":"Dynamic",
"Sleep":50,
"LoadAvg":19,
"POWER1":"ON",
"RSSI":72,
"LinkCount":1,
"Downtime":"0T00:08:07"
}
   }    <-------- Is this really part of the payload?

@123
Is part of payload that I read with mqtt explorer.
The indentation ( } } ) can be a copy/past error.

If the payload has one open brace { and two close braces } } then it is not a properly formatted JSON string. Home Assistant won’t convert it to a JSON object and so value_json will be undefined.

Error parsing value: ‘value_json’ is undefined

It’s very strange that the Sonoff would produce a malformed JSON string.

@123
Hello Taras,
the exact JSON string is:

{
"Time":"2019-11-15T11:47:02",
"Uptime":"8T23:32:19",
"Vcc":3.237,
"SleepMode":"Dynamic",
"Sleep":50,
"LoadAvg":19,
"POWER1":"OFF",
"POWER2":"OFF",
"POWER3":"OFF",
"POWER4":"OFF",
"0T00:08:07"
}

I read this string from STATE, see below:

I tried with this new binary_sensor in configuration.yaml, modified state topic in: state_topic: "tele/sonoff4pro2/STATE"

  - platform: mqtt
    state_topic: "tele/sonoff4pro2/STATE"
    name: "Tapparella_stato"
    payload_on: "ON"
    payload_off: "OFF"
    value_template: "{{ value_json.POWER1 }}"
    device_class: "door"

Now sensor is available but not show status ON / OFF and the state is always “closed”

This string is not valid JSON because the last line does not contain a key:

{
"Time":"2019-11-15T11:47:02",
"Uptime":"8T23:32:19",
"Vcc":3.237,
"SleepMode":"Dynamic",
"Sleep":50,
"LoadAvg":19,
"POWER1":"OFF",
"POWER2":"OFF",
"POWER3":"OFF",
"POWER4":"OFF",
"0T00:08:07"    <----------- This a value but without a key
}

Do you suggest how to get the json string with key missing? I use mqtt explorer but at this point I am convinced that it does not detect the values I need

@123
This is a complete valid json:

{
   "Time":"2019-11-15T13:47:15",
   "Uptime":"9T01:32:32",
   "Vcc":3.204,
   "SleepMode":"Dynamic",
   "Sleep":50,
   "LoadAvg":19,
   "POWER1":"OFF",
   "POWER2":"OFF",
   "POWER3":"OFF",
   "POWER4":"OFF",
   "Wifi":{
      "AP":1,
      "SSId":"Bora-bora",
      "BSSId":"9C:F9:D4:8C:45:33",
      "Channel":11,
      "RSSI":74,
      "LinkCount":1,
      "Downtime":"0T00:08:07"
   }
}

I doubt this a problem with MQTT Explorer. However, to compare results, you can use Home Assistant to subscribe to tele/sonoff4pro2/STATE and examine the payload it receives.

Go to Developer Tools > MQTT and use the ‘Listen to a topic’ function:

Is that the payload from this topic: tele/sonoff4pro2/STATE. If it is, then it is a valid JSON string and the binary_sensor should be able to interpret it properly.

The result is always closed
immagine

Could it be the device_class?

No. The device_class modifies the binary_sensor’s appearance in the UI. It has nothing to do with the binary_sensor’s ability to receive data.


The image shows two sensors with the same name. Why?

Oh, this is a mistake. The correct image is
immagine

I need to know that the sensor tells me when the shutter is moving. That is when it rises “uphill” when it comes down “downhill”.
The climb is “POWER1” in “ON” mode and the descent is “POWER2” in “ON” mode. The “OFF” modes are when it stops.
The sonoff switch has 2 relays, one is used for the climb and the other for the descent

It sounds like you need to use MQTT Cover.

I’m going to let someone else jump in here and help you. Good luck!

@123
I am very sorry that you are abandoning me.
I just need a sensor that tell me when a switch status is ON or OFF. Then I’ll do the rest

Abandoning?

Let me remind you that you sent me a Private Message asking for my assistance and I complied without hesitation.

All support provided in this community forum is on a volunteer basis. No one is obligated, or should be made to feel obligated, to help others.

We are now over 19 posts into this thread without a solution. The information you’ve provided has changed so many times (or has simply been wrong) that I no longer feel our discussion is productive. That’s why I’m stepping aside. Hopefully, someone else will read this thread, discover the clue that I missed, and provide you with a solution.

Good luck.

2 Likes