[SOLVED] Tasmota MQTT with config files

Hi there all,

I’m trying to connect my tasmota (sonof) device with my openhab server. Somehow, it doesn’t work. Since I like coding I’m trying with the config files. But it get’s messed up.
What does work:
Embedded broker is online and working.
If i use my pc as a client, I can command the tasmota

So I assume that the fault is in my code. So I post it here. Maybe you see some error?

default.items file:

Switch Bathroom_Tasmota_Switch "Boiler" <switch>
Switch Bathroom_Tasmota_Relay "Tasmota" <switch> {channel="mqtt:topic:mosquitto:Bathroom_Boiler_Control:Bathroom_Boiler_Relay" }

default.things file:

Bridge mqtt:broker:mosquitto [ host="192.168.178.39", secure=false ]
{
    Thing mqtt:topic:Bathroom_Boiler_Control "Bathroom Boiler Control" {
    Channels:
        Type switch : Bathroom_Boiler_Relay [ stateTopic="stat/tasmota-switch/POWER", commandTopic="cmnd/tasmota-switch/POWER", on="ON", off="OFF" ]        
    }
}

default.rules file:

rule "Tasmota_ON"
when
    Item Bathroom_Tasmota_Switch changed from OFF to ON
then
    Bathroom_Tasmota_Relay.sendCommand(ON)
end

rule "Tasmota_OFF"
when
    Item Bathroom_Tasmota_Switch changed from ON to OFF
then 
    Bathroom_Tasmota_Relay.sendCommand(OFF)
end

I guess my sitemap is not really needed sinde I dond’t get error’s here.
My problem is or in my things file, or in my items file. One of the 2

Did it yesterday and comparing your files to mine, I see a differnce here:
Thing topic Sonoff_Test1 “Sonoff Test1” @ “Wohnzimmer” {

Hi FSchubert,

Thx for the fast reply. What is this @standing for then. To what does it relate? Because in some examples i find it, and in others i dont.

I don’t know yet, copied from the examples.

You will see this @whatever in PaperUI on top tab’s when you click on Control.

1 Like

Can you post the logs you get from fronttail when trying to use the rule above.

Sitemap has nothing to do with your rules. Can you turn on/off the switch using BasicUI?

Maybe try the rule this way and see what happens.

rule "Tasmota_ON"
when
    Item Bathroom_Tasmota_Switch changed
then
    if (Bathroom_Tasmota_Switch.state == OFF)
        Bathroom_Tasmota_Relay.sendCommand(ON)
end

rule "Tasmota_OFF"
when
    Item Bathroom_Tasmota_Switch changed 
then
    if (Bathroom_Tasmota_Switch.state == ON) 
        Bathroom_Tasmota_Relay.sendCommand(OFF)
end

Also note if you are using OH version 2.4 you will need to restart OH each time you make a change to your MQTT item file. This has been fixed in OH 2.5 milestone versions.

So I changed the code in the .things file to:

Bridge mqtt:broker:mosquitto [ host="192.168.178.39", secure=false ]
{
    Thing mqtt:topic:Bathroom_Boiler_Control "Bathroom Boiler Control" @ "Haardvuur Controller"{
    Channels:
        Type switch : Bathroom_Boiler_Relay [ stateTopic="stat/tasmota-switch/POWER", commandTopic="cmnd/tasmota-switch/POWER", on="ON", off="OFF" ]        
    }
}

logtail:

==> /var/log/openhab2/events.log <==
2019-11-17 16:22:07.696 [hingStatusInfoChangedEvent] - 'mqtt:broker:mosquitto' changed from OFFLINE to ONLINE
2019-11-17 16:22:07.702 [me.event.ThingUpdatedEvent] - Thing 'mqtt:broker:mosquitto' has been updated.
2019-11-17 16:23:33.450 [ome.event.ItemCommandEvent] - Item 'Bathroom_Tasmota_Switch' received command ON
2019-11-17 16:23:33.473 [vent.ItemStateChangedEvent] - Bathroom_Tasmota_Switch changed from OFF to ON
2019-11-17 16:23:33.504 [ome.event.ItemCommandEvent] - Item 'Bathroom_Tasmota_Relay' received command ON
2019-11-17 16:23:33.512 [nt.ItemStatePredictedEvent] - Bathroom_Tasmota_Relay predicted to become UNDEF
2019-11-17 16:23:37.193 [ome.event.ItemCommandEvent] - Item 'Bathroom_Tasmota_Switch' received command OFF
2019-11-17 16:23:37.206 [vent.ItemStateChangedEvent] - Bathroom_Tasmota_Switch changed from ON to OFF
2019-11-17 16:23:37.226 [ome.event.ItemCommandEvent] - Item 'Bathroom_Tasmota_Relay' received command OFF
2019-11-17 16:23:37.234 [nt.ItemStatePredictedEvent] - Bathroom_Tasmota_Relay predicted to become UNDEF
2019-11-17 16:29:25.459 [ome.event.ItemCommandEvent] - Item 'Bathroom_Tasmota_Switch' received command ON
2019-11-17 16:29:25.474 [vent.ItemStateChangedEvent] - Bathroom_Tasmota_Switch changed from OFF to ON
2019-11-17 16:29:25.499 [ome.event.ItemCommandEvent] - Item 'Bathroom_Tasmota_Relay' received command ON
2019-11-17 16:29:25.510 [nt.ItemStatePredictedEvent] - Bathroom_Tasmota_Relay predicted to become UNDEF
2019-11-17 16:29:27.776 [ome.event.ItemCommandEvent] - Item 'Bathroom_Tasmota_Switch' received command OFF
2019-11-17 16:29:27.801 [vent.ItemStateChangedEvent] - Bathroom_Tasmota_Switch changed from ON to OFF
2019-11-17 16:29:27.817 [ome.event.ItemCommandEvent] - Item 'Bathroom_Tasmota_Relay' received command OFF
2019-11-17 16:29:27.825 [nt.ItemStatePredictedEvent] - Bathroom_Tasmota_Relay predicted to become UNDEF

So There’s some action when I use the switch. But the mqtt code never gets to the tasmota.

Did you create a generic mqqt thing in PaperUI?

You have two items and only one with a channel? Is this a sonoff basic?

Here is example of sonoff basic. The top one has temp and humidity sensor connected to GPIO pins.

Things file

Bridge mqtt:broker:pibroker "pibroker" [ host="10.0.1.12", port=1883, secure=false, username="MyUsername", password="XXXXXXX" ]
{
    // Sonoffs
    Thing topic sonoff11 "Living Room Light" @ "Living Room" {
    Channels:
        Type switch : power       "Power"         [ stateTopic="stat/sonoff11/POWER", commandTopic="cmnd/sonoff11/POWER" ]
        Type number : temperature "Temperature"   [ stateTopic="tele/sonoff11/SENSOR", transformationPattern="JSONPATH:$.SI7021.Temperature" ]
        Type number : humidity    "Humidity"      [ stateTopic="tele/sonoff11/SENSOR", transformationPattern="JSONPATH:$.SI7021.Humidity" ]
    }

    Thing topic sonoff2 "Couch Light" @ "Couch Light" {
    Channels:
        Type switch : power        "Power"         [ stateTopic="stat/sonoff2/POWER", commandTopic="cmnd/sonoff2/POWER" ]
    }
}

Items file:

Switch LivingRoom_Light "Living Room Light" <light>  ["Lighting"] { channel="mqtt:topic:pibroker:sonoff11:power" }
Number LivingRoom_Light_Temp "Temperature [%.1f °F]"      <temp>             { channel="mqtt:topic:pibroker:sonoff11:temperature" }
Number LivingRoom_Light_Humidity    "Humidity [%.1f %%]"    <humidity>       { channel="mqtt:topic:pibroker:sonoff11:humidity" }

Switch CouchLight "Couch Light" <light>  ["Lighting"]  { channel="mqtt:topic:pibroker:sonoff2:power" }

Simple rule, using cron to turn on/off the couch light at a certian time.

rule "Turn Couch Light On"

when

	Time cron "0 30 18 ? * *"

then

	CouchLight.sendCommand(ON)

end

rule "Turn Couch Light Off"

when

	Time cron "0 30 23 ? * *"

then 

	CouchLight.sendCommand(OFF)

end

Thx a lot for all the replies!

Yes, I’m on OH2.4 and never did a reboot. I also never made a generic thing in the paper UI. I assumed that the code would do the trick. Since my brooker came in the UI that way. But I can be mistaken!

It’s a sonoff basic indeed. I use 2 Item’s as well. One for the switch defined in my basic UI and then one for the handling to MQTT. Some reasoning mistake as well?

If I make a generic mqtt thing. I’ll call it

Bathroom_Boiler_Control

and the channel:

Bathroom_Boiler_Relay

correct?

I see here that you have defined

["Lightning"]

I don’t have this. What’s the function here?

Name it what you like but that’s where the channel is linked to the item. Some recommend that you create the Thing using PaperUI only and then files for items. I’ve never tried it that way, as you see above, but toy with it and see what happens.

As for the rule I would start with being able to turn on/off the switch using BasicUI first. Then move to rules.

That is a tag used for controlling via Alexa. You can omit that if you do not plan to use Alexa or Google device.

Yep, meanwhile I’ve made this generic thing with the same name as in my code. PaperUI says its’s online.

I can reboot and try again.

Make sure you do not have simple mode turned on in PaperUI, else you may end up with duplicate items.

You only need to restart if changing/editing the actual file in /etc/openhab2/items. Are you using VSCode or other for editing files?

From command line you do not have to reboot just use sudo systemctl stop openhab2 then sudo systemctl start openhab2. It is also a good idea to stop OH before rebooting as well.

Wow you really know how to debug. Simple mode is indeed on! Because I followed that instruction.

So after reboot:

2019-11-17 17:04:42.776 [ome.event.ItemCommandEvent] - Item 'Bathroom_Tasmota_Switch' received command ON
2019-11-17 17:04:42.795 [vent.ItemStateChangedEvent] - Bathroom_Tasmota_Switch changed from NULL to ON
2019-11-17 17:04:44.008 [ome.event.ItemCommandEvent] - Item 'Bathroom_Tasmota_Relay' received command OFF
2019-11-17 17:04:44.016 [nt.ItemStatePredictedEvent] - Bathroom_Tasmota_Relay predicted to become NULL
2019-11-17 17:04:47.989 [ome.event.ItemCommandEvent] - Item 'Bathroom_Tasmota_Switch' received command OFF
2019-11-17 17:04:48.004 [vent.ItemStateChangedEvent] - Bathroom_Tasmota_Switch changed from ON to OFF
2019-11-17 17:04:48.031 [ome.event.ItemCommandEvent] - Item 'Bathroom_Tasmota_Relay' received command ON
2019-11-17 17:04:48.043 [nt.ItemStatePredictedEvent] - Bathroom_Tasmota_Relay predicted to become NULL

I’m still learning and figuring it out as I go.:smiley:

FYI, NULL - indicates an Item is not yet initialized. You only need a switch item, not sure why you have both relay and switch unless your using it as some type of proxy item.

Now I’m afraid I’ve to many items. (ignore the online thing, that’s just to see if my phone is in the wifi or not)

To answer your question:
I thought: 1 device to define the switch on my basic UI and 1 devide to define the relay I want to control. But I may be wrong

You can use this single rule for both.


rule "Tasmota"
when
    Item Bathroom_Tasmota_Switch changed 
then
    if (Bathroom_Tasmota_Switch.state == ON) {
        Bathroom_Tasmota_Switch.sendCommand(OFF)
    }

    else if (Bathroom_Tasmota_Switch.state == OFF){
         Bathroom_Tasmota_Switch.sendCommand(ON)
    }
end