[SOLVED] OpenHAB, MQTT and Sonoff help

Hi folks.

I’m trying to get a fairly basic openhab setup working.

Thus far i have got mosquito setup and working fine, the sonoff installed with the Tasmota firmware and talking to mosquito.

I’ve also added an “item” within openhab pointing at the sonoff device.

What i’m stumped with is how exactly i can control it?

If i go into PaperUI, i can see it listed in items, but theres no button. The “control” screen is blank. I tried to add a “thing” but it goes to the “bindings” page and its also blank.

I managed to go into HabPanel, and created a dashboard and added a button linked to the item, and that works fine. Clicking the switch turns the sonoff on and off, and manually switching the sonoff also switches it on and off on the interface.

But it looks clunky, and doesnt seem to integrate with the mobile app. When i open the app its just blank, theres nothing showing.

What i would like to achieve, is two things.

  1. I would like an App on my phone (and perhaps an equivalent web interface), to display the two buttons showing their state and allowing me to toggle them on and off. If i load the App just now, it just seems blank.

  2. I would like to configure a timer, so that one of the switches is turned on at midnight, and turned off again at 7am.

You need to set up a sitemap and put that item there:

You need a rule and could use a cron trigger for that:


Setup the sitemap last night and got that working. Not tackled the cron part yet.

I’m a bit puzzled why there is a nice interface where it looks like you should be able to set everything up, and then instead you have to manually edit the config files?

I’m also wondering if theres some sort of feedback loop or checking functionality. For instance, i noticed that if i turned the sonoff off with its physical button, the switch in the interface changed position to reflect that it was now off. So clearly the status signal from the sonoff is making it back to the interface.

However if i unplugged the sonoff, i could switch it “on” and the interface sat showing on, despite the fact that it wasnt actually powered up. Is there a way to check the status of the device periodically and either try to make the state match the interface, or update the interface to show that its not infact on.


An alternative approach is not to change the state in response to the UI command in the first place, but wait for an update from the real device. Look into {autoupdate="false} for your Item.

thanks, i’ll look into autoupdate=false.

I was more thinking about the timer actually. If something odd happens at midnight and the device misses the trigger, it would be handy if the system tried again a few times to get it to turn on.

I guess i could just have another few cron entries.

Bit of playing at the weekend and i’ve come up with this, modified from one of the wiki examples:

import org.joda.time.*
import org.openhab.model.script.actions.Timer

var Timer tEVCharger

rule "Turn on EV charger"
        Time cron "0 0 12 * * ?"   // Every day 00:12 hours
         * EV Charger
         // Cancel timer to avoid reschedule
        if(tEVCharger!=null) {
        logInfo("EVChargerCron","Timer tEVCharger cancelled")
        sendCommand(EV_Charger, ON) //turn on charger
        logInfo("EVChargerCron","Timer tEVCharger created")
        tEVCharger = createTimer(now.plusMinutes(15)) [|
                logInfo("EVChargerCron","Timer tEVCharger executed")
                sendCommand(EV_Charger, ON) //every 15 minutes resend on command
                tEVCharger.reschedule (now.plusMinutes (15))

rule "Stop EV Charger"
                Time cron "0 0 8 20 * ?" //turn charger off at 8:20am
                if(tEVCharger!=null) {
                        logInfo("EVChargerCron","Timer tEVCharger cancelled - its morning")
                sendCommand(EV_Charger, OFF)

Seems to work fine (i tested it with different times during the day, and tried both manually shutting it off, and also having it unpowered). Essentially it resends the “on” command every 15minutes during the active period.

As for the auto-update thing that acts slightly oddly. In the browser, if i click on when the device is offline, the UI does immediately switch to on, however refreshing the page it appears back at off again.

1 Like