Wildcard for sensor in automation

Hello,

I did a automatoin which notifies me when a water sensor alerts.
I have various sensors in this automation. The message to my handy app is: message: Water sensor has triggert.
Now I want to know which sensor has triggert. Is it possible to put in a wildcard for the sensor in the message? For example: Water Sensor Office has triggert.

Yes, with templating you can do that:

action:
- service: notify.notify
  data:
    message: "The {{ trigger.to_state.name }} has detected water"
1 Like

Thank you for your reply.
Sorry, I´m new in Home Assistant.
I put this code to the configuration.yaml and what do I put in the "message: "?

It would help if you posted the automation that you have now.

Here you are

- id: '111'
  alias: Wassersensoren ausgelöst - notify
  description: ''
  trigger:
  - type: moist
    platform: device
    device_id: 
    entity_id: binary_sensor.water_3
    domain: binary_sensor
  - type: moist
    platform: device
    device_id: 
    entity_id: binary_sensor.wassersensor_wohnzimmer
    domain: binary_sensor
  - type: moist
    platform: device
    device_id: 
    entity_id: binary_sensor.wassersensor_kuche
    domain: binary_sensor
  condition: []
  action:
  - service: notify.mobile_app
    data:
      message: "Wassersensor {{ trigger.to_state.name }} ausgelöst"
      title: Wassersensor
  mode: single

When the sensor has, I get the message without the sensor name (only: Wassersensor ausgelöst).

I suspect the problem is that you’re using device triggers, and the template values for triggers aren’t documented.

You could try moving to state triggers:

- id: '111'
  alias: Wassersensoren ausgelöst - notify
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.water_3
    to: 'on'
  - platform: state
    entity_id: binary_sensor.wassersensor_wohnzimmer
    to: 'on'
  - platform: state
    entity_id: binary_sensor.wassersensor_kuche
    to: 'on'
  condition: []
  action:
  - service: notify.mobile_app
    data:
      message: "Wassersensor {{ trigger.to_state.name }} ausgelöst"
      title: Wassersensor
  mode: single

I changed it, but the sensor name still doesn’t appear…

Check your log file for errors, warnings, and other useful information.

It works now. I didn´t change anything. Thank you very much :+1:
I use it in diffrent automations. It works everywhere.
Only in one automation (Battery check) I get a error in the log: • Batterie check: Error executing script. Unexpected error for call_service at pos 1: Error rendering data template: UndefinedError: ‘trigger’ is undefined
Here the automation:

- id: 'xxx'
  alias: Batterie check
  description: ''
  trigger:
  - platform: state
    entity_id: sensor.fenster_xxx_battery_level
    to: '100'
  condition: []
  action:
  - service: notify.mobile_app
    data:
      message: '{{ trigger.to_state.name }} Batterie fast leer'
  mode: single

The sensor “sensor.fenster_xxx_battery_level” has 100% battery. I use the “to: 100” trigger to receive a push message immediately when I start the automation.
What can be the problem?

The {{ trigger.to_state.name }} part only works when the automation is triggered by the trigger itself.
You cannot manually execute it, since doing so results in an undefined trigger.

2 Likes

Ah OK. Thank you for your post.