Notify which windows are left open when you leave the house

There’s lots of documentation. I suggest you review this section: Templating

This template has many errors:

message: "{{ name("entity") }} blank has been {{ states("entity") }}"
  • If you use double-quotes outside of the template, you must use single-quotes inside the template (or vice-versa). You can’t use the same quotes inside and outside of the template.
  • There is no name() function so I don’t know where you got that from.
  • This part "entity" means it is simply the word entity and nothing else. Passing the word "entity" to the states() function is invalid and accomplishes nothing.

Based on just the example you posted, I don’t know where it is used (automation or a script?) or which entities it is attempting to use. Please explain how you intend to use it.

Its in a automation that my doors and windows changes state are the triggers and the notification is the action

Post it and please format it correctly. The previous example you posted was unformatted and makes it difficult to determine if it has syntax errors (like incorrect indentation).

alias: notification windows and dorr
description: ‘’
trigger:

  • platform: state
    entity_id: switch.mqtt_front_dorr
  • platform: state
    entity_id: switch.garage_door
  • platform: state
    entity_id: binary_sensor.tv_dorr_temp
  • platform: state
    entity_id: switch.cinema_left_dorr
  • platform: state
    entity_id: binary_sensor.living_right_door
  • platform: state
    entity_id: binary_sensor.livingroom_left_dorr
  • platform: state
    entity_id: binary_sensor.kitchen_right_window
    condition: []
    action:
  • service: notify.mobile_app_skynet_mobile
    data:
    title: dorr window
    message: ‘{{ name(“entity”) }}blank has been {{ states(“entity”) }}’
    mode: single

That’s not a properly formatted automation but at least I can now see how you intend to use the notification.

    message: "{{ trigger.to_state.name }} is now {{ 'open' if trigger.to_state.state == 'on' else 'closed' }}"
1 Like

Thanks for the help worked perfectly

I’ve been trying to add some more sensors to your code today, but if I try adding it to the automation, HA gives me an

"Message malformed: invalid template (TemplateSyntaxError: Encountered unknown tag ‘elif’.) for dictionary value @ data[‘action’][0][‘data’]"

error.

I’d be very greatful for anyone to help me.

Here is my code:

service: tts.google_translate_say
data:
  entity_id: media_player.kinderzimmer_display
  message: >
   {% set e_schlafzimmer = is_state('binary_sensor.fensterkontakt_e_schlafzimmer', 'on') %}
   {% set kinderz = is_state('binary_sensor.fensterkontakt_kinderz', 'on') %}
   {% set kuche = is_state('binary_sensor.fensterkontakt_kuche', 'on') %}
   {% set wc = is_state('binary_sensor.fensterkontakt_wc', 'on') %}
   {% set wozimmer_links = is_state('binary_sensor.fensterkontakt_wozimmer_links', 'on') %}
   {% set wozimmer_rechts = is_state('binary_sensor.fensterkontakt_wozimmer_rechts', 'on') %}
   {% elif e_schlafzimmer %} Please close the schlafzimmer window.
   {% elif kinderz %} Please close the kinderzimmer window.
   {% elif kuche %} Please close the kueche window.
   {% elif wc %} Please close the wc window.
   {% elif wozimmer_links %} Please close the wohnzimmer links window.
   {% elif wozimmer_rechts %} Please close the wohnzimmer rechts window.
   {% else %} Everything is closed.
   {% endif %}

You are using a chain of if-elif-elif-elif-else statements but you forgot to make the first one if.

Change this:

   {% elif e_schlafzimmer %} Please close the schlafzimmer window.

to this:

   {% if e_schlafzimmer %} Please close the schlafzimmer window.

EDIT

If you wish, you can have all the open windows reported in a single sentence.

service: tts.google_translate_say
data:
  entity_id: media_player.kinderzimmer_display
  message: >
   {% set fenster = [
     states.binary_sensor.fensterkontakt_e_schlafzimmer,
     states.binary_sensor.fensterkontakt_kinderz,
     states.binary_sensor.fensterkontakt_kuche,
     states.binary_sensor.fensterkontakt_wc,
     states.binary_sensor.fensterkontakt_wozimmer_links,
     states.binary_sensor.fensterkontakt_wozimmer_rechts ]
     | selectattr('state', 'eq', 'on')
     | map(attribute='name') | list %}
   {% set qty = fenster | count %}
   {% if qty == 0 %}
     Everything is closed.
   {% else %}
     Please close the following window{{ 's' if qty > 1 else ''}}: {{ fenster | join(', ') }}
   {% endif %}

EDIT

Correction. Added missing comma.

Thank you so much! It workes flawlessly now!

hi taras
I am trying to adapt the following code to my automation.
I have 2 problems.
if i simply paste your code to the developers tools - template
I got the following

TemplateRuntimeError: No test named 'eqon'.

I am not sure that this is a problem.
but when I try to change the bianary sensors with mine
like this

 message: >
   {% set fenster = [
     states.binary_sensor.8_erker_tzami,
     states.binary_10_ypnodomatia_tzami,
     states.binary_5_trapezaria_tzami,
     states.binary_sensor.13_mpanio_wc ]
     | selectattr('state', 'eq' 'on')
     | map(attribute='name') | list %}
   {% set qty = fenster | count %}
   {% if qty == 0 %}
     Everything is closed.
   {% else %}
     Please close the following window{{ 's' if qty > 1 else ''}}: {{ fenster | join(', ') }}
   {% endif %}

I get the following

TemplateSyntaxError: expected token ',', got '_erker_tzami'

I am not sure what is wrong. Have you got any ideas?

Thank you for bringing that to my attention.

The example I posted is missing a comma between 'eq' and 'on' here:

| selectattr('state', 'eq' 'on')
                          ^
                          |
                  Add a comma here

I have corrected the example.

Yes, now the first problem is eliminated. If I paste your code i get “Everything is closed.”
but
in the same code if I try to replace the first sensor with a mine one
like this

message: >
   {% set fenster = [
     states.binary_sensor.10_ypnodomatia_tzami,

I get the following error

`TemplateSyntaxError: expected token ',', got '_ypnodomatia_tzami'`

the binary sensor name is 10_ypnodomatia_tzami
is there any chance that HA template can’t work with this kind of naming in the binary sensors (if they have a number for example?)

my 4 sensors names are
binary_sensor.10_ypnodomatia_tzami,
binary_sensor.5_trapezaria_tzami,
binary_sensor.13_mpanio_wc
binary_sensor.8_erker_tzami

There is a problem when the entity’s object_id begins with a number and it’s mentioned in the documentation here.

You need to reference it using bracket notation:

   {% set fenster = [
     states.binary_sensor["10_ypnodomatia_tzami"],

As an alternative, you can change the template’s design and use the expand function.

   {% set fenster = expand('binary_sensor.10_ypnodomatia_tzami', 'binary_sensor.5_trapezaria_tzami',
       'binary_sensor.13_mpanio_wc', 'binary_sensor.8_erker_tzami')
     | selectattr('state', 'eq', 'on')
     | map(attribute='name') | list %}
1 Like

that’s it. now it works.
thank you!

Missing the comma again.:slight_smile: Just noting, because I know you wouldn’t want to leave it wrong. :slight_smile:

1 Like

Where is it missing? :thinking:

Duh, found where it’s missing! The evils of blindly copy-pasting …

Fixed. Thanks for spotting it!

1 Like

Hi,

I’m struggling with the templating here; I put all the window sensors into a group, but I can’t get the automation to parse. I get the error: Error rendering data template: TypeError: argument of type ‘NoneType’ is not iterable

Can anyone suggest what is wrong and how to fix it?


alias: Status Automation
description: ""
trigger:
  - platform: state
    entity_id:
      - input_boolean.status
    from: "off"
    to: "on"
condition: []
action:
  - service: notify.alexa_media_bedroom
    data:
      message: >-
        {% set open_windows = states | selectattr('entity_id','in', state_attr('group.windows','entity_id')) | selectattr('state','eq','on') | map(attribute='name') | list %}
        {% if open_windows | length == 0 %}
          All windows are closed.
        {% elif open_windows | length == 1 %}
          There is a window open in the {{ open_windows | join('') }}.
        {% else %}
          There are windows open in the {{ open_windows[:-1] | join(', ') }}{{',' if open_windows | length > 2 else ''}} and {{ open_windows[-1]}}.
        {% endif %}
mode: single

You most likely don’t have the group.windows entity in your system. That’s a prerequisite and you have to manually create it.

You are right! I put the window sensors in a group, but the group is showing as binary_sensor.windows. I will just change the name to binary_sensor.windows and see if that helps.

I have the following script working as a notification

alias: "Windows open notification 2 "
sequence:
  - service: notify.mobile_app_imrans_pixel
    data_template:
    message: >
     {{ states | selectattr('entity_id','in', state_attr('group.windows','entity_id')) | selectattr('state','eq','on') | map(attribute='name') | join(', ') }}
    title: "Window still open: "
    data:
      priority: 1
mode: single
icon: mdi:window-open-variant

Can anyone help to get this working as tts. I have tried the following but I just get a voice saying null. If I change the string to simple text then it works.

alias: New Script
sequence:
  - service: notify.mobile_app_imrans_pixel
    data:
      message: TTS
      data:
        tts_text: {{ states | selectattr('entity_id','in', state_attr('group.windows','entity_id')) | selectattr('state','eq','on') | map(attribute='name') | join(', ') }}
        channel: alarm_stream_max

Do I need to add something to get the string recognised for tts.
Thanks