Strange error about script, and attribute lower, neither of which are used in automation

please check with me…I am lost :wink: Ive made an error in the bottom line (referencing the trigger.to_state, while I should have referenced the binary_sensor in the condition. Having said that, I completely fail to see why the I see this error:


about an attribute lower in the script execution using this automation:

  - alias: Uv Alert
    id: 'Uv Alert'
    trigger:
      platform: state
      entity_id: sensor.dark_sky_uv_index
    condition:
      condition: state
      entity_id: binary_sensor.uv_alert
      state: 'on'
    action:
      service: notify.notify
      data_template:
        title: >
         {{as_timestamp(now())|timestamp_custom('%X')}} : Uv Alert: {{trigger.to_state.state}}
        message: >
          {% set mapper = {'1':'Zeer laag','2':'Zeer laag','3':'Laag',
                           '4':'Laag','5':'Matig','6':'Matig',
                           '7':'Hoog','8':'Hoog','9':'Zeer hoog',
                           '10':'Zeer hoog'} %}
          {{trigger.to_state.name}} {{'verbeterde'
          if trigger.to_state.state|int < trigger.from_state.state|int
           else 'verslechterde'}} from {{trigger.from_state.state}} -
           {{mapper[trigger.from_state.state]}} to
            {{trigger.to_state.state}} - {{mapper[trigger.to_state.state]}}.
            Maximaal {{state_attr(trigger.to_state, 'Maximaal in zon')}}

more-info on the logged error in 2 images because of too large for my screen:

If anyone can spot the attribute lower…

btw, this is the referenced binary_sensor in the condition, but, since it is not used in the bottom notification template that shouldn’t even matter.

Shouldn’t I simply get an error about trigger.to_state not having the attribute ‘Maximaal in de zon’ ?

binary_sensor:

  - platform: template
    sensors:
      uv_alert:
        friendly_name: Uv Alert
        value_template: >
          {{states('sensor.dark_sky_uv_index')|int > 4}}
        attribute_templates:
          Uv index: >
            {{states('sensor.dark_sky_uv_index')|int}}
          Omschrijving: >
            {% set index = states('sensor.dark_sky_uv_index') %}
            {% set mapper = {'1':'Zeer laag','2':'Zeer laag','3':'Laag',
                   '4':'Laag','5':'Matig','6':'Matig',
                   '7':'Hoog','8':'Hoog','9':'Zeer hoog',
                   '10':'Zeer hoog'} %}
            {{mapper[index] if index in mapper else 'unknown'}}
          Maximaal in zon: >
            {% set index = states('sensor.dark_sky_uv_index') %}
            {% set mapper = {'1':'100 - 50','2':'100 - 50','3':'35 - 25',
                   '4':'35 - 25','5':'25 - 15','6':'25 - 15',
                   '7':'15 - 10','8':'15 - 10','9':'minder dan 10',
                   '10':'minder dan 10'} %}
            {{mapper[index] if index in mapper else 'unknown'}} minuten.
        device_class: safety

thanks for having a look

You’re sending a state object to state_attr. Change it to trigger.entity_id or trigger.to_state.entity_id

Ok thanks will have a look there, but that bit is actually working… this is the message that is logged:

27 May: 17:02:09 - notify: Dark Sky UV Index verslechterde from 1 - mapper[trigger.from_state.state] to
 8 - mapper[trigger.to_state.state]. Maximaal 15 - 10 minuten.
 in de zon!

I cant get the mappers to work in the templates.

should have posted the correct version, sorry:

        message: >
          {% set mapper = {'1':'Zeer laag','2':'Zeer laag','3':'Laag',
                           '4':'Laag','5':'Matig','6':'Matig',
                           '7':'Hoog',8:'Hoog',9:'Zeer hoog',
                           10:'Zeer hoog'} %}
          {{trigger.to_state.name}} {{'verbeterde' if trigger.to_state.state|int < trigger.from_state.state|int
           else 'verslechterde'}} van {{trigger.from_state.state}} - {{mapper[trigger.from_state.state]}} tot
           {{trigger.to_state.state}} - {{mapper[trigger.to_state.state]}}. Maximaal {{state_attr('binary_sensor.uv_alert','Maximaal in zon')}}
           in de zon!

I have set 8, 9 and 10 as numbers, to see if the error is in using a string or number, but that doesnt matter. simulating it in developer-tools/template works with the strings alright.

fixed it.

  - alias: Uv Alert
    id: 'Uv Alert'
    trigger:
      platform: state
      entity_id: sensor.dark_sky_uv_index
    condition:
      condition: state
      entity_id: binary_sensor.uv_alert
      state: 'on'
    action:
      service: notify.notify
      data_template:
        title: >
          {% set now = as_timestamp(now())|timestamp_custom('%X') %}
          {% set tostate = trigger.to_state.state %}
          {% set omschrijving = state_attr('binary_sensor.uv_alert','Omschrijving') %}
          {{now}} : Uv Alert: {{tostate}} - {{omschrijving}}
        message: >
          {% set mapper = {'1':'Zeer laag','2':'Zeer laag','3':'Laag',
                           '4':'Laag','5':'Matig','6':'Matig',
                           '7':'Hoog','8':'Hoog','9':'Zeer hoog',
                           '10':'Zeer hoog'} %}
          {% set tostate = trigger.to_state.state %}
          {% set fromstate = trigger.from_state.state %}
          {% set max = state_attr('binary_sensor.uv_alert','Maximaal in zon') %}
          {{trigger.to_state.name}} {{'verbeterde' if tostate|int < fromstate|int
           else 'verslechterde'}} van {{fromstate}} -  {{mapper[fromstate]}} tot
           {{tostate}} - {{mapper[tostate]}}. Maximaal {{max}} in de zon!

still don’t get the original error… must be HA’s internal inimitable logic
though my automation now works, I am hesitant to mark this as solved, because of the original quest and title.

original error was python and had nothing to do with your template. It’s typically referred as an "unhandled exception’.

Just guessing here, but I think the state_attr( …) was the problem as @petro pointed out. The second last line of the error says:

return self_states.get(entity_id.lower())

So it tries to get the state of the entity by first making the given string all lowercase, with the method lower(). However you did not pass a string, you passed an object and in python you access attributes of objects with the dot notation, therefore it is looking for the attribute lower(), which of course doesn’t exist in the passed object. I hope you understand what I mean :rofl:

1 Like

Oh yeah, it definitely was, which is why I pointed it out.

Marked solved. I see it now… thanks!