Add sensor value to notification message

I’ve got this example event set up, to show an notification on my tablet frontend (using tileboard):

Look like that on my tablet:

warning

As you can see, the message does not appear as intended. What I’d like to see is the content of this variable (yellow marker in screenshot):

states

How can I add an attribute of the sensor state to the JSON message?

Thanks for any help!

You probably need to use {{states.sensor.**sensor-name**.state}} to get the desired value parsed into a template.

There is a very usefull feature in HA to test these things out. It’s called Template editor and its found in the side bar under Developer tools.
image
Go there, delete the existing code and paste yours, see what the result is, and go from there.

Edit: the template editor has been now moved to Developer Tools - > Template editor tab

3 Likes

It works in the template editor:

But unfortunately, if I use the same message format in the event:

{
   "command": "notify",
   "message": "{{states.sensor.dwd_weather_warnings_current_warning_level.attributes.warning_1_headline }}"
}

it still looks like that :frowning:

3

Try removing the quotes?

{
   "command": "notify",
   "message": {{states.sensor.dwd_weather_warnings_current_warning_level.attributes.warning_1_headline }}
}

Without quotes: Error parsing JSON: SyntaxError: JSON.parse: expected property name or '}' at line 3 column 17 of the JSON data

I dont know how tileboard notifications work,
Maybe you need to open another bracket in the json and give it some data_template in order to parse the template data?

Here’s how my automations work with html5 notifications, maybe it’ll help you out:

...
  action:
  - data_template:
      message: Stella has left the house {{ now().strftime('%H:%M')}}
      title: Presence
    service: notify.notify_html5

I don’t think the automation editor supports templates. You will have to write it in yaml.

it supports templates

1 Like

As far as I can see, I’m using the same syntax:

{
   "command": "notify",
   "message":  "{{ states.sensor.dwd_weather_warnings_current_warning_level.state }}"
}

So it probably is a problem with the TileBoard component?

I tried many different variations in YAML, without success.

e.g.:

  action:
  - event: tileboard
    event_data:
      command: notify
      icon: mdi-weather-lightning-rainy
      id: dwd_warnung
      lifetime: '28800'
      data_template:
        message: 'State {{ states.sensor.dwd_weather_warnings_current_warning_level.state }}'
      title: Wetterwarnung
      type: warning

For what it’s worth, here’s my (working) automation rule:

  action:
    - service: switch.turn_off
      entity_id: switch.sonoff_*hidden*
    - service: notify.debug
      data:
        message: "DEBUG ToTheLiving - Timer finished, lamp is going out again. Bowlamp: {{ states.switch.sonoff_*hidden*.state }}"

I’m using simplepush as a notification platform.

1 Like

your var need to be like this!
{{ states(‘PUT_YOUR_ENTITY_HERE’) }}

:slight_smile:
Hope that you still looking for an anwser :wink:

5 Likes

Hello,
I’m new to this board and quite new to HASS, but I’m loving this system and loving this community, especially this thread, because now I found a way to monitor the tracker of my dog.

Have a nice day.

1 Like

Welcome, enjoy :smiley:

As the answer is not here and I have been searching for it for hours you need to use this:
state_attr(‘sensor.livingroom’, ‘Battery numeric’)

So do not use states.

This worked for me! Thanks :slight_smile:

I know your Question is Years old but hopefully this can help others too.

First you should check the Sensor in Developer Tools → States.

Then head over to Developer Tools → Template to play around.

Shows the State
{{ states.sensor.wetter_warnungen_kreis_garmisch_partenkirchen_current_warning_level.state }}

Shows all Attributes
{{ states.sensor.wetter_warnungen_kreis_garmisch_partenkirchen_current_warning_level.attributes }}

Shows only one Attributes
{{ states.sensor.wetter_warnungen_kreis_garmisch_partenkirchen_current_warning_level.attributes.warning_1_headline }}

Now you can do something like this, it may look complicated but try to one value at the time

{{ states.sensor.wetter_warnungen_kreis_garmisch_partenkirchen_current_warning_level.attributes.warning_1_headline }} in {{ states.sensor.wetter_warnungen_kreis_garmisch_partenkirchen_current_warning_level.attributes.region_name }}. {{ states.sensor.wetter_warnungen_kreis_garmisch_partenkirchen_current_warning_level.attributes.warning_1_description }}
{% if as_timestamp(states.sensor.wetter_warnungen_kreis_garmisch_partenkirchen_current_warning_level.attributes.warning_1_start) | timestamp_custom('%Y-%m-%d') == now().strftime('%Y-%m-%d') %}
Von heute {{ as_timestamp(states.sensor.wetter_warnungen_kreis_garmisch_partenkirchen_current_warning_level.attributes.warning_1_start) | timestamp_custom('%I:%M') }} bis {{ as_timestamp(states.sensor.wetter_warnungen_kreis_garmisch_partenkirchen_current_warning_level.attributes.warning_1_end) | timestamp_custom('%I:%M') }}
{% else  %}
Vom {{ as_timestamp(states.sensor.wetter_warnungen_kreis_garmisch_partenkirchen_current_warning_level.attributes.warning_1_start) | timestamp_custom('%I:%M (%d.%m)') }} bis {{ as_timestamp(states.sensor.wetter_warnungen_kreis_garmisch_partenkirchen_current_warning_level.attributes.warning_1_end) | timestamp_custom('%I:%M (%d.%m)') }}
{% endif %}

{% if states.sensor.wetter_warnungen_kreis_garmisch_partenkirchen_current_warning_level.attributes.warning_1_instruction != none %}
  Es gibt folgende Hinweiße: {{ states.sensor.wetter_warnungen_kreis_garmisch_partenkirchen_current_warning_level.attributes.warning_1_instruction }}
{% endif %}
13 Likes

This is perfect. Thanks. Just what I needed to learn.

1 Like

Hi, that worked for my sensor too. But how do I learn this. Is there a guide for that? I’m other programming software you get suggestions when you type the dot. Will this come to HA as well?

I have been trying to do this for alexa notification where the text for the notification message comes from the value of a template sensor. After several attemps, finally the below worked for me. Hopefully can be of help to someone else trying to do the same:

service: notify.alexa_media_kannan_s_2nd_echo_dot_2
data:
  data:
    type: tts
  message: "{{states(\"sensor.front_door_people_announce\")}}"

So you have to put the backslah before the quotes.