Sending list of all devices in a notification message

Hi,
I am quite new to home assistant and already got quite a lot working. I am now trying to list all my devices every 5 minutes towards Telegram. The telegram bot works fine and also the devices are scanned frequently, but what I can’t figure out is how I would go through all my devices and send them in the message. My Automation attempt is below, but I have no idea how the create the message (thats why there are now just question marks).

Any advice would be appreciated.

- id: '1534004396541'
  alias: Notify all devices to Telegram every 5 minutes 
trigger:
  - platform: time
    minutes: /5
  condition: []
  action:
  - data:
      message: ?? List all device in group.all_devices with indication away or home ?? 
    service: telegram_bot.send_message

Hi @john2014, you can play with it in Developer Tools / Templates.

{% for device in states.device_tracker %}
{{ device.name }} is {{ device.state }}  
{%- endfor %}

@VDRainer ty so much! You got me in the right direction

I made this now, works like a charm. Shows both the online and offline devices.

  action:
  - data_template:
      message: "Home: {% for state in states.device_tracker -%}
{% if state.state_with_unit == 'home' %}{{ state.name | lower }}, {% endif %}{% endfor %}


Away: {% for state in states.device_tracker -%}
{% if state.state_with_unit == 'not_home' %}{{ state.name | lower }}, {% endif %}{% endfor %}"
    service: telegram_bot.send_message
1 Like

FYI, here is another way to achieve the same thing:

  action:
  - data_template:
      message: |-
        Home: {{ states.device_tracker|selectattr('state','eq','home')|map(attribute='name')|join(', ') }}
        Away: {{ states.device_tracker|selectattr('state','eq','not_home')|map(attribute='name')|join(', ') }}
    service: telegram_bot.send_message

In your solution I’d suggest changing:

{% if state.state_with_unit == 'home' %}

to:

{% if state.state == 'home' %}

Same thing for the ‘not_home’ case.

2 Likes

@pnbruckner thanks for the improvement!

Just wondering what is that difference between .state and .state_with_unit?

and what does “|-” do?

See:

Basically .state is just the state (e.g., ‘32’), whereas .state_with_unit includes the unit_of_measurement (e.g., ‘32 °F’.)

You’ve probably seen:

xxx: >

The > means do not keep new lines, whereas | means keep new lines. The - after them means do not keep the new line at the end.

See:

1 Like

@pnbruckner thanks again. But I cant seem to get the automation that you posted to work.

If i copy paste the code below, it wont work. Also the whole message turns green in the configurator. Not sure what im doing wrong

action:
  - data_template:
      message: |-
        Home: {{ states.device_tracker|selectattr('state','eq','home')|map(attribute='name')|join(', ') }}
        Away: {{ states.device_tracker|selectattr('state','eq','not_home')|map(attribute='name')|join(', ') }}
    service: telegram_bot.send_message

Well, try changing |- to >-. Maybe HA’s YAML parser doesn’t support |. I guess I never tried it.

Also, for Away, you might want to consider changing:

|selectattr('state','eq','not_home')

to:

|selectattr('state','ne','home')

The former will only list the entities whose state is exactly ‘not_home’, whereas the latter will list all entities whose state is not exactly ‘home’ (e.g., it will also list entities that are in a zone other than the home zone.)

1 Like

hmm no >- also doesnt work. The error I get on config validation is:

Invalid config for [automation]: expected a dictionary for dictionary value @ data['action'][0]['data_template']. Got None
extra keys not allowed @ data['action'][0]['message']. Got None. (See /config/configuration.yaml, line 61). Please check the docs at https://home-assistant.io/components/automation/

But ill play around with it a bit more, must be some kind of simple formatting or something

Please post the entire automation.

weird, i just wanted to copy and paste it, but then tried it again and it seems to be working!

I still see a message that something is wrong with my config, but when I press validate config it says success.

But this part seems to be working, ty very much.!