[SOLVED] Sonoff Tasmota firmware Version Check and Update

  • Platform information:
    • Hardware: _ Raspberry Pi 3 Model B Rev 1.2_
  • OS: Raspbian GNU/Linux 8 (jessie)
    • Java Runtime Environment: (Zulu Embedded 8.25.0.76-linux-aarch32hf) (build 1.8.0_152-b76)
    • openHAB version: OH 2.4.0.M5
    • Bindings:Astro, AVM, EXEC, MQTT, Network, NTP, Samsung, Sonos, Weather (binding-weather1),MapDB-Persitence, http
    • Transformations: exec, Javascript, JSONPATH, Map, RegEx, Scale

I read a lot in the Community blogs about Sonoff -Firmware and Firmware-Flashing. I have flashed several sonoffs via AtomIO and they are working in my openHAB-Setup. I was inspired by @ThomDietrich, @AndrewZ and of course Theo Arends and created the following setup to get some information:

sonoff.rules

val sonoff_device_ids = newArrayList("sonoffs")

rule "Sonoff Tasmota Version"
when
		Item Dummy2 changed to ON or
		Channel 'astro:sun:local:noon#event' triggered START
then
		logInfo("sonoff.rules", "Tasmota Rel.Vers. set to Items: " )
		for (String device_id : sonoff_device_ids) {
					publish("peter", "cmnd/" + device_id + "/status", "2")
					logInfo("sonoff.rules", "Sonoff Maintenance: " + device_id + " / " + sonoff_device_ids)
		}
end

sonoff.items

//**********************************************************************************************************************************************************************************************************************
//POW Sensoren
//**********************************************************************************************************************************************************************************************************************
//192.168.178.62	Kugellampe
Switch    Sonoff_Pow_01             "Kugellampe  [MAP(de.map):%s]"         <kugel1>           (EG_Kind2,gLight,gSonoff) { mqtt=">[peter:cmnd/pow_01/power:command:*:default],
                                                                                                                                <[peter:stat/pow_01/POWER:state:default]" }
Number    Sonoff_Pow_01_Verbrauch   "Kugellampe Verbrauch Total[%.2f kWh]" <kugel1>           (Chart_Sys_Temp)          { mqtt="<[peter:tele/pow_01/SENSOR:state:JSONPATH($.ENERGY.Total)]" }
//        For Fun and Test
Number    Sonoff_Pow_01_Strom       "Kugellampe Stromaufnahme [%.2f A]"    <kugel1>           (Chart_Sys_Temp)          { mqtt="<[peter:tele/pow_01/SENSOR:state:JSONPATH($.ENERGY.Current)]" }
Number    Sonoff_Pow_01_Spannung    "Kugellampe Spannung[%.2f Volt]"       <kugel1>           (Chart_Sys_Temp)          { mqtt="<[peter:tele/pow_01/SENSOR:state:JSONPATH($.ENERGY.Voltage)]" }
Number    Sonoff_Pow_01_Leisung     "Kugellampe Leistung[%.2f W]"          <kugel1>           (Chart_Sys_Temp)          { mqtt="<[peter:tele/pow_01/SENSOR:state:JSONPATH($.ENERGY.Power)]" }
Number    Sonoff_Pow_01_VCC         "Kugellampe VCC[%.2f V]"               <energy>           (Chart_Sys_Temp)          { mqtt="<[peter:tele/pow_01/STATE:state:JSONPATH($.Vcc)]" }
String    Sonoff_Pow_01_State       "Kugellampe Status [%s]"               <chart>            (Chart_Sys_Temp)          { mqtt="<[peter:stat/pow_01/POWER:state:default]" }
Number    Sonoff_Pow_01_RSSI        "Kugellampe RSSI [%s]"                 <qualityofservice> (EG_Kind2,gRSSI)          { mqtt="<[peter:tele/pow_01/STATE:state:JSONPATH($.Wifi.RSSI)]" }
String    Sonoff_Pow_01_Tele        "Kugellampe Wlan [%s]"                 <chart>            (Chart_Sys_Temp)          { mqtt="<[peter:tele/pow_01/STATE:state:JSONPATH($.Wifi.SSId)]" }
DateTime  Sonoff_Pow_01_Date        "Kugellampe Refresh [ %1$tH:%1$tM]"    <clock>            (Chart_Sys_Temp)          { mqtt="<[peter:tele/pow_01/STATE:state:JSONPATH($.Time)]" }
String    Sonoff_Pow_01_Version     "Kugellampe Version [%s]"              <sonoff_pow>       (Chart_Sys_Temp,gVer)     { mqtt="<[peter:stat/pow_01/STATUS2:state:JSONPATH($.StatusFWR.Version)]" }
//
Switch    Dummy2                    "Testschalter Dummy2 [%s]"                                (gPower)
String    Sonoff_Current_FW_Available "Current Release available from Theo[%s]" <arendst>     (Chart_Sys_Temp,gVer)     { http="<[tasmotaRelease:10000:JSONPATH($[0].name)]"}

http.cfg

# configuration of the second cache item
# Tasmota Release Status (cached twice a day)
#<id2>.url=
tasmotaRelease.url=https://api.github.com/repos/arendst/Sonoff-Tasmota/tags
#<id2>.updateInterval=
tasmotaRelease.updateInterval=43200000

logInfo

2018-10-31 22:31:15.406 [INFO ] [.smarthome.model.script.sonoff.rules] - Tasmota Rel.Vers. set to Items: 
2018-10-31 22:31:15.417 [INFO ] [.smarthome.model.script.sonoff.rules] - Sonoff Maintenance: sonoffs / [sonoffs]

and the result looks like this:

As i’m a newbie and do not understand Java and programming, i’m wondering how the rule works.

My first question:
From where does the rule know which are the “sonoffs” items (group topic) in the array ? I didn’t declared them anywhere. Is there a mechanism in openHAB to store it an can one see them with system tools ?

My second question:
As i understand the “for loop” looks in every sonoff (via the publish command) and the firmwareinformation comes back via the item-channel. and it loops as long as every topic in the array is passed?! Therefore i fill in the logInfo-Staetment in the" for-loop", but it’s only shown once, as in the above logger. in my IMHO it should be shown for every topic.

Maybe there is someone to explain me, my stupid questions ? :sweat_smile:

The next step for me will be to test the OTA-Update, but i’m a bit scared, because i’ve crashed one or two sonoff-basics, while trying to update them via the web-interface. (I tried it too with minimal-version and afterwards the normal,etc.)

There are a lot of more questions (validation of acuel firmware and device firmware), or what is the difference between “upload” and “upgrade” in the firmware commands ?


for me it looks like if it’s same.
If anyone can help me i will be very happy

1 Like

Lucky for you, nothing posted above is Java.

I think there is missing code, perhaps a System started Rule that populates sonoff_device_ids with IDs.

Actually the publish is the call to the MQTT Action to send a message to the status topic of the sonoff with the given ID. Presumably the sonoff will publish it’s current status including the tasmota version in response which gets picked up by the Item bound to the status topic.

The array contains the device_id’s only and the topic is created in the publish command (cmd/device_id/status).

It’s only shown once because you need to populate the array list with all your device ids.

1 Like

Using that line, the array should be filled with those items that have the same MQTT GroupTopic (at least the comment in the original code from the Wikistates that!)
The default for GroupTopic is “sonoffs”, did you change that?
If you want to use it hardcoded for your devices you can fill that array like this:

val sonoff_device_ids = newArrayList(
    "sonoff_1",
    "sonoff_2",
    "sonoff_3",
) 

I used my MQTT Topics of each device for that list, YOU NEED TO CHANGE THAT! Put such in the sonoff.rules above the rule-code instead of your first line!

My working rule (which is copied from the wiki) looks like.

rule "Sonoff Maintenance"
when
    Item Sonoff_Action received command
then 
    logInfo("Sonoff Maintenance", "Sonoff Maintenance on all devices: " + receivedCommand)
    for (String device_id : sonoff_device_ids) {
        logInfo("Sonoff Maintenance","Device: {}", device_id)
        switch (receivedCommand) {
            case "restart" :
                publish("opusMQTT", "cmnd/" + device_id + "/restart", "1") 
            case "queryFW" :
                publish("opusMQTT", "cmnd/" + device_id + "/status", "2")
            case "upgrade" : {
                publish("opusMQTT", "cmnd/" + device_id + "/otaurl", "http://sonoff.maddox.co.uk/tasmota/sonoff.bin")
                publish("opusMQTT", "cmnd/" + device_id + "/upgrade", "1")
            }
        }
    }
    Sonoff_Action.postUpdate(NULL)
end
2 Likes

Oh, so there is a topic that ALL tasmota sonoffs will subscribe to /cmd/sonoffs/status so you don’t have to send a message to each one individually. That makes sense.

1 Like

Hi Rich, thanks for your hints.
The only System started Rule that i have is this:

rule "Look for Pi_Office is online"

when
		//Item Dummy2 changed to ON or
		System started
		
	then
	if (CPU_RasPi_Temp_num.state == 0)
	{
	//logInfo (filename, " Raspi Office Status: " + CPU_RasPi_Temp_num)
	return;
	}
	if (RasPi_online.state == OFF  && CPU_RasPi_Temp_num.state != 0)
	{		
			logInfo (filename, " Raspi ist offline " + ":" + " " + CPU_RasPi_Temp_num)
			CPU_RasPi_Temp_num.postUpdate(0)
			//return;
	}
	else {
			logInfo (filename, " Raspi ist online oder 0 ")
	}
	logInfo (filename, " Raspberry-Status " + ": " + CPU_RasPi_Temp_num)
	

end

As you see no “sonoff_device_ids” in the rule. This rule only looks if my other RasPi is online and shows me here:

The only thing i know is that “sonoffs” is the Group topic-name of all my sonoff-switches (and of course for rest of the tasmota-world too :wink:)


I think that another mechanism must be there to fill that array with my several topics. This is the mystery for me :thinking:.

Sorry, but i do not quite understand what is meant with “populate the array list”. Do you have an example for me ?

Thanks in advance.
Peter

Hi Jürgen,
thanks for response. I took my above rule from the wiki too and stripped it (only using the release check) and for the value-declaration the “lazy-way” as i understand, to not maintain the array list. So i will try your tip and test with it.

In what cases do you use the “restart-function” , and does the “upgrade-function” works for you without problems?

Regards
Peter

The Restart could be used if a device needs a restart. That has not happened for, so far.
And yes the upgrade functions works for me. All devices which are in the list and are online will be updated.
I can’t why it doesn’t seem to work with the GroupTopic, I never used it that way.

sorry guys it take a while to understand, and thank you for your patience. When using the rule like Jürgen the result was as i expected.

2018-11-02 00:33:55.453 [INFO ] [.smarthome.model.script.sonoff.rules] - Tasmota Rel.Vers. set to Items: [pow_01, stehlampe, LampeYork]
2018-11-02 00:33:55.460 [INFO ] [.smarthome.model.script.sonoff.rules] - Sonoff Maintenance: pow_01 / [pow_01, stehlampe, LampeYork]
2018-11-02 00:33:55.465 [INFO ] [.smarthome.model.script.sonoff.rules] - Sonoff Maintenance: stehlampe / [pow_01, stehlampe, LampeYork]
2018-11-02 00:33:55.471 [INFO ] [.smarthome.model.script.sonoff.rules] - Sonoff Maintenance: LampeYork / [pow_01, stehlampe, LampeYork]

and i think this is what Rich meant with “populate the array list”.

val sonoff_device_ids = newArrayList(
    "pow_01",
    "stehlampe",
    "LampeYork"
)
//val sonoff_device_ids = newArrayList("sonoffs")

rule "Sonoff Tasmota Version"
when
		Item Dummy2 changed to ON or
		Channel 'astro:sun:local:noon#event' triggered START
then
		logInfo("sonoff.rules", "Tasmota Rel.Vers. set to Items: " + sonoff_device_ids[0])
		for (String device_id : sonoff_device_ids) {
					logInfo("sonoff.rules", "Sonoff Maintenance: " + device_id + " / " + sonoff_device_ids)
		}
		
/*
		logInfo("sonoff.rules", "Tasmota Rel.Vers. set to Items: " )
		for (String device_id : sonoff_device_ids) {
					publish("peter", "cmnd/" + device_id + "/status", "2")
					logInfo("sonoff.rules", "Sonoff Maintenance: " + device_id + " / " + sonoff_device_ids)
		}
		*/
end

And now after a sleepless night, thinking about the words of Rich

it makes “click” in my brain (if there is one :wink:) and i understand. The Array has only one parameter (sonoffs), but the MQTT-broker gets the “MQTT-Group-topic” and does the work to address all the devices with the same Group-topic. (No more mystery :roll_eyes:). That’s great. Thank you very much again.

Peter

Hi Jürgen, may i ask you some more questions ?

  1. Do you have different typs of Sonoffs or only “Sonoff-Basic”.
  2. When flashing the Sonoffs for the first time, do you use first “minimal-version” and then "normal version ?
  3. Whats about your settings in the “user_config.h” and “platformio.ini” (e.g. SSID, Password, Baud-Rate, upload_port, env_default, etc). Are they lost ?

I ask this, because i got problems when trying to flash via the web-interface
grafik

it neither works with “Update via Web-Server” nor with “Upload from File”. I always get errors. In one case i crashed a Sonoff.

Thanks in advance.
Peter

Yes, I have besides the basics also Dual, TH and RF sonoffs.
When flashing the first time I used a full version. It was something like 6.1., with no error.
I will try to update a single device via OTA to 6.3 later today and report if it is working for me. I’m on a small holiday-trip, so please be patient.

1 Like

I forgot the last question. All settings of tour device will be kept!

The test updating a sonoff didn’t work! One problem migth have been the OTA URL pointing to: " …sonoff.ino.bin" instead of “…sonoff.bin”.
I should have looked more closely before starting! So this remains unfinished until I’m back home. Sorry

Yes, that one thing that makes tasmota really powerful.
I am not going to list the advanced features of that firmware but it really makes sense to use that one.
It is easy enough for beginners and advanced enough so that you can do pretty much anything with it.

Update:
The update seemed to take longer then expected but finally did work, (There wasn’t anything else I could do). I think the problem was caused by a week wifi signal, this device is far away from the wifi-router.
Greetings from Wangerooge…

If only it supported the analog pin on a NodeMCU without recompiling the code I’d use it on all my devices. But I’ve a few of those and so I use ESP Easy. It was easier.

Thanks for your answer. But please enjoy your holiday. All the rest can wait. Have a nice weekend.

Regards - Peter

Sorry to re-open the item but I tried the feature: val sonoff_device_ids = newArrayList(“sonoffs”)
but fail!

How is mqtt setup at your side? Where is the link between reception of the sonoffs-group topic and the update of the array?

Thanks

Hi Bart,
no problem at all. Which rule you have used. The one i’m using

val sonoff_device_ids = newArrayList("sonoffs")

rule "Sonoff Tasmota Version"
when
		Item Dummy2 changed to ON or
		Channel 'astro:sun:local:noon#event' triggered START
then
		logInfo("sonoff.rules", "Tasmota Rel.Vers. set to Items: " )
		for (String device_id : sonoff_device_ids) {
					publish("peter", "cmnd/" + device_id + "/status", "2")
					logInfo("sonoff.rules", "Sonoff Maintenance: " + device_id + " / " + sonoff_device_ids)
		}
end

or the one of Jürgen @opus ?

rule "Sonoff Maintenance"
when
    Item Sonoff_Action received command
then 
    logInfo("Sonoff Maintenance", "Sonoff Maintenance on all devices: " + receivedCommand)
    for (String device_id : sonoff_device_ids) {
        logInfo("Sonoff Maintenance","Device: {}", device_id)
        switch (receivedCommand) {
            case "restart" :
                publish("opusMQTT", "cmnd/" + device_id + "/restart", "1") 
            case "queryFW" :
                publish("opusMQTT", "cmnd/" + device_id + "/status", "2")
            case "upgrade" : {
                publish("opusMQTT", "cmnd/" + device_id + "/otaurl", "http://sonoff.maddox.co.uk/tasmota/sonoff.bin")
                publish("opusMQTT", "cmnd/" + device_id + "/upgrade", "1")
            }
        }
    }
    Sonoff_Action.postUpdate(NULL)
end

I still have a problem with the “upgrade-function”.

Today i tried it with a single sonoff like in this rule.

// Work with a list of selected Sonoff modules

val sonoff_device_ids = newArrayList(
    "pow_01"
)

// OR
// Work with the grouptopic, addressing ALL modules at once
// val sonoff_device_ids = newArrayList("sonoffs")

rule "Sonoff Maintenance"
when
    Item Sonoff_Action received command
then 
    logInfo("sonoff.rules", "Sonoff Maintenance on all devices: " + receivedCommand)
    for (String device_id : sonoff_device_ids) {
        switch (receivedCommand) {
            case "restart" :
                //publish("broker", "cmnd/" + device_id + "/restart", "1")
                logInfo("sonoff.rules", "Sonoff Maintenance: " + device_id + " / " + receivedCommand + " / " + sonoff_device_ids)
            case "queryFW" :{
                publish("peter", "cmnd/" + device_id + "/status", "2")
                logInfo("sonoff.rules", "Sonoff Maintenance: " + device_id + " / " + receivedCommand + " / " + sonoff_device_ids)
            }
            case "upgrade" : {
                //publish("broker", "cmnd/" + device_id + "/otaurl", "http://sonoff.maddox.co.uk/tasmota/sonoff.bin")
                //publish("broker", "cmnd/" + device_id + "/upgrade", "1")
                logInfo("sonoff.rules", "Sonoff Maintenance: " + device_id + " / " + receivedCommand + " / " + sonoff_device_ids)
            }
        }
    }
    Sonoff_Action.postUpdate(NULL)
end

and it failed too. So i think it’s the way how you flashed your sonoff-switches for the first time. I crashed another Sonoff POW when using the web-interface. Even resuscitation via FTDI wasn’t successful :cry:

to flash. At the moment flashing via OTA doesn’t work for me. I have to learn more about it.Until now i didn’t find the right “workflow”, although it’s often said “it’s easy to flash via OTA”.

Have you installed the MQTT-Binding and the MQTT-Action ?. Can be installed via Paper-Ui.
In my mqtt-cfg.file i only changed the line

# URL to the MQTT broker, e.g. tcp://localhost:1883 or ssl://localhost:8883
peter.url=tcp://localhost:1883

all the rest i leaved unchanged. The file is placed in the services-folder

grafik.

I forgot, but it can be that you have to install Mosquitto, but i think it’s already installed.

As i understand no reception of the topic into the array of all the sonoffs group topics is done. For the Array-List is it no matter if you use sonoffs or pow_01 or basic-2340 or whatever, it’s only a parameter of the Array-List. All the rest is done in the publish-Command of the broker. MQTT will look if it’s a single topic or a group-topic and then the full-topic will be done once (topic) or repeated for every topic in the group.

I haven’t tested, but i think when using the Group-topic sonoffs it is not necessary to use an ArrayList, you can also use a String value in your rule and omitting the for - loop and of course a bit changing in the rest of the rule (device_id / sonoff_device_ids) .

Hi Peter,

OUCH, painful tunnel vision. yesterday I was so busy creating arrays/lists that I was too much focusing on this. Indeed the array only contains the group topic and mqtt should deal with it.
Thanks for the eye-opener

In the pasted code the actual upgrade comment is commented out and your broker is set incorrectly (as it’s called peter I think but it says broker). So if by failing you mean it did not run, this seems expected :slight_smile:

I see no reason why flashing via the Sonoff-WebFrontEnd should not work for you. My earlier test did work, only the reconnection to the WiFi took very long (the device is two floors away from the WifiRouter!).
Doing the same via the rule (as posted above) for all connected devices did work during the test last night!

Indeed, you are right. Thanks. I will try again. (After Upgrade Test, i commented it out again, but if broker name is wrong, it’s clear that nothing can happen or crash :wink:)