Z-Wave device battery level

I know this thread is old, but it fits my question :wink:

I have a zwave motion sensor that gives me several sensor nodes and a zwave.node_name entity, that shows up in the front-end:

How can I access the battery level with a value template?

That would look something like this:

- platform: template
  sensors:
    hall_sensor_battery:
      value_template: >-
        {%- if states.zwave.hall_sensor_4 -%}
          {{ states.zwave.hall_sensor_4.attributes.battery_level }}
        {%- else -%}
          n/a
        {%- endif -%}
      unit_of_measurement: '%'
      entity_id: zwave.hall_sensor_4
6 Likes

Thank you for your very quick answer! It works :slight_smile:

1 Like

My pleasure :slight_smile:

Wondering if anyone would know the answer to this, Prior to somewhere around release 0.39 or 0.40 when battery levels suddenly began to appear I was unable to get my Schlage zwave lock to show me a battery level through a template. I would get this message -> ‘Error rendering template: SecurityError: access to attribute ‘__32’ of ‘DomainStates’ object is unsafe.’

Release 0.39 or 0.40, I believe, began showing the battery levels and I didn’t need a template to have them show in the front end but with release 0.41 the battery levels disappeared again and I am back to having templates to show battery levels however just as prior to 0.39 I can not get my Schlage lock to show me a battery level.

Does anyone know of a solution to this message? -> Error rendering template: SecurityError: access to attribute ‘__32’ of ‘DomainStates’ object is unsafe. (zwave.__32 is the entitiy id of my lock, also I did set it up with a network key and it works as expected, just unable to show the battery level on the front end)

Thanks!

Try this for a template:

states.zwave["32"].attributes.battery_level

I tried this in the Dev Tools but received this error message -> “Error rendering template: TemplateSyntaxError: expected name or number”

{% if states.zwave.[“32”] %}
{{ states.zwave.[“32”].attributes.battery_level }}
{%- else -%}
n/a
{%- endif -%}

Since it’s a syntax error I am thinking it’s a typo or it doesn’t like the [“32”] for the name?

Remove the period between zwave and [“32”]. I didn’t put one there :wink:

Good catch, unfortunately that didn’t do it either. Sorry, one thing I failed to mention is that there is 2 underscores prior to the node number of 32 as shown below. If I do not use the underscores the message reads "Error rendering template: UndefinedError: ‘None’ has no attribute ‘attributes’.

“{{ states.zwave[”__32"].attributes.battery_level }}" in the Dev Tool the message it still displays -> “Error rendering template: SecurityError: access to attribute ‘__32’ of ‘DomainStates’ object is unsafe.”.

Does {{ states.zwave["__32"].state }} work in the Template editor in the Dev Tools?

No, same unsafe message:

{{ states.zwave["__32"].attributes.battery_level }} -> “Error rendering template: SecurityError: access to attribute ‘__32’ of ‘DomainStates’ object is unsafe.”

In that case I have no idea. It could be a bug.

This is the only thread that comes up on google so far I’ve found that has reference to the problem I’m having. It seems related to having a double underscore preceding any other values.

Hi everyone,

This thread it about a year old, but since it best describes the problem I have, I will ask here again :slight_smile:

Does anyone have a working template for “getting” the battery_level “out” so that it can be added to a group and be used in automations (for zwave devices)

I have this (more) zwave devices and I would like to “fetch” the battery_level and present it in a group, but I can’t get it to work.

The template for that should be: {{ states.zwave.hallway_pir.attributes.battery_level }}

1 Like

Use this script/package: Howto create battery alert without creating a template for every device

2 Likes

Thanks @fanaticDavid :slight_smile:

My bad ! - I created the template under binary_sensors.yaml – sooo placing them in sensors did the trick :wink:

Thanks @fanaticDavid for pointing me in the right direction.
If you want a battery icon that adapts to the battery level, you can use this config:

  garage_sensor_battery:
    friendly_name: 'Garage Batteri'
    value_template: >-
      {%- if states.zwave.aeotec_zw120_door_window_sensor_gen5 -%}
        {{ states.zwave.aeotec_zw120_door_window_sensor_gen5.attributes.battery_level }}
      {%- else -%}
        'unknown'
      {%- endif -%}
    icon_template: >-
      {%- if states.zwave.aeotec_zw120_door_window_sensor_gen5 -%}
        {% set battery_level =  states.zwave.aeotec_zw120_door_window_sensor_gen5.attributes.battery_level|int('unknown') %}
        {% set battery_round = (battery_level|int / 10)|int * 10 %}
        {% if battery_level == 'unknown' %}
          mdi:battery-unknown
        {% else %}
          {% if battery_round >= 100 %}
            mdi:battery
          {% elif battery_round > 0 %}
            mdi:battery-{{ battery_round }}
          {% else %}
            mdi:battery-alert
          {% endif %}
        {% endif %}
      {%- else -%}
        'unknown'
         mdi:battery-unknown
      {% endif %}
    unit_of_measurement: '%'
    entity_id: zwave.aeotec_zw120_door_window_sensor_gen5

or you can just set the device_class to ‘battery’ as below:

 office_pir_battery:
      friendly_name: 'office_pir_battery'
      unit_of_measurement: '%'
      device_class: battery
      value_template: >-
       {{ state_attr('zwave.office_pir','battery_level') }}
1 Like

I have been using this package and it seems to work pretty well. Had to tweak it a little for my notification and front end needs, but no complaints so far. Great work by the creator.

2 Likes