Confused with templates

Finally I was able to connect my self buildt door Computer (hosting a nfc/pin Lock and several detectors and bell knobs) to HA. I send bell notifications through rest to A and HA requests a picture of the Pi Cam like this:

- action:
  - data_template:
      message: '{{trigger.event.data.sender}}'
      data:
        image: http://192.168.X.Y:5000/door.jpg?raw=true
    service: notify.alle
  alias: PiDoorEvent
  condition: []
  id: '9843753984786'
  trigger:
  - event_data: {}
    event_type: pidoor
    platform: event

What I now would to add is that notifications only arrive, if we are in the home zone. Unfortunately I am confused how o add an other template to use code like this:

 {% if is_state("device_tracker.vog_l29", "home") and
       is_state("device_tracker.eml_l29", "home") %}
    notify.alle
  {% elif is_state("device_tracker.eml_l29", "home") %}
    notify.nicole
  {% else %}
    notify.michi
  {% endif %}

I tried several ways to no avail. Any help is very appreciated.

Use the choose action.
There you set up two choose actions.
One if both is home, and the action to both.
And a second if Nicole is home and that action, and in the default action you set notify Michi.
I’m not sure you can Jinja the service like that.

action:
  - service: >
      {% if is_state("device_tracker.vog_l29", "home") and is_state("device_tracker.eml_l29", "home") %}
        notify.alle
      {% elif is_state("device_tracker.eml_l29", "home") %}
        notify.nicole
      {% else %}
        notify.michi
      {% endif %}
    message: your message here...

Thanks. I combined both templates this way and it seems to work:

- action:
    - service: >
        {% if is_state("device_tracker.vog_l29", "home") and is_state("device_tracker.eml_l29", "home") %}
        notify.alle
        {% elif is_state("device_tracker.eml_l29", "home") %}
        notify.nicole
        {% elif is_state("device_tracker.vog_l29", "home") %}
        notify.michi
        {% endif %}
      data_template:
          message: '{{trigger.event.data.sender}}'
          data:
            image: http://192.168.X.Y:5000/door.jpg?raw=true
  alias: PiDoorEvent
  condition: []
  id: '9843753984786'
  trigger:
  - event_data: {}
    event_type: pidoor
    platform: event

The template you have created for service has a flaw.

What does it produce if neither vog_129 or eml_129 is home? The answer is it produces nothing which is an empty service call and will cause an error.

That’s why it was recommended to use choose or to terminate the if-elif chain with an else like in tom_l’s example.

Its intended to produce nothing in that case as we both do not want to receive the bell notification if we are not at home. Should I create a kind of dummy service for that case? If the error does not flood log files I can live with it as long the rest works.

Why create hacks, or live with errors, when you can simply use choose?

1 Like

You are absolutely right! I will try solving it using choose. As I unfortunately have very little spare time to deal with the HA project I was happy to get it up and running in the first place. But I absolutely share the philosophy to do things right instead of doing it the hacky way!

You can also do it like this:

- id: '9843753984786'
  alias: PiDoorEvent
  trigger:
  - platform: event
    event_type: pidoor
    event_data: {}
  condition: []
  action:
    - variables:
        vog: '{{ is_state("device_tracker.vog_l29", "home") }}'
        eml: '{{ is_state("device_tracker.eml_l29", "home") }}'
    - condition: template
      value_template: '{{ vog or eml }}'
    - service: 'notify.{{ "alle" if (vog and eml) else "nicole" if eml else "michi" }}'
      data:
        message: '{{trigger.event.data.sender}}'
        data:
          image: http://192.168.X.Y:5000/door.jpg?raw=true

There’s a Template Condition within the action that permits execution of the service call only if at least one device_tracker indicates home. If no one is home, the service call is not executed and the automation ends.

1 Like