Visualization of entity attributes


#1

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.Schermata%20del%202019-01-03%2022-39-01

Any idea to solve this issue?

Thanks

  - 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

Template isn't working with "bracket" notation
#2

I think it’s because your entity_id starts with a number. The docs say you need to use brackets to render it properly. See here about half way down:

So, try like this:

value_template: >
  {% if states.sensor['2'].attributes.battery_level %} 
    {{ states.sensor['2'].attributes.battery_level}}
  {% else %} 
    Unknown 
  {% endif %}

#3

I try

value_template: >
  {% if states.sensor['2'].attributes.battery_level %} 
    {{ states.sensor['2'].attributes.battery_level}}
  {% else %} 
    Unknown 
  {% endif %}

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.

Any other solution?

Thank you


#4

I just checked the syntax on one of my devices that has a battery level and it works fine in the template editor.

Can you copy the template into the template editor and see what the result is, please?

It may be something else around that code that is causing the error.


#5

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

what’s wrong?


#6

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 %}'


#7

Actually, I just created a real template sensor with the above formatted code using one of my devices and it doesn’t work. :thinking:

- platform: template
  sensors:
    bat_te_1:
      friendly_name: "BATTERIA TEMPERATURA 1"
      unit_of_measurement: '%'
      value_template: '{{ states.zwave["door_window_sensor_1"].attributes.battery_level}}'
      # value_template: >
        # {% if states.zwave["door_window_sensor_1"].attributes.battery_level %} 
          # {{ states.zwave["door_window_sensor_1"].attributes.battery_level}}
        # {% else %} 
          # Unknown 
        # {% endif %}

If I put the template in the editor it works fine. If i use it in a real sensor it always shows as “unknown”.

I’m going to play around with this some more and try to figure out why it doesn’t work. If I fugure anything else out I’ll post back with my results.


#8

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?


#9

This is the entire code of battery package (the version with sensor that begin with number without brackets):

#################################################################

##                      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()}}

#10

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.


#11

Sorry, but the result is always the same. If I use your code the configuration is correct but in sensor.bat_te_2 is showed unknow

If I use this parts in sensor.bat_te_2

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 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


#12

I know. that’s why I created the other thread. because it’s not working correctly.

I know. that’s why addressed that code (that you already tried) in post #6 above.

Which is why I said this in response:

It will still show as “unknown” until we get the “bug” worked out but it will at least remove all of your errors.

Maybe it will be easier to just move on past trying to get your existing sensor.2 working and just rename the sensor to make it easier.

How did that sensor.2 get created? Did you manually enter the information in your config or did it get autodiscovered from some component/device?


#13

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?


#14

It might be possible.

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.


#15

Works fine!
Thank you for all your patience.


#16

That’s great!

I still want to know why the other way wouldn’t work.

According to all the docs I read it should have worked.