Help with templating (setting variables and some formatting)

Hi all,

I’m struggling with templates (sort of new to that) right now.

What I’m trying to achieve:

- alias: 'Alarm Triggered'
  initial_state: true
  trigger:
    - platform: state
      entity_id: sensor.visonic_panel_alarm_status
    - platform: state
      entity_id: sensor.visonic_panel_trouble_status
  condition:
    condition: template
    value_template: "{{ trigger.to_state.state != 'None' }}"
  action:
    - service: notify.telegram_oliver
      data_template:
        message: " Alarm Status changed!
        
        Panel last event is {{ state_attr('alarm_control_panel.visonic_alarm', 'Panel Last Event') }}
        
        Alarm is last triggered by {{ state_attr( 'XXXX', 'friendly_name') }}"

Now I would like the XXXX in the last line to be filled by the following template value:

{{ state_attr('alarm_control_panel.visonic_alarm', 'Panel Last Event Data'['Entity']) }}

The above works, and actually gives me a string that refers to an entitiy (that is actually a valid binary sensor. Now i would like to have the friendly name of this sensor printed instead of the entity name (that doesnt make sense to me).

Can i set a variable somewhere before the data_template part? Or within?

Furthermore, I often see a notifier message beginning with a >. Why is that? I know it is used to break strings over multiple lines, by the if I just add a linebreak (or 2) it works as well).

I think you’re looking for something like this?

  action:
    - service: notify.telegram_oliver
      data_template:
        message: >
          {% set alarm_id = 'alarm_control_panel.visonic_alarm' %}
          {% set sensor_id = state_attr(alarm_id, 'Panel Last Event Data'['Entity']) %}
          Alarm Status changed!
          Panel last event is {{ state_attr(alarm_id, 'Panel Last Event') }}
          Alarm is last triggered by {{ state_attr(sensor_id, 'friendly_name') }}

This should be close. The > is for multiple line templates whereas if it is a single line it’s enclosed in quotes. Notice I used > and removed your original quotes.

1 Like

Ok Tnx! that explains a lot. However if the automation runs it still generates an error:

Alarm Triggered: Error executing script. Unexpected error for call_service at pos 1: ‘NoneType’ object has no attribute ‘lower’

The first line (Panel last event is) is not the problem, because that works if I comment the second line.
I’m kind of trying to see the effect, because there are quite some states the alarm panel goes trough. With every state the panel sometimes gives an entity, but sometimes the value of “entity” is null or none.
I guess this is what I’m hitting here.

Is there a way to print the variable in the automation to see what the actual value is?
After that i guess I could try an if statement that is the value is “null” or "none"or doesn’t start with “binary_sensor” (is that possible?), that part of the automation is not triggered.

I treid this:

    - service: notify.telegram_oliver
      data_template:
        message: >
          {% set alarm_id = 'alarm_control_panel.visonic_alarm' %}
          {% set sensor_id = state_attr(alarm_id, 'Panel Last Event Data'['Entity']) %}
          Alarm Status changed! Panel Alarm status is now {{ state_attr(alarm_id, 'Panel Alarm Status') }}
          {% if sensor_id.startswith('binary_sensor') %}
            Alarm is last triggered by {{ state_attr(sensor_id, 'friendly_name') }}
          {% else %}
          {% endif %}

But this again results in:

Alarm Triggered: Error executing script. Unexpected error for call_service at pos 1: Error rendering data template: UndefinedError: ‘None’ has no attribute ‘startswith’

‘none’ is hard to work with :slight_smile:

Well I was kind of guessing what your system was returning. Based on your code I assumed that ‘Panel Last Event Data’ was a dictionary and that the key ‘Entity’ would return the entity_id (domain.object_id) of the last event. Perhaps you could tell me which integration you are using or the state and attribute structure of the Panel Last Event Data.

Hi micque, tnx for your helped but i already fixed it. I was actually using the wrong triggers (based on state) while this component is actually pushing HA events to work with. However, your explanation about the formating/use of data_template helped a lot :).