[SOLVED] MQTT2 Sonoff

Greetings!
Please tell me about setting up a sonoff, my English is very poor (Google), which is probably why the search did not give a result.
Sonoff is configured through files of things and items, everything works well, except if the device is in the on state and disconnected from 220 volts, the lwt will change to offline, but the device status will remain as on. How to make the device status change to off in such a situation?

Switch		box_heater_air	            "heater (AIR) [%s]"				<radiator>		(gHistory,gGarageScene1_OFF)			{ channel="mqtt:topic:g_mqtt:box_heater_air_things:power", 
                                                                                                                                  channel="mqtt:topic:g_mqtt:box_heater_air_things:state",
                                                                                                                                  autoupdate="false"  }

This is how to add something like this:

Switch		box_heater_air	            "heater (AIR) [%s]"				<radiator>		(gHistory,gGarageScene1_OFF)			        { channel="mqtt:topic:g_mqtt:box_heater_air_things:power", 
                                                                                                                                          cchannel="mqtt:topic:g_mqtt:box_heater_air_things:state",
                                                                                                                                          channel="mqtt:topic:g_mqtt:box_heater_air_things:lwt",
                                                                                                                                          autoupdate="false"  }

But the lwt is of type string:
Type string : lwt "lwt" [stateTopic="tele/box-heater-air/LWT"]

Thanks!

Your item has too many channels:

Switch		box_heater_air	            "heater (AIR) [%s]"				<radiator>		(gHistory,gGarageScene1_OFF)			{ channel="mqtt:topic:g_mqtt:box_heater_air_things:power" }

Should be enough.
Add another String item linked to the LWT channel
And a rule:

rule "OFF when offline"
when
    item LWTitem changed to "Offline"
then
    box_heater_air.postUpdate(OFF)
end

Have you checked the power rated of your box heater? If it is above 10A you should not be using a sonoff basic.

box_heater_air.postUpdate(UNDEF)

is also allowed, useful to signify faults

The state channel is needed for when, for example, the openHAB rebooted. The power channel changes only when the device is turned on or off.

Type switch : power     "Power"                 [stateTopic="stat/box-heater-air/POWER1", commandTopic="cmnd/box-heater-air/cmnd/POWER1"]
Type switch : state     "State"                 [stateTopic="tele/box-heater-air/STATE",  transformationPattern="JSONPATH:$.POWER1"]

Yes! I use sonoff pow.

I have a rule written a couple of months ago, but commented out, I don’t remember why :slight_smile:
Removed the comment, it works! And I was sure that using
autoupdate = "false"
will not work.
Thank!

Thank! I’ll take a note, but in this case, the OFF option is more suitable for me.

@vzorglub @rossko57 Please see the final version of my rule, I did not overdo it?

import org.eclipse.smarthome.model.script.ScriptServiceUtil
//---------------------------------------------------------------------------------------------
rule "Sonoff LWT"
when
    Member of gSonoffLWT changed
then
    if(triggeringItem.state.toString=="Offline") {
        val logStr = "" + (if (triggeringItem.label !== null) triggeringItem.label else triggeringItem.name ) 
                        + ": " + triggeringItem.state.toString

        val String curItemName = triggeringItem.name.split("_lwt").get(0)
        val SwitchItem  curItem = ScriptServiceUtil.getItemRegistry.getItem(curItemName)
        logInfo("RULE","<-- " + logStr)
        curItem.postUpdate(OFF)

        createTimer(now.plusSeconds(30)) [|
            if(triggeringItem.state.toString=="Offline") {
                sendTelegram("bot1", "(g) " + logStr) 
            }
		]        
    }
end 
//---------------------------------------------------------------------------------------------

PS a timer is needed if the device reconnected and again on the network, for some reason, some devices of sonoff with tasmota firmware do so often.

Looks good. But the logging is duplicating what the system does anyway. A bit unnecessary in my opinion.

Thanks for watching! And the output to the log is for the first time, for debugging.