Nammi
(Nammi)
October 12, 2018, 12:44am
1
I have Googled and searched for a resolution to my problem, but alas I’m not sure where I am going wrong.
I would like to simply show the current battery percentage for my sensor and switches in their own tab.
To begin with, in my configuration.yaml I have the following for one sensor:
sensor:
platform: template
sensors:
unused_switch:
friendly_name: ‘Test’
value_template: ‘{{ states.sensor.unused_switch.attributes.battery_level }}’
unit_of_measurement: ‘%’
entity_id: sensor.0x00158d0001e7932d
device_class: battery
The it shows the %, but not the actual value. I have attached screen shots of the output as well as a screenshots of the states page.
Any pointers as to what I am doing wrong please?
allan
(Allan)
October 12, 2018, 1:27am
2
Can you quote your config section (as shown in the blue box at the page top, it makes it easier for people to check your layout/syntax).
Have you tried using the entity_id in the template instead of the friendly_name?
value_template: ‘{{ states.sensor.unused_switch.attributes.battery_level }}’
becomes
value_template: ‘{{ states.sensor.0x00158d0001e7932d.attributes.battery }}’
Also have you tried experimenting in the templates section of the developer tools? You can tryout your template strings without having to save and reload and see the results in almost realtime.
I also just noticed that your template is battery_level but the entity is battery
poebae
October 12, 2018, 1:38am
3
What is “sensor.unused_switch”? Is it an actual sensor that exists in your setup? If not, that’s why it’s not fetching a value.
If you’re trying to get the battery level of the sensor it needs to be
" {{ states.sensor.0x00158d0001e7932d.attributes.battery }} "
Note that it’s battery, not battery_level.
Nammi
(Nammi)
October 12, 2018, 2:21am
4
Hi both,
Many thanks for your replies, I have updated my configuration file to read:
sensor:
- platform: template
sensors:
unused_switch:
friendly_name: 'Test'
value_template: ‘{{ states.sensor.0x00158d0001e7932d.attributes.battery }}’
unit_of_measurement: '%'
entity_id: sensor.0x00158d0001e7932d
device_class: battery
Unfortunately it returns the error:
Error rendering template: TemplateSyntaxError: expected token 'end of print statement', got 'x00158d0001e7932d'
I have attempted @poebae advice and updated the value template to:
" {{ states.sensor.sensor.0x00158d0001e7932d.attributes.battery }} "
But this also returns the same error.
@allan , thanks for the advice on formatting and the template section. Like a newbie I have been making the changes in the files themselves and then reloading the scripts and sometimes the raspberry pi itself.
I am also attaching two screen shots highlighting the sensor itself. The sensor is there, it exists and when I use the click function the system does recognise it.
What’s the result when you put the expression
{{ states.sensor.0x00158d0001e7932d.attributes.battery }}
in the /dev-template?
If that doesn’t give you the battery level there might be something wrong with your device_id (I’m not 100% sure but I seem to remember that the device ID shouldn’t start with a number).
And I think you need to take the line out that shows the entity_id itself - its what we call a DoppelMoppel
1 Like
poebae
October 12, 2018, 2:59am
6
Sorry, I’d accidentally written ‘sensor’ twice. Try this:
" {{ states.sensor.0x00158d0001e7932d.attributes.battery }} "
with double quotes " "
tom_l
October 12, 2018, 3:12am
7
Correct.
See the note at the bottom of this section: Templating - Home Assistant
So @Nammi needs to use:
{{ states.sensor['0x00158d0001e7932d'].attributes.battery }}
1 Like
Nammi
(Nammi)
October 12, 2018, 7:40am
8
@tom_l Your suggestion with the brackets has corrected my errors, fantastic.
My configuration.yaml now reads as follows:
sensor:
- platform: template
sensors:
unused_switch:
friendly_name: 'Test'
value_template: '{{ state_attr('sensor.[0x00158d0001e7932d]', 'battery') }}'
unit_of_measurement: '%'
device_class: battery
- platform: template
sensors:
front_door_switch:
friendly_name: 'Front Door Switch'
value_template: '{{ state_attr('binary_sensor.[0x00158d00022f1391]', 'battery') }}'
unit_of_measurement: '%'
device_class: battery
However, the battery now only shows as ‘None’. I have tried adding in a second device to see if it would show the battery information of it, but it also returns the same result.
tom_l
October 12, 2018, 8:03am
9
Did you try it with the one I (finally) suggested:
{{ states.sensor['0x00158d0001e7932d'].attributes.battery }}
Nammi
(Nammi)
October 12, 2018, 8:43am
10
Sorry I must have over looked it that message!
I can confirm using your last comment it now shows the battery percentage.
I spent WAY longer on this than I had originally anticipated, such is the life of a newbie.
For anyone else who comes across this same error, the following works for me:
sensor:
- platform: template
sensors:
unused_switch:
friendly_name: 'Test'
value_template: '{{ state_attr('sensor.[0x00158d0001e7932d]', 'battery') }}'
unit_of_measurement: '%'
device_class: battery
2 Likes
I think you will probably have this thought many, many more times:
“I spent WAY longer on this than I had originally anticipated, such is the life!”
I’ve been working with HA for almost two years now - and it seems to be getting worse rather than better
2 Likes