Check Battery Level for Door Sensors


I have about 8 door sensors.

I would like to run a script every day at specific time to check the battery level of all door sensors, if the battery level under specific threshold, it should call a service to send me sms message.

I have created the below script.

I am getting this error when running the script:

“Failed to call service script/test. Error rendering data template: TemplateRuntimeError: cannot assign attribute on non-namespace object”

Anyone can help?

alias: Battery Level Check
{%- for entity_id in ( states.binary_sensor | map(attribute=‘entity_id’)) -%}
{%- if (state_attr(entity_id,‘battery_level’) | int < 2) -%}
{%- if (state_attr(entity_id,‘battery_level’) != None) -%}

  • service: input_text.set_value
    entity_id: input_text.sms_message
    value: {{ state_attr(entity_id,‘friendly_name’) }} Battery Level {{ state_attr(entity_id,‘battery_level’) }}
  • service: rest_command.sms_battery_low_message
    {% endif -%}
    {% endif -%}
    {%- endfor -%}
    mode: single

Please see the sticky post - particularly section 11 on formatting code.

Many thanks.
I have no issue with formatting.
Somehow, the format was modified when posted this topic.

Because you didn’t use code markup, which is explained in that sticky post.

You can try the following blueprint