All my battery templates stopped working

I’m currently on 0.82.0. Not sure which version upgrade when they stopped working. Here is an example which stays “unknown” When I check its battery_level attribute, it reports “99”.
Can someone steer me in the right direction?

   - platform: template
    sensors:
      ecolink_dw_battery:
        friendly_name: Ecolink DW Battery
        value_template: "{% if zwave.ecolink_motion_detector_2 %}
          {{ states.zwave.ecolink_motion_detector_2.attributes.battery_level }}
            {% else %}
              unknown
            {% endif %}"
        unit_of_measurement: '%'

There was a breaking change with 0.81 re template sensors.
I don’t fully understand when (and when not) you have to include an entity_id in your config.

Looking forward to getting a little more explanation here now as well.

I got my worktime sensor back to work by inserting the entity_id as shown here:

- platform: template
  sensors:
    worktime:
      friendly_name: WorkTime
      entity_id: sensor.date_time
# worktime is Mon-Fri, starts at 08:00h and lasts until 16:59h
      value_template: '{{ now().weekday() in (0,1,2,3,4) and now().hour >=8 and now().hour < 17 }}'

So, I assume by including entity_id: zwave.ecolink_motion_detector_2 it would work.

1 Like

As @chairstacker says, the system can’t work out which entity to track because of your if statement.

I would also add that your if statement seems a convoluted way of doing this, the state_attr template extension is designed for this exact purpose, and also provides an entity_id to track…

  - platform: template
    sensors:
      ecolink_dw_battery:
        friendly_name: Ecolink DW Battery
        value_template: "{{ state_attr('zwave.ecolink_motion_detector_2' , 'battery_level') }}" 
        unit_of_measurement: '%'
2 Likes

@chairstacker - to answer your question, the template you’ve pasted contains no entities. Now() templates are only evaluated at that exact moment, so by adding the sensor.time as an entity_id to track, the system can reevaluate the template every time the time sensor changes (which is every minute, on the minute).

You could, for example, change that entity_id to your device tracker, and then it would only be reevaluated every time you move, or your living room light and it only be reevaluated when you turn them on/off. The actual output of the sensor is the same as it always was, but it only updates that value when the entity it is tracking changes state.

Hope that makes sense.

Understood - that’s an explanation I understand :+1:
Wasn’t able to get that from the original description of the breaking change, though.

2 Likes

Thanks to both you and @chairstacker. I’ve made progress I think but it still doesn’t say 99%. Instead of saying unknown now, it says None %. Not sure if I have everything the way I should. Here is what I have:

  - platform: template
    sensors:
      ecolink_dw_battery:
        friendly_name: Ecolink DW Battery
        entity_id: sensor.date_time
        value_template: "{{ state_attr('zwave.ecolink_motion_detector_2' , 'battery_level') }}" 
        unit_of_measurement: '%'

If you’re getting ‘none’, would suggest that either the entity_id is wrong or the name of the battery attribute is wrong.

[zwave.ecolink_motion_detector_2](https://192.168.1.38:8123/dev-state#) initializing node_id: 7 node_name: Ecolink Motion Detector manufacturer_name: Ecolink product_name: Motion Detector query_stage: CacheLoad is_awake: false is_ready: false is_failed: false is_info_received: true max_baud_rate: 40000 is_zwave_plus: true capabilities: zwave_plus,beaming,routing sentCnt: 1 sentFailed: 1 retries: 0 receivedCnt: 4 receivedDups: 0 receivedUnsolicited: 4 sentTS: 2018-11-15 20:51:41:589 receivedTS: 2018-11-15 20:56:10:114 lastRequestRTT: 0 averageRequestRTT: 0 lastResponseRTT: 0 averageResponseRTT: 0 battery_level: 99 wake_up_interval: 14400 friendly_name: Ecolink DW Motion Detector

Above is a copy and paste of that sensor from the states page.

How bizarre - try…

  - platform: template
    sensors:
      ecolink_dw_battery:
        friendly_name: Ecolink DW Battery
        entity_id: zwave.ecolink_motion_detector_2
        value_template: "{{ state_attr('zwave.ecolink_motion_detector_2' , 'battery_level') }}" 
        unit_of_measurement: '%'

Edit - alignment!

1 Like

And also confirm you’re restarting homeassistant in between these tries, not just reloading the core?

1 Like

Yes, I am restarting each time.

That fixed it! Thank you! So the entity_id needs to be the sensor name? I’ve got a Neo Motion Zwave sensor with the same problem.

1 Like

It kinda should be in most circumstances, but it doesn’t have to.

The problem with using a different entity_id is that if the entity you actually want updates from is doing something weird when your template updates you’re going to get duff info, but using the same entity_id means it’s always in sync.

As per my first example though, in this case you shouldn’t need the entity_id line at all, because the entity_id to track is already in the template and is already unambiguous.

2 Likes

I’m going through each one of my zwave sensors and making the changes. I can’t just add the entity_id to fix it. It only works if I also change it to your code using state_attr. All this code worked a couple of weeks ago lol. I would never have figured that out on my on. Thanks again.

2 Likes

@anon43302295: Thanks a million for this–it works perfectly for me as well.

I’d add that the addition of device_class: battery under the definition also gives it a nice dynamic battery icon representing the actual charge state, too. It’s an easy change that adds a bit of polish. (Also not my own insight, but since I didn’t see it mentioned here, I figured I’d chime in.)

1 Like