Notify which windows are left open when you leave the house

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

        tts_text: >
          {{ states | selectattr('entity_id','in', state_attr('group.windows','entity_id')) | selectattr('state','eq','on') | map(attribute='name') | join(', ') }}

Works perfectly. Thank you!!

Hi,

Can anyone help me finish this off. So far Alexa announces the specific windows that are open (great!), but I would like her to finish the very end of the sentence with “is open”. At the moment she says, Dining Room Window, Living Room Window etc (depending which window is open). But it would be great if I could get her to say “[entity name] is open”

service: notify.alexa_media_living_room_echo
metadata: {}
data:
  data:
    type: tts
  message: >-
    {{ states | selectattr('entity_id','in',
    state_attr('binary_sensor.window_sensors','entity_id')) | selectattr('state','eq','on') |
    map(attribute='name') | join(', ') }}

You only have to add „is open“ after the brackets }} and put the complete message in ‘