OpenHab send sensor notification to iOS/Android using IFTTT

Tags: #<Tag:0x00007f616cef6ff8> #<Tag:0x00007f616cef6ee0> #<Tag:0x00007f616cef6df0>

If you are like me and you looking for a way to send various notifications from OH to your iOS/Android phone free, IFTTT is probably the easiest way to do it.

Here are the steps needed:

Register and create applet on IFTTT

  1. Register at https://ifttt.com
  2. Go to the “My Applets” and click on the “New Applet”
  3. In the “Choose Service” box searh for the “Maker”
  4. You will have to enter “Event Name” for the “Receive a web request” trigger, this will be the notification you will call using POST method. Use “Sensor_Alarm” in this example
  5. Chose action service, in this case search for the “Notification”
  6. Enter notifications text: “The event named “{{EventName}}” occurred on the Maker service with {{Value1}} {{Value2}}”
  7. Click “Finish”

Find out IFTTT key used in the API

  1. Click on the username in the top right corner
  2. Choose Services
  3. Click on the “Maker” and choose settings
  4. Paste the URL found in the separate window/tab so you can test the applet
  5. Key is the text behind “https://maker.ifttt.com/use/

I used the IFTTT to send notifications when my garage or front door are opened/closed. Below is the rule that I use on my zwave sensors. For this rule to work, you will need to enable persistence using “everyUpdate” strategy. If you use default “everyChange” you will not be able to detect updates that are not changing state. This will trigger false alarms.

First create group and items:
/etc/openhab2/items/zwave.items

Group gSensor_Notification
Contact     Z110_Garage_Sensor_Door     "Garage Door [%s]"      <Door>          (gSensor_Notification)
Contact     Z110_Entrance_Sensor_Door   "Garage Door [%s]"      <Door>          (gSensor_Notification)

Add items to persistence:
/etc/openhab2/persistence/rrd4j.persist

Strategies {
    // for rrd charts, we need a cron strategy
    everyMinute : "0 * * * * ?"
}
Items {    
    gSensor_Notification* : strategy = everyUpdate, everyMinute, restoreOnStartup
}

And here is the rule that will send notifications using IFTTT:
/etc/openhab2/rules/notify_sensors.rules

rule "Send IFTTT Notification for Sensors in Specific Group"

    when
        // Assign all sensor that will require notification together in one gorup
        Item gSensor_Notification received update

    then
        // IFTTT parameters
        var String ifttt_key = "__put__your__own__key__here__"
        var String ifttt_event = "Sensor_Alarm"
        var String ifttt_url = "https://maker.ifttt.com/trigger/" + ifttt_event + "/with/key/" + ifttt_key
        var String notification_json

        // Get the last item that was updated
        var lastItem = gSensor_Notification.members.sortBy[lastUpdate].last
        // Avoid sending the alert if the state didn't change
        // Compare state before update and now
        var lastState = lastItem.historicState(lastItem.lastUpdate.toDateTime.minusSeconds(1))

        //Check if last state is different than the current state. Zwave sensors send status reports on predefined intervals. This will trigger lastUpdate filed regardless of the sensor state
        if (lastState.state != lastItem.state) {
            notification_json = '{"value1":"' + lastItem.name + '","value2":"' + lastItem.state + '"}'
            sendHttpPostRequest(ifttt_url, "application/json", notification_json)
            logInfo("IFTT",    "IFTTT: ALARM - " + lastItem.name + " changed state from: " + lastState.state + " to: " + lastItem.state)
        }
        else {
            // State didn't changed, supress the notification
            logInfo("IFFT", "IFFT: SUPPRESED - " + lastItem.name + " changed state from: " + lastState.state + " to: " + lastItem.state)
        }

end
4 Likes

Thanks! Great tutorial for how to connect openHAB to IFTTT through the Maker channel. I moved this to Tutorials and Examples.

For completeness, other alternatives (all equally easy IMHO) include:

  • myopenhab.org with openHAB Cloud Connector and the sendNotification Actions
  • can send notifications to phones with openHAB app installed
  • can receive online/offline notifications if openHAB itself goes down
  • myopenhab.org with openHAB Cloud Connector and IFTTT using the openHAB channel
  • replace the Maker channel in the example above with the openHAB channel and the sendHttpPostRequest with a sendNotification
  • Any one of the notification addons including NotifyMyAndroid, Prowl, Pushover, Pushsafer, Telegram, and XMPP.
  • each of these addons define their own Action to call to send the notification
  • MQTTWarn which opens up even more (almost 60) notification and database services to openHAB through MQTT

Finally, I’ll point users to the Separation of Behaviors design pattern for a way to centralize your notification logic so it is easy to experiment with lots of different approaches for notifications and set up logic to use different notification mechanisms based on time, system state, etc. For example, send notifications through one service during the day but use an alternative one at night.

Sorry to hijack your tutorial. This looked like a good place to document the other ways as well for future readers.

5 Likes

I wish i know about this before. Thanks for the alternatives and design pattern definitely looks interesting, I’ll try to rewrite my rules using this logic.

Thanks

I’m sorry, but is this tutorial up to date? I’ try it today, but on the action "3. In the ‘Choose Service” box search for the “Maker”’ I’ found 4 services and a lot of applets, but I’m afraid, not what you mean.?

There is a possibility, to connect IFTTT to the openhab cloud service, but I can’t connect there, because I use the reverse proxy with https. Or how I can connect to the openhab cloud with the reverse proxy and https? Any ideas?

The notifications seems very difficult, I’d play around since a long time with some third party tools (MQTT, XMPP…) and without an useful result.
The goal: I’d installed 5 zwave motion sensors, and these sensors should create an alarm on my android (5.1) phone, if one sensor switch on.
Why we can’t send the notifications direct from any openhab rules to the habdroid app like a e-mail to the e-mail-client?

Sincerely
Jens

It appears they renamed the channel to Webhooks. But as far as I can tell it works the same.

There is nothing preventing you from doing both. OH connects to my openhab.org through the Cloud Connector binding which is independent of the reverse proxy. You can host your own instance of the myopenhab.org server as well.

We can. But to do so you need to have a static we web address to publish the messages to and then the Android openHAB app retrieved the messages from that. This is part of the service that myopenhab.org provides.

There are lots of other third party services that have OH actions including Prowl, PushBullet, Telegram, Notify My Android, and more.

thank you for the fast reply.

OH connects to my openhab.org through the Cloud Connector binding which is independent of the reverse proxy.

So I must take a mistake. I’ll try it again.

We can. But to do so you need to have a static web address to publish the messages to

My OpenHab server on my raspi has a static web adress (not DynDNS). So it should work. Is there any tutorial or example, how I must do?

Thanks
:slight_smile: Jens

In addition you the static address you also need a server running there at that address to received the messages and the phones can retrieve them. For the Android openHAB app that server is myopenhab.org.

See https://github.com/openhab/openhab-cloud/blob/master/README.md for how to set up your own instance of the Cloud Service.

But all things considered, since you are willing to rely on IFTTT you are clearly not against using cloud services. So why not save a lot of work and use myopenhab.org in the first place? Or if the only thing you are needing is notifications, why not rely on a third party notification service like one of those I already mentioned?

Hallo,

now I’m understand- there must be a separate tier between openhab and the android device- the direct connection doesn’t work.
So I’ll try to connect to openhab cloudservice. I must check my firewall, then it should work.

Thanks for your replies
Jens

Does someone know why the IFTTT recipe would take an hour to send a notification? Can we speed it up?
The real goal of it is not to send a notification but to turn on a light connected via wifi which as a IFTTT channel. It would be the easiest way to do so, but an hour to turn on a light is not acceptable

I´m currently having the same issues.
I wanted to change the Motion Detection of some cameras that only support IFTTT.
The current way is: openHAB -> myopenhab -> IFTTT -> Camera
I noticed that the IFTTT applet runs an hour after the item changed in openHAB.
This isn´t nice but ok when leaving, but it´s not ok when coming back and the cameras are still online to record when motion is detected.

I´m going to try the direct way from openHAB to IFTTT through webhooks.
Atleast for my Geofency iBeacon this works just in time.

kind regards
Michael

Did you get anywhere with the webhooks way?
Did it speed up the applet running?
Was there any changes to the original rule with it now being webhooks v the old maker

I want to do the same for my camera / presence detection for exactly the same reason, the myopenhab cloud link makes the applet run within an hour which is far to many motion detection notices on my phone for it to be acceptable.

Yes and yes.
I´m using the webhooks and it works almost without a delay.

I build a total of 6 webhooks for my two cameras.
Both ON/OFF, Camera1 ON/OFF, Camera2 ON/OFF

Great, that’s the answer I hoped you would give…sound perfect for what I’m after.

Did you have to change much of the code in the first post to work with webhooks rather than the old maker or have IFTTT effectively just changed the name?

Been playing with this and I’m almost there but scratching my head a little…

Here’s my code

rule “Send IFTTT Notification for Presence in vPresent”

when
    // Presence Sensor Variable changes state
    Item vPresent changed

then
if(vPresent.state == ON)
{
	logInfo("vIFTTTPresence", "IFTTT - Nothing to Do")
}
else
{
    // IFTTT parameters
    var String ifttt_key = "my_key_in_here"
    var String ifttt_event = "my_IFTTT_variable_here"
    var String ifttt_url = "https://maker.ifttt.com/trigger/" + ifttt_event + "/with/key/" + ifttt_key
	sendHttpPostRequest(ifttt_url)
	logInfo("vIFTTTPresence", "IFTTT - Sending Presence state to IFTTT")
}

Now, it works in that it sends the webhook and the applet in IFTTT runs but for some reason it wants to now reset my Presence to off all the time - yet my presence variable ‘vPresent’ is controlled by my iPhone presence rule…I’m trying to get this rule to run as a slave based in when I go out / come back and my iPhone presence changes the vPresent to OFF / ON and this rule then runs.

Where am I going wrong?

EDIT > Fixed, I’ve updated the above code, my ‘if’ was wrong, it should be

if(vPresent.state == ON) not if(vPresent.state != ON) (essentially I got confused as to what state I wanted it to run against and had it !=, ie not equal to, rather than ==, ie equal to)

Plus, I also had a restart rule on the .rules file which set the vPresent state to OFF and then checked for status of the devices that determine my presence so I think it ended up in a recurring loop…

EDIT 2 > The applet is now running seconds after the webhook rule runs rather than the ‘within an hour’ using the OH applet…problem so far solved!