List the states of all entities in a group in html table format - handy for email

I have HASS send me an email (via SMTP notify) with a photo from a camera when there is movement at home, but no-one from the family (a group of devices) is detected as home.

The cool thing (IMHO) is that the automation builds a list of states of devices (from a group called occupancy) and builds the list in html table format - which makes it present very nicely in the email when I get it.

code below:

  1. bar_snapshot shell command grabs snapshot from my generic IP camera and “converts” it to be valid for use in an email
  2. group occupancy (cut down for publication) is the entities I am interested in for testing occupancy
  3. the automation runs when no-one is home (per the family device-tracker group) and sends the status and last changed timestamps for each entity in the group in a nice html table format.

Only issue is I cant get the last_changed attribute converted to local time - it is always UTC!

Hope someone can make use of it!

If you want to play along at home, grab the loop from the automation and enter it in the templates developer section changing the group occupancy to one that exists in your HASS instance:
image

      {%- for entity_id in states.group.occupancy.attributes.entity_id -%}
      {% set parts = entity_id.split('.') -%}
      <tr><td>{{ states[parts[0]][parts[1]].attributes.device_class }} </td><td>{{ states[parts[0]][parts[1]].name }} </td><td> {{states(entity_id)}} </td><td> {{(states[parts[0]][parts[1]].last_changed | timestamp_local ).strftime("%a, %d %b %Y %H:%M:%S") }}</td></tr>
      {%- endfor %}

All my relevant config:

shell_command:
  bar_snapshot: "wget -q -O /home/pi/.homeassistant/snapshots/bar.jpg http://10.1.1.xxx:8123/api/camera_proxy/camera.barcam?api_password=mypwd && convert /home/pi/.homeassistant/snapshots/bar.jpg /home/pi/.homeassistant/snapshots/bar.jpg"
 

group:
  occupancy:
    view: no
    entities:
      - binary_sensor.door_window_sensor_158d000149a219
      - binary_sensor.door_window_sensor_158d00016c80f8

automation:
  - alias: 'Movment at home'
    trigger:
      - platform: state
        entity_id: binary_sensor.motion_sensor_158d0001231a61, binary_sensor.door_window_sensor_158d0001a67cef, binary_sensor.motion_sensor_158d00012319de
        to: 'on'
    condition:
      - condition: state
        entity_id: group.family
        state: not_home     
    action:
      - service: shell_command.bar_snapshot
      - delay: 
          seconds: 2
      - service: notify.phil_work
        data:
          title: "Movement at home" 
          message: >
            Movement no family {{now().strftime("%a, %d %b %Y %H:%M:%S")}} <br />
            <table><th> sensor status: </th> 
              {%- for entity_id in states.group.occupancy.attributes.entity_id -%}
              {% set parts = entity_id.split('.') -%}
              <tr><td>{{ states[parts[0]][parts[1]].attributes.device_class }} </td><td>{{ states[parts[0]][parts[1]].name }} </td><td> {{states(entity_id)}} </td><td> {{(states[parts[0]][parts[1]].last_changed | timestamp_local ).strftime("%a, %d %b %Y %H:%M:%S") }}</td></tr>
              {%- endfor %}          
            </table>            
          data:
            images: 
              - /home/pi/.homeassistant/snapshots/bar.jpg
1 Like

Saved my life mate. have been trying to work out this Group Loop for quite a while.
Thanks a lot

Can you share your config, I’m wondering how you have integrated it in your setup? Thnx

Just used this part

{%- for entity_id in states.group.GROUPNAME.attributes.entity_id -%}
{% set parts = entity_id.split(‘.’) -%}
states[parts[0]][parts[1]].name
{%- endfor %}

Managed to get this working with the data I need but Im not getting a table view in the email, its not formatting properly

This is the contents off the email,

Lounge temp min and max Sun, 01 Sep 2019 21:19:36 <br /> <table><th> sensor status: </th>

   <tr><td>Min temp</td><td>22.5</td></tr>

   <tr><td>Max temp</td><td>23.0</td></tr>
</table>

the code is:

action:
  - service: notify.e_mail_temperature_states
    data:
      message: >
        Lounge temp min and max {{now().strftime("%a, %d %b %Y %H:%M:%S")}} <br />
        <table><th> sensor status: </th>
          {%- for entity_id in states.group.email_stats.attributes.entity_id %}
          {% set parts = entity_id.split(".") %}
            <tr><td>{{ states[parts[0]][parts[1]].name }}</td><td>{{states(entity_id)}}</td></tr>
          {%- endfor %}
        </table> 

Did you ever get the table to display?

1 Like