I thought this might be useful for others. I made it since I have quite a few z-wave devices, and I don’t want to create template sensors for each one of them.
# put this in a file named custom_components/battery_state.py
import logging
from homeassistant.helpers.event import track_state_change
from homeassistant.const import STATE_ON, STATE_OFF, STATE_HOME, STATE_NOT_HOME, MATCH_ALL
_LOGGER = logging.getLogger(__name__)
DOMAIN = 'battery'
DEPENDENCIES = []
def setup(hass, config=None):
"""Setup the Battery component. """
_LOGGER.info("The 'battery' component is ready!")
def state_changed(entity_id, old_state, new_state):
if new_state is None:
return
if 'battery_level' in new_state.attributes:
hass.states.set('%s_battery' % entity_id,
float(new_state.attributes['battery_level']),
{
'friendly_name': "%s Battery" % new_state.attributes['friendly_name'],
'unit_of_measurement': '%',
'icon': 'mdi:battery'
})
track_state_change(hass, MATCH_ALL, state_changed)
return True
Caveat: This creates a new battery sensor for every sensor it detects, so you may end up with multiple battery sensors per device if that device reports multiple sensors.
Very cool - I’ve been looking at something similar using the RESTFul API - some of my devices report an attribute “battery” not “battery_level” so you may want to add that to your code.
Thank you for the custom component. I use the vera hub for my z-wave devices but had the same problem.
I modified version of the code a bit so that the battery values were all set as sensors and not as whatever the parent component was. What I found was that if my sensor was a binary_sensor the value not display properly.
I was then also able to use the same custom_component template and modify it to extract the power usage from my vera switches.
Edit:
I forgot to add that when geting the values from the vera component they already had a ‘%’ on the value. I removed theis by adding a ‘[:-1]’ to the line getting the attributes. This removes the last character of the string.
So, when I first started creating individual template sensors for each of my devices battery states, I was told that my sensors would be polling the battery constantly for changes to the battery state, which was causing more unnecessary drain on each devices battery as it was constantly asking the device for its battery state live, for every percent change. So I was told to add the additional entity_id: reference as the last line of my sensor blocks so that it would only check for the battery level when the state changed on the device listed in entity_id:, and thus causing no extra drain on the battery as it was only reporting back when a state changed on my conditional device instead of consistently polling for changes.
Since I don’t need to know the battery levels for every percent it drops, I created an input_boolean called input_boolean.update_batteries and I have an automation that turns the input_boolean on if its off, and off if its on, every day at 6pm, and since each of my battery sensors are only updating on a state change of the input_boolean.update_batteries, it only polls my battery levels once a day.
So I haven’t tested it with your component, but for those that don’t want this component to start draining their batteries faster, you may want to try adding the extra entity_id: option into this component to see if you can save yourself from buying new batteries before you need to.
Of course, I don’t know how if you can simply add that line underneath the line that says ‘icon’: ‘mdi:battery’ and have it work, since the extra entity_id option may only be a feature of a template sensor, but you can try and report back to let us know if it works.
If not, I personally won’t be using this component unless there is a way to limit when it is polling my batteries.
Here is an example of one of my battery sensors so you can see what I mean:
This may have something to do with whether you are using usb zwave direct connection or via a hub such as Vera.
I am using my zwave devices via a VeraLite hub and have not noticed any difference to the battery life, yet…
This would make sense in my case, as far as I know querying the Vera http API does not force polling of devices.
I am no zwave expert but from my experience using the VeraLite hub with my devices I cannot wake up a battery device by polling it. My devices will only send/receive information when woken by the appropriate physical input such as internal timer, motion or door opening. I can change the device parameters to make it stay awake and send information, this does have a significant impact on battery life. But to do this i need to set the new parameters in the hub and then manually wake the device up to update the device parameters.
But as I said, I am no zwave expert and only have my setup to go by.
This is interesting. I am actually looking for some way to create a notification once a battery of any device drops below a certain threshold the. Otification then should just contain the name of the device.
I like this as I wanted to see the battery levels of my door sensors and you did warn that it would create multiple sensors. However, for some reason I cannot hide the extra sensors. I created lines for each of them in my customize.yaml but they still appear. I’m not getting any errors or anything in the log either.
Sorry, I haven’t really been around lately to reply to everyone. I pasted a new version of this script below. I changed it to work like a normal component with a config, and included the advice of some of the replies to this thread.
Personally, I have been using a whitelist of sensors I want to get battery information from. Here’s my example config, with the script saved as custom_components/battery_state.py:
@tinglis1 thanks for the idea! I was thinking a ‘battery attribute sensor’ would be a weird/too specific of a component to be included, but I really like your idea of making a generic attribute sensor.
I’ll have some time next week that I could work on this idea and get something submitted.
Go in the configuration.yaml file like that? I tried it in mine just like that (well, with my own sensors obviously) and nothing is showing up. I am not getting any errors but am not seeing anything in HA or even in the entities list.