IFTTT to Trigger an automation with conditions

I would like to use IFTTT to trigger an automation.

In the past I’ve used scripts for this, but the automation I’m building requires a number of ‘or’ conditions, and HA throws up errors that I can’t use conditions in scripts (the documentation here disagrees, but at any rate… I get the errors)

My next thought was to use IFTTT to trigger an event (as described in this post) but that’s where I’m stuck. Can I make custom “events” and trigger them using IFTTT?

First, you absolutely CAN use a condition in a script. They are used somewhat differently than they are in automations however.
If you wanted to create a custom event, you would have to go to the developer tools, but I think that’s barking up the wrong tree.
IF you absolutely need the conditions that are supported in a trigger, you could write a trigger that initiates the script if all the conditions are met. Then, you could trigger that automation using IFTTT with an API call to /api/services/automation/trigger

Hm, perhaps you can help me understand better, then? If conditions in scripts are just as possible as in automations, I’d prefer to do things that way because that’s what I’m most familiar with.

I have a physical toggle switch (no on/off, just toggle) that controls a few lights in a room via ifttt - but the problem is if one light is on and the other is off, “toggle” will just turn one off and the other on. I want the script to instruct home assistant to turn the lights off if one or more are already on, and on if they are all off.

Friendly folks in gitter suggested I use two automations that both use the same trigger to get this “if/then, else” logic. Since I am using IFTTT to toggle scripts instead of automations, I set it up like this.

  toggle_living_room_lights_0:
    sequence:
      - service: script.toggle 
        entity_id: script.toggle_living_room_lights_1, script.toggle_living_room_lights_2
  
  toggle_living_room_lights_1:
    sequence:      
      - condition:
          condition: or
          conditions:
          - condition: state
            entity_id: light.living_room
            state: 'on'
          - condition: state
            entity_id: switch.overhead_lights
            state: 'on'
      - service: homeassistant.turn_off
        entity_id: group.living_room_lights

  toggle_living_room_lights_2:
    sequence:
      - condition: state
        entity_id: group.living_room_lights
        state: 'off'
      - service: homeassistant.turn_on
        entity_id: group.living_room_lights

The idea is that IFTTT will trigger toggle_living_room_lights_0, which will in turn toggle the other two scripts. Depending on the state of the lights in the room, one of the two scripts will fail to meet the conditions and stop. The other will carry out the desired action.

But this setup gives me errors in the log saying scripts can’t use conditions.

Have i done something wrong?

This should answer your direct question.

toggle_living_room_lights_1:
  sequence:      
    - condition: template
      value_template: '{{states.light.living_room.state=="on" or states.switch.overhead_lights.state=="on"}}'
    - service: homeassistant.turn_off
      entity_id: group.living_room_lights

However, I would do this all with one script instead.
You can use a service_template to determine which service to call. There’s a good example of it here:

1 Like

Wow, the example for service_template is exactly what I needed. I’m still learning the proper syntax for templating (not a computer languages person - is it a specific language? Have no idea what function the specific ==s and {} have) but am anxious to teach myself more slowly - examples like this for learning practical usage are invaluable!

Thanks very much for your help!

The template language is called jinja2. If you want to really learn it, go to the template tool in developer tools, and play with it there.

1 Like