[SOLVED] Sonoff dual - Roller shutter

logDebug() will only output something if you set the log level to DEBUG, this is done via karaf console by using the command

log:set DEBUG org.eclipse.smarthome.model.script.sonoff

As I created two rules to control the sonoff, I wanted to get some difference between the output, but I wanted the same logger to be used (named “sonoff”)

I added this to the end of the rule to command opening and closing from GH. It is also necessary to create an item with the “Switchable” tag for GH. It seems to work well, the only flaw that I can’t correct is a small input to the motor at the end of the stroke. Example: 100% shutter (closed) I send the closing command and should do nothing, instead it activates the motor for a moment.

rule "google home up"
when
   Item rolleshutter received command ON
then 
     if (Sonoff6.state !== 0 )  {	
	  Sonoff6.sendCommand("0")
	 } 
         	  	
	end 
	
	rule "google home down"
when
   Item rollershutter received command OFF
then 
     if (Sonoff6.state !== 100 ) {
	 Sonoff6.sendCommand("100")
	 }    
             	
	end 

Rules can be picky about comparing percentage types; try

if ( (Sonoff6.state as Number) !== 0 )  {

thanks for your help. I found the error:

if (Sonoff6.state ! = 0 )

instead of:

if (Sonoff6.state !== 0 )
1 Like

Please don’t mix up things:

= -> set var to, e.g. i = 4
== -> compare the value, is it the same? e.g. if(i == 4)
=== -> compare if identical, e.g. if(i === null)
!= -> not the same value
!== not identical

So, if comparing a value to null, you should use === or !==, if comparing to a value (like 0), you have to use == or !=.

1 Like

ok, thanks, I mark the reminder of the examples

why if I restart OH it does not update the STATE and I have to use the buttons on the wall?

What is your current configuration of items and things, relating to sonoff?

when I restart OH received this message:

[WARN ] [rest.core.item.EnrichedItemDTOMapper] - Failed transforming the state 'NULL' on item 'mqttSonoff8state' with pattern 'JSONPATH($.Wifi.RSSI):%s %%': Invalid path '$.Wifi.RSSI' in 'NULL %' 

I have no things configured in paperUi
I only entered in the item file:

Switch mqttSonoff8_1        "SOnOff 8-1 [%s]"                        					 										{mqtt=">[mosquitto:cmnd/shelly-soggiorno/POWER1:command:*:${command}],<[mosquitto:stat/shelly-soggiorno/POWER1:state:REGEX((.*))]"}
Switch mqttSonoff8_2        "SOnOff 8-2 [%s]"                          				  	 										{mqtt=">[mosquitto:cmnd/shelly-soggiorno/POWER2:command:*:${command}],<[mosquitto:stat/shelly-soggiorno/POWER2:state:REGEX((.*))]"}
String mqttSonoff8lwt       "SOnOff soggiorno   [%s]"                                       				 					{mqtt="<[mosquitto:tele/shelly-soggiorno/LWT:state:default]" }
String mqttSonoff8state     "SOnOff 8   [JSONPATH($.Wifi.RSSI):%s %%]"                      				 					{mqtt="<[mosquitto:tele/shelly-soggiorno/STATE:state:default]" }
Rollershutter Sonoff8       "soggiorno   [%d%%]"                                     (tapparelle,sonoff)              			{ autoupdate="false"}
Switch tapparella_soggiorno	"tapparella soggiorno "         	     <rollershutter> (tapparelle,sonoff)    [ "Switchable" ]    { autoupdate="true"} 

and the sitemap:

Default item=Sonoff8
       Selection item=Sonoff8 mappings=[ 0="0%",25="25%",50="50%",75="75%",100="100%" ]

furthermore I noticed that with the closing percentages (25% … 50% … 75%) they do not match … example 50% and almost 75% of closure

a specification, as a broker I use mosquitto is a 1.x binding there is no need for things … right?

Jepp, if using mqtt1, there is no thing at all. In question of tele, these items will be updated with a given frequence (imho 600 seconds default). As openHAB will not ask for the current value (and mosquitto wont tell it if not retained, even if asked for…)
By the way: retain has to be set by the publisher, not the subscriber :wink:

You will have to wait until your shelly sends an update for tele/# channels

so, if I understand correctly, I have to set:
mosquitto.retain = true
because by setting poweretain on Tasmota, OH after the reboot it does not keep the last value, even waiting for the teleperiod update time (in my case it is 300)

As far as I understood, retain has to be set per topic. This is done by the publisher.

I also have other peripherals with the Tasmota firmware connected with mosquitto, and yesterday, restarting OH I realized that they also don’t receive the update and I have to do it from OH

@Udo_Hartmann: I want to use this code in Openhab2.4 mqtt2 but I don’t know how to create the Things (channels) for this two rows from Udo’s Item file:

Switch mqttSonoff6_1 "SOnOff 6-1 [%s]" {mqtt=">[mosquitto:sonoff_t6/cmnd/POWER1:command:*:${command}],<[mosquitto:sonoff_t6/stat/POWER1:state:REGEX((.*))]"}
Switch mqttSonoff6_2 "SOnOff 6-2 [%s]" {mqtt=">[mosquitto:sonoff_t6/cmnd/POWER2:command:*:${command}],<[mosquitto:sonoff_t6/stat/POWER2:state:REGEX((.*))]"}

Easy. Bridge, Thing, Channels:

Bridge mqtt:broker:mosquitto "Mosquitto" [
    host="127.0.0.1",                // or whatever the ip of the broker is
    port=1883,                       // same for port
    clientID="openHAB2-2"           // client id of openHAB in the broker
 ] {
     Thing topic sonoff6 "sonoff 6" @ "mqtt" {
        Channels:
            Type switch : ch1 "Power" [ stateTopic= "sonoff_t6/stat/POWER1", commandTopic="sonoff_t6/cmnd/POWER1" ]
            Type switch : ch2 "Power" [ stateTopic= "sonoff_t6/stat/POWER2", commandTopic="sonoff_t6/cmnd/POWER2" ]
     }
 }

Items:

Switch mqttSonoff6_1 "SOnOff 6-1 [%s]" { channel="mqtt:topic:mosquitto:sonoff6:ch1" }
Switch mqttSonoff6_2 "SOnOff 6-2 [%s]" { channel="mqtt:topic:mosquitto:sonoff6:ch2" }

There are several other options for the bridge, e.g. LWT, username, password… if you use the standard port (1883), you can omit this parameter, too. If you don’t set the clientID, openHAB will use a random id (and afaik it will change after restarting openHAB, so it’s better to set this parameter)

@Udo_Hartmann: Thanks. Do I need the REGEX state? With this files I see the Wifi RSSI value in my sitemap, but their is no action on the Jinvoo curtain switch if I select a % value.
Interlock 1, Setoption80 0
Or does it works also in Rollershuter mode with one thing: …[stateTopic=“stat/JalloJin6/SHUTTER1” , commandTopic=“cmnd/JalloJin6/SHUTTERPOSITION”, on=“0”, off=“100” ]? I use some of this devices with this config, but without % value.

Thing topic JalloJin6 "JalloJin 6" {
    Channels:
        Type switch : ch1 "Power" [ stateTopic= "JalloJin6/stat/POWER1", commandTopic="JalloJin6/cmnd/POWER1", on="ON", off="OFF" ]
        Type switch : ch2 "Power" [ stateTopic= "JalloJin6/stat/POWER2", commandTopic="JalloJin6/cmnd/POWER2", on="ON", off="OFF" ]
        Type string : devicestate "Jallo-Test [%s]" [ stateTopic="tele/JalloJin6/LWT" ]
        Type string : wifi-rssi "Wifi RSSI" [ stateTopic="tele/JalloJin6/STATE", transformationPattern="JSONPATH:$.Wifi.RSSI" ]
}
Items:

```csv
Switch mqttJalloJin6_1 "JalloJin 6-1 [%s]" {channel="mqtt:topic:embedded-mqtt-broker:JalloJin6:ch1" }
Switch mqttJalloJin6_2 "JalloJin 6-2 [%s]" {channel="mqtt:topic:embedded-mqtt-broker:JalloJin6:ch2" }
String mqttJalloJin6LWT "JalloJin 6 LWT [%s]" {channel="mqtt:topic:embedded-mqtt-broker:JalloJin6:devicestate"}
String mqttJalloJin6state "JalloJin 6 WIFI [JSONPATH($.Wifi.RSSI):%s %%]" {channel="mqtt:topic:embedded-mqtt-broker:JalloJin6:wifi-rssi"}
Rollershutter JalloJin6 "JalloJin 6 [%d%%]" { autoupdate="false" }

added postcommand=“true”, OpenHab Log: Item ‘JalloJin6’ received command 50 (found it in the german forum, also written from you :grinning:)
I think I have some failure in the rule that I have copied above or I have to add the “Log Debug”.
Is it correct that I have to use the following commands in the console via e.g. Putty for Openhab 2.4?
bash> openhab-cli console
openhab2> log:set DEBUG org.eclipse.smarthome.model.script.mylogger
openhab2> logout

var long intStartTime
var Timer tMqtt6 = null
var boolean lMqtt6 = false

rule "JalloJin6"
when
    Item mqttJalloJin6_1 changed or
    Item mqttJalloJin6_2 changed
then
    logDebug("JalloJin","6: {} is {}",triggeringItem.name,triggeringItem.state)
    if(triggeringItem.state==ON) {
        intStartTime=now.millis
        logDebug("JalloJin","6: StartTime= {}",intStartTime)
    }
    else {
        val long myPercent = (now.millis - intStartTime)/200
        logDebug("JalloJin","6: StopTime= {}; Duration= {}",now.millis, now.millis-intStartTime)
        logDebug("JalloJin","6: Percent= {}",myPercent)
        if (!(JalloJin6.state instanceof Number)) JalloJin6.postUpdate(0)
        if (triggeringItem.name == "mqttJalloJin6_1") {
            JalloJin6.postUpdate(if(((JalloJin6.state as Number)-myPercent) > 0 ) (JalloJin6.state as Number)-myPercent else 0)
        }
        else {
            JalloJin6.postUpdate(if(((JalloJin6.state as Number)+myPercent) < 100 ) (JalloJin6.state as Number)+myPercent else 100)
        }
    }
end

rule "JalloJin6 2"
when
    Item JalloJin6 received command
then
    logDebug("JalloJin","6-2: received command: {}",receivedCommand)
    if(receivedCommand instanceof Number) {
        logDebug("JalloJin","6-2: received command is Number")
        lMqtt6 = true
        if(tMqtt6 !== null) tMqtt6.cancel
        val Integer iDuration = (((receivedCommand as Number) - (JalloJin6.state as Number)) * if((receivedCommand as Number) > (JalloJin6.state as Number)) 200 else -200).intValue
        logDebug("JalloJin","6-2: Duration: {} mSec",iDuration)
        if((receivedCommand as Number) < (JalloJin6.state as Number)) {
            logDebug("JalloJin","6-2: UP!")
            mqttJalloJin6_1.sendCommand(ON)
        }
        else {
            logDebug("JalloJin","6-2: DOWN!")
            mqttJalloJin6_2.sendCommand(ON)
        }
        tMqtt6 = createTimer(now.plusMillis(iDuration), [
            logDebug("JalloJin","6-2: STOP!")
            if(mqttJalloJin6_1.state == ON) mqttJalloJin6_1.sendCommand(OFF)
            if(mqttJalloJin6_2.state == ON) mqttJalloJin6_2.sendCommand(OFF)
            tMqtt6 = null
            lMqtt6 = false
        ] )
    }
    else if (!lMqtt6)
        switch (receivedCommand) {
            case UP: mqttJalloJin6_1.sendCommand(ON)
            case DOWN:mqttJalloJin6_2.sendCommand(ON)
            case STOP: {
                if(mqttJalloJin6_1.state == ON) mqttJalloJin6_1.sendCommand(OFF)
                if(mqttJalloJin6_2.state == ON) mqttJalloJin6_2.sendCommand(OFF)
            }
        }
end

Log file output regarding the rule:
[ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘JalloJin6 2’: Could not cast NULL to java.lang.Number; line 38, column 66, length 25
[ERROR] [ui.internal.items.ItemUIRegistryImpl] - transformation throws exception [transformation=org.eclipse.smarthome.transform.jsonpath.internal.JSonPathTransformationService@1620c27, value=86 %]
[vent.ItemStateChangedEvent] - mqttJalloJin6state changed from 92 to 90

As in the configuration there is no reference to REGEX, you don’t need REGEX. Don’t use on=“ON” and off=“OFF” as parameters, as you don’t need them.
You are using the JSONPATH Transformation Service twice (once in the channel, second in the label of the linked item, that doesn’t make sense.

In question of the Error: The first error may emerge because JalloJin6.state isn’t of type Number (yet). You should test this before using it. (like receivedCommand)
2nd Error… maybe tele/JalloJin6/STATE has sometimes no vaild path to Wifi.RSSI (Payload changes from time to time)

After I had selected “50%” I see the following in the Log (it looks like a Number):
[ome.event.ItemCommandEvent] - Item ‘JalloJin6’ received command 50
[ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘JalloJin6 2’: Could not cast NULL to java.lang.Number; line 38, column 66, length 25

Sitemap is:

Selection item=JalloJin6 mappings=[ 0="0%",25="25%",50="50%",75="75%",100="100%" ]