Dear, I have a strange behavior.
I try to visualize the battery level of some sensor. For most of them works fine. For some I have the visualization of “-” instead a temperature. For example bat_te_1 is correct visualized but bat_te_2 no.
but I retreive this error.
Error loading /home/homeassistant/.homeassistant/configuration.yaml: while parsing a block mapping in “/home/homeassistant/.homeassistant/packages/battery.yaml”, line 208, column 9 expected <block end>, but found ‘<scalar>’ in “/home/homeassistant/.homeassistant/packages/battery.yaml”, line 212, column 47
If I try to use
states.sensor[‘2’].attributes.battery_level
the configuration is ok but the same is showed - instead the battery level.
If I copy this value_template: '{% if states.binary_sensor['2'].attributes.battery_level %} {{ states.binary_sensor['2].attributes.battery_level}} {% else %} Unknown {% endif %}'
in the template the result is:
but if I try to check the configuration I have this error:
Error loading /home/homeassistant/.homeassistant/configuration.yaml: while parsing a block mapping in “/home/homeassistant/.homeassistant/packages/battery.yaml”, line 208, column 9 expected <block end>, but found ‘<scalar>’ in “/home/homeassistant/.homeassistant/packages/battery.yaml”, line 212, column 47
The problem with the last one is that you have nested the same quotes type in the template. And you are missing a second quote mark in the “else” statement entity_id at the ['2]. And you seemed to have switched from “sensor” to “binary_sensor”?
Try this:
value_template: '{% if states.sensor["2"].attributes.battery_level %}{{ states.sensor["2"].attributes.battery_level}}{% else %}Unknown{% endif %}'
I think we should carry on with your issue in this thread and leave the other one to determine if it’s a bug or not.
based on this:
I have always the same error:
Error loading /home/homeassistant/.homeassistant/configuration.yaml: while parsing a block mapping in “/home/homeassistant/.homeassistant/packages/battery.yaml”, line 208, column 9 expected <block end>, but found ‘<block mapping start>’ in “/home/homeassistant/.homeassistant/packages/battery.yaml”, line 213, column 10
can you please post you entire code around this sensor that you are trying to create?
As far as I can see the template looks syntactically correct even though it produces incorrect results. there may be something else outside of the template causing your issue.
What are lines “208” & “213” in the battery package code?
I’m not sure why you have all those extra line spaces in your config but I’ll tell you that it makes it extremely difficult to find syntax issues.
I copied your code above into my text editor and removed all of the extra blank lines and found several indentation errors that I think are giving you the “mapping” errors you are seeing. Here is the “corrected” version. I don’t use packages but I’m pretty confident that the way I’ve rewritten it will be correct:
#################################################################
## Battery package
#################################################################
# Visualizza lo stato delle batterie di sensori e altro
# Invia notifica persistent e su telegram quando una batteria
# scende sotto una certa soglia
#################################################################
homeassistant:
customize:
# Node Anchors
package.node_anchors:
customize: &customize
package: 'battery_alert'
expose: &expose
<<: *customize
haaska_hidden: false
homebridge_hidden: false
# Group
group.battery_alert:
<<: *customize
friendly_name: "Battery Alert"
icon: mdi:steam
# Automation
automation.battery_alert:
<<: *customize
friendly_name: "Battery Alert"
#################################################################
## Input number
#################################################################
input_number:
soglia_minima_batterie:
name: Soglia minima batteria
min: 5
max: 99
step: 1
#################################################################
## Sensors
#################################################################
sensor:
- platform: template
sensors:
bat_oc_5:
friendly_name: "BATTERIA PORTA STANZA BLU 5"
unit_of_measurement: '%'
value_template: '{% if states.binary_sensor.openclose_17 %} {{ states.binary_sensor.openclose_17.attributes.battery_level}} {% else %} Unknown {% endif %}'
icon_template: mdi:battery
- platform: template
sensors:
bat_oc_1:
friendly_name: "BATTERIA PORTA GIARDINO 1"
unit_of_measurement: '%'
value_template: '{% if states.binary_sensor.openclose_4 %} {{ states.binary_sensor.openclose_4.attributes.battery_level}} {% else %} Unknown {% endif %}'
icon_template: mdi:battery
- platform: template
sensors:
bat_oc_2:
friendly_name: "BATTERIA PORTA INGRESSO 2"
unit_of_measurement: '%'
value_template: '{% if states.binary_sensor.openclose_5 %} {{ states.binary_sensor.openclose_5.attributes.battery_level}} {% else %} Unknown {% endif %}'
icon_template: mdi:battery
- platform: template
sensors:
bat_oc_3:
friendly_name: "BATTERIA PORTA BAGNO GIU 3"
unit_of_measurement: '%'
value_template: '{% if states.binary_sensor.openclose_6 %} {{ states.binary_sensor.openclose_6.attributes.battery_level}} {% else %} Unknown {% endif %}'
icon_template: mdi:battery
- platform: template
sensors:
bat_oc_4:
friendly_name: "BATTERIA PORTA BAGNO ALF 4"
unit_of_measurement: '%'
value_template: '{% if states.binary_sensor.openclose_7 %} {{ states.binary_sensor.openclose_7.attributes.battery_level}} {% else %} Unknown {% endif %}'
icon_template: mdi:battery
- platform: template
sensors:
bat_pr_1:
friendly_name: "BATTERIA MOVIMENTO CUCINA 1"
unit_of_measurement: '%'
value_template: '{% if states.binary_sensor.presence_18 %} {{ states.binary_sensor.presence_18.attributes.battery_level}} {% else %} Unknown {% endif %}'
icon_template: mdi:battery
- platform: template
sensors:
bat_pr_2:
friendly_name: "BATTERIA MOVIMENTO SCALE 2"
unit_of_measurement: '%'
value_template: '{% if states.binary_sensor.presence_21 %} {{ states.binary_sensor.presence_21.attributes.battery_level}} {% else %} Unknown {% endif %}'
icon_template: mdi:battery
- platform: template
sensors:
bat_te_1:
friendly_name: "BATTERIA TEMPERATURA 1"
unit_of_measurement: '%'
value_template: '{% if states.sensor.temperature_8.attributes.battery_level %} {{ states.sensor.temperature_8.attributes.battery_level}} {% else %} Unknown {% endif %}'
icon_template: mdi:battery
- platform: template
sensors:
bat_te_2:
friendly_name: "BATTERIA TEMPERATURA 2"
unit_of_measurement: '%'
value_template: '{% if states.sensor.2.attributes.battery_level %} {{ states.sensor.2.attributes.battery_level}} {% else %} Unknown {% endif %}'
icon_template: mdi:battery
- platform: template
sensors:
bat_te_3:
friendly_name: "BATTERIA TEMPERATURA 3"
unit_of_measurement: '%'
value_template: '{% if states.sensor.3.attributes.battery_level %} {{ states.sensor.3.attributes.battery_level}} {% else %} Unknown {% endif %}'
icon_template: mdi:battery
- platform: template
sensors:
bat_wa_1:
friendly_name: "BATTERIALLAGAMENTO BALCONE"
unit_of_measurement: '%'
value_template: '{% if states.binary_sensor.water_19.attributes.battery_level %} {{ states.binary_sensor.water_19.attributes.battery_level}} {% else %} Unknown {% endif %}'
icon_template: mdi:battery
#################################################################
## Groups
#################################################################
group:
# Scheda batterie
card_battery:
name: Battery
view: yes
icon: mdi:battery
entities:
- sensor.bat_oc_5
- sensor.bat_oc_4
- sensor.bat_oc_3
- sensor.bat_oc_2
- sensor.bat_oc_1
- sensor.bat_pr_1
- sensor.bat_pr_2
- sensor.bat_te_1
- sensor.bat_te_2
- sensor.bat_te_3
- sensor.bat_wa_1
- input_number.soglia_minima_batterie
#################################################################
## Automazioni
#################################################################
# Notifica in caso di batteria troppo bassa
automation:
- alias: Allarme batteria
initial_state: true
trigger:
- platform: time
at: '20:30:00'
condition:
- condition: template
value_template: >
{% macro battery_level() %}
{%- set threshold = states.input_number.soglia_minima_batterie.state | int -%}
{% set domains = ['light', 'switch', 'sensor', 'binary_sensor', 'zwave', 'lock'] %}
{% for domain in domains -%}
{% for item in states[domain] if ((item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) or ("battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown"))) -%}
{% if (item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) -%}
{{ item.name }} ({{ item.attributes['battery_level'] }}){%- if not loop.last %}, {% endif -%}{% endif -%}
{% if "battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown") -%}
{{ item.name }} ({{ item.state }}){% if not loop.last %}, {%- endif %} {% endif -%}
{% endfor %}
{%- endfor %}
{% endmacro %}
{{ battery_level() |trim != "" }}
action:
- service: persistent_notification.create
data_template:
title: Attenzione! Batterie sotto la soglia impostata
notification_id: low-battery-alert
message: >
{% macro battery_level() %}
{%- set threshold = states.input_number.soglia_minima_batterie.state | int -%}
{% set domains = ['light', 'switch', 'sensor', 'binary_sensor', 'zwave', 'lock'] %}
{% for domain in domains -%}
{% for item in states[domain] if ((item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) or ("battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown"))) -%}
{% if not "umidit" in item.name | lower and not "press" in item.name | lower -%}
{% if (item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) -%}
{{ item.name }} ({{ item.attributes['battery_level'] }}){%- if not loop.last %}, {% endif -%}{% endif -%}
{% if "battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown") -%}
{{ item.name }} ({{ item.state }}){% if not loop.last %}, {%- endif %} {% endif -%}
{% endif -%}
{% endfor %}
{%- endfor %}
{% endmacro %}
{{ battery_level() }}
- service: notify.PUSHOVER
data:
title: "Attenzione! Batterie sotto la soglia impostata"
message: >
{% macro battery_level() %}
{%- set threshold = states.input_number.soglia_minima_batterie.state | int -%}
{% set domains = ['light', 'switch', 'sensor', 'binary_sensor', 'zwave', 'lock'] %}
{% for domain in domains -%}
{% for item in states[domain] if ((item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) or ("battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown"))) -%}
{% if not "umidit" in item.name | lower and not "press" in item.name | lower -%}
{% if (item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) -%}
{{ item.name }} ({{ item.attributes['battery_level'] }}){%- if not loop.last %}, {% endif -%}{% endif -%}
{% if "battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown") -%}
{{ item.name }} ({{ item.state }}){% if not loop.last %}, {%- endif %} {% endif -%}
{% endif -%}
{% endfor %}
{%- endfor %}
{% endmacro %}
{{ battery_level()}}
The only thing I wasn’t too sure about is the huge templates in the actions section but even without knowing all of the various levels of “for” and “if” and “macro” I think think this should be OK too.
You should be able to just copy and past that into your config and (I think) it will work.
Once you get that fixed then hopefully we can get the sensor template worked out.
I have this error:
Error loading /home/homeassistant/.homeassistant/configuration.yaml: while scanning for the next token found character ‘%’ that cannot start any token in “/home/homeassistant/.homeassistant/packages/battery.yaml”, line 211, column 26
If I use this code: value_template: '{% if states.sensor['2'].attributes.battery_level %} {{ states.sensor['2'].attributes.battery_level}} {% else %} Unknown {% endif %}'
I have this error:
Error loading /home/homeassistant/.homeassistant/configuration.yaml: while parsing a block mapping in “/home/homeassistant/.homeassistant/packages/battery.yaml”, line 207, column 9 expected <block end>, but found ‘<scalar>’ in “/home/homeassistant/.homeassistant/packages/battery.yaml”, line 211, column 47
I have this error:
Error loading /home/homeassistant/.homeassistant/configuration.yaml: while parsing a block mapping in “/home/homeassistant/.homeassistant/packages/battery.yaml”, line 207, column 9 expected <block end>, but found ‘<scalar>’ in “/home/homeassistant/.homeassistant/packages/battery.yaml”, line 211, column 47
The sensor.2 was autodiscovered. I use xiaomi aqara sensors and are connected via raspbee. Then there is deconz application that find the sensor. In this case is possible to rename the sensor?
Go the states page in the dev tools section, then click on the link icon on the left side of the sensor. In the window that pops up there might be a gear icon in the top right. If there is you should be able to click on that and in there rename the entity_id.