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?
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?
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.
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
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.
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
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.