Using multiple entity attributes in a notify message

Hi. I’d like to get a message with the reason why my sunscreen closed (can be multiple reasons, sunset, rain, wind speed, etc.).

How can one use multiple entity attributes in a notify message please?

Expecting:
Sunscreen closed. OpenWeatherMap Wind speed is now 33 km/h.

Getting:
Sunscreen closed. OpenWeatherMap Wind speed is now 33 33

Message

service: notify.my_phone
data:
  message: >-
    Sunscreen closed. {{ state_attr(trigger.entity_id, 'friendly_name') }}
    is now {{ states(trigger.entity_id) }} {{ states(trigger.entity_id,
    'unit_of_measurement') }}

Entity + attributes: sensor.openweathermap_wind_speed
state_class: measurement
unit_of_measurement: km/h
attribution: Data provided by OpenWeatherMap
device_class: wind_speed
friendly_name: OpenWeatherMap Wind speed

Any pointers are welcome!

PS Found this, but it I could not fix the problem with it: Working with the trigger object - #4 by Didgeridrew

It is usually unnecessary to use the states() or state_attr() function on the trigger variable.

Assuming you are using a state trigger

service: notify.my_phone
data:
  message: >-
    Sunscreen closed. {{ trigger.to_state.attributes.friendly_name }}
    is now {{ trigger.to_state.state }} {{ trigger.to_state.attributes.unit_of_measurement }}
1 Like

Works like a charm! Thank you, you thought me a new concept today.

Newbie here!
I hope adding to this topic is appropriate.

I want to be able to use notify with multiple recursive attributes for an entity. In my case below, Sea Tide times.

The contents of the attribute look like this…

Rising
[[β€˜2024-10-09 01:32:33’, 6.9], [β€˜2024-10-09 09:00:16’, 2.2], [β€˜2024-10-09 13:53:15’, 6.7], [β€˜2024-10-09 21:20:41’, 2.4], [β€˜2024-10-10 02:16:55’, 6.5], [β€˜2024-10-10 09:42:05’, 2.6], [β€˜2024-10-10 14:43:33’, 6.2], [β€˜2024-10-10 22:08:20’, 2.8], [β€˜2024-10-11 03:19:03’, 5.9], [β€˜2024-10-11 10:39:26’, 3.0], [β€˜2024-10-11 16:58:39’, 5.7], [β€˜2024-10-11 23:17:21’, 3.1], [β€˜2024-10-12 05:48:02’, 5.7], [β€˜2024-10-12 12:21:31’, 3.1], [β€˜2024-10-12 18:31:38’, 5.9], [β€˜2024-10-13 01:22:08’, 3.0], [β€˜2024-10-13 07:03:04’, 6.2], [β€˜2024-10-13 13:59:08’, 2.5], [β€˜2024-10-13 19:38:33’, 6.4], [β€˜2024-10-14 02:38:11’, 2.4], [β€˜2024-10-14 08:01:52’, 6.8], [β€˜2024-10-14 15:05:56’, 1.9], [β€˜2024-10-14 20:33:18’, 7.0], [β€˜2024-10-15 03:37:48’, 1.8], [β€˜2024-10-15 08:51:33’, 7.4], [β€˜2024-10-15 16:04:31’, 1.3], [β€˜2024-10-15 21:19:56’, 7.5]]

What i’d like to be able to use notify for is to share the High and Low tides (if height is <5.0m then its low). Looking something like this…

High Tide 2024-10-09 01:32:33 at 6.9m
Low Tide 2024-10-09 09:00:16 at 2.2m
High Tide 2024-10-09 13:53:15 at 6.7m
Low Tide 2024-10-09 21:20:41 at 2.4m
High Tide 2024-10-09 13:53:15 at 6.7m
Low Tide 2024-10-10 09:42:05 at 2.6m
High Tide 2024-10-10 14:43:33 at 6.2m

Thanks very much in advance for your guidance?

If you just want to print the values, all you need is a for loop:

{% for p in state_attr('sensor.dungeness_tide','predictions') %}
{{- 'Low' if p[1] < 5 else 'High' }} Tide {{p[0]}} at {{p[1]}}m
{% endfor %}

If you have other requirements, you need to share them for us to advise on them.

@Didgeridrew, Thank you so so much. How would I be able to show only the first 6 rows? Also, you’ll notice that there are multiple entries per day. If this script ran midway through the day, is there a way to calculate showing only from the current time onwards (instead of showing the history for the day)?
Thanks for your guidance again.

Modify the template as follows:

{% set later = state_attr('sensor.dungeness_tide','predictions')
| selectattr(0, 'gt', now() | string ) | list %}
{% for p in later[:6] %}
  {{- 'Low' if p[1] < 5 else 'High' }} Tide {{p[0]}} at {{p[1]}}m
{% endfor %}

Absolutely brilliant. THANK YOU SO MUCH! #Legend

One last thing, I can see that the time is out by 1hr (UTC rather than BST). Is there a way to add a timezone offset to the time?

i.e. High Tide 2024-10-09 13:53:15 at 6.7m should be High Tide 2024-10-09 14:53:15 at 6.7m

{% set later = state_attr('sensor.dungeness_tide','predictions')
| selectattr(0, 'gt', utcnow() | string ) | list %}
{% for p in later[:6] %}
  {{- 'Low' if p[1] < 5 else 'High' }} Tide {{((p[0]~'+00:00')|as_datetime|as_local).strftime('%Y-%m-%d %H:%M') }} at {{p[1]}}m
{% endfor %}

You nailed it for me. Can’t thank you enough.