Question re: getting Sensor display status in email automation

I have this automation:

alias: test ping
description: test ping
trigger:
  - platform: state
    entity_id:
      - binary_sensor.192_168_1_4
      - binary_sensor.192_168_1_5
    from: "on"
    to: "off"
  - platform: state
    entity_id:
      - binary_sensor.192_168_1_4
      - binary_sensor.192_168_1_5
    from: "off"
    to: "on"
condition: []
action:
  - action: notify.me_gmail_com
    metadata: {}
    data:
      message: >-
        Connection to {{ trigger.to_state.attributes.friendly_name }} is now {{
        trigger.to_state.state }} at {{now().strftime("%Y-%m-%d %H:%M:%S")}}
      title: >-
        Connection to {{ trigger.to_state.attributes.friendly_name }} is now {{
        trigger.to_state.state }} at {{now().strftime("%Y-%m-%d %H:%M:%S")}}
      target: [email protected]
mode: single

and it works great but!!.. this is what it creates:

Connection to 192.168.1.4 Access point in Shed is now off at 2024-09-27 19:04:20

What i would like is to ensure that instead of “off” and “on” status I get a “Connected” or “Disconnected” text in the email?

So basically what the GUI shows:

image

Not need on if/then statements in the automation as I am sure this is just a matter of getting this syntax correct ?..

{{ trigger.to_state.state }}

Any ideas? Thanks in advance! :slight_smile:

I do not think you can have access to translations here because state_translated does not work on a state from the trigger variable, so an inline if or iif is probably easiest. Still if, but at least you do not need multiple lines:

{{ 'connected' if trigger.to_state.state == 'on' else 'disconnected' }}

Edit : come to think of it (haven’t tried) for the new state you can probably also use something like:

{{ state_translated(trigger.entity_id) }}
2 Likes

@Edwin_D awesome! :slight_smile: I tried your 2nd suggestion plus a bit of a reworded message and this works great:

alias: test ping
description: test ping
trigger:
  - platform: state
    entity_id:
      - binary_sensor.192_168_1_4
      - binary_sensor.192_168_1_5
    from: "on"
    to: "off"
  - platform: state
    entity_id:
      - binary_sensor.192_168_1_4
      - binary_sensor.192_168_1_5
    from: "off"
    to: "on"
condition: []
action:
  - action: notify.me_gmail_com
    metadata: {}
    data:
      message: >-
        {{ trigger.to_state.attributes.friendly_name }} has {{
        state_translated(trigger.entity_id) }} to network at
        {{now().strftime("%Y-%m-%d %H:%M:%S")}}
      title: >-
        {{ trigger.to_state.attributes.friendly_name }} has {{
        state_translated(trigger.entity_id) }} to network at
        {{now().strftime("%Y-%m-%d %H:%M:%S")}}
      target: [email protected]
mode: single

I now get this when IP is down:

192.168.1.4 Access point in Shed has Disconnected to network at 2024-09-27 20:12:01

and IP is back up::

192.168.1.4 Access point in Shed has Connected to network at 2024-09-27 20:09:06

Thanks!

1 Like

I wonder… rather than me have a list of entity_id in the automation, I wonder if it’s possible to have this list based on the contents of the area:

image

Any ideas?