Can't get HA to recognize custom sensor

So after watching several videos about templates I managed to get this far with creating
custom sensors for my phone.

i created a file called sensors.yaml

and added ‘sensor: !include sensors.yaml’ to my configuration

Here is the code that I came up with that i added to the sensors.yaml, which check up both with the editor in the development tools and Lint:


- platform: template
  sensors:
    battery_level_main_phone:
      friendly_name: Battery Level Main Phone
      value_template: "{{ states('sensor.sm_n960u_battery_level') }}"
    icon_template: >-
      {% set mainbattery = states('sensor.sm_n960u_battery_level') |
      int | round(0, 'floor') %} {% if mainbattery == 100 %}
        mdi:battery
      {% else %}
        {% set x = (((mainbattery/5) | int) +1) * 5 %}
        mdi:battery-{{x}}
      {% endif %}
    unit_of_measurement: "%"
- platform: template
  sensors:
    battery_level_work_phone:
      friendly_name: Battery Level Work Phone
  value_template: "{{states('sensor.sm_g781w_battery_level')}}"
  icon_template: >-
    {% set workbattery = states('sensor.sm_g781w_battery_level') |
    int | round(0, 'floor') %} {% if workbattery == 100 %}
      mdi:battery
    {% else %}
      {% set y = (((workbattery/5) | int) +1) * 5 %}
      mdi:battery-{{y}}
    {% endif %}
  unit_of_measurement: "%"

I don’t know if the extra variables are needed, but I got the correct values in the template editor.

so i did a reboot, and it doesn’t show my sensors in the list of entities when you go to states in the development tools

I’m obviously missing something obvious but for the life of me can’t figure out what. Is there something else I need to define?

If it matters, I’m using a virtual machine of home assistant under linux.

edit: after further searching the documentation i found the section that checks and reloads the yaml code in the development tools and got the following errors

Home Assistant or any integration from starting. It's also possible to only do the basic validation check without restarting.
Configuration will not prevent Home Assistant from starting!
Integration error: password - Integration 'password' not found.
Invalid config for 'sensor.template' at sensors.yaml, line 15: expected dictionary for dictionary value 'sensors->unit_of_measurement', got '%'
Invalid config for 'sensor.template' at sensors.yaml, line 5: required key 'value_template' not provided
Invalid config for 'sensor.template' at sensors.yaml, line 7: expected dictionary for dictionary value 'sensors->value_template', got "{{states('sensor.sm_g781w_battery_level')}} {% set workbattery = states('sensor.sm_g781w_battery_level') | int | round(0, 'floor') %} {% if workbattery == 100 %}\n  mdi:battery\n{% else %}\n  mdi:battery-{{workbattery}}\n{% endif %}"
Invalid config for 'sensor.template' at sensors.yaml, line 25: expected dictionary for dictionary value 'sensors->icon_template', got '{% set mainbattery = states(\'sensor.sm_n960u_battery_level\') | int | round(0, \'floor\') %} {% if mainbattery == 100 %}\n  mdi:battery\n{% else %}\n  mdi:battery-{{mainbattery}}\n{% endif %} unit_of_measurement: "%"' 

Does this mean I’ve messed up my spacing somehow?

Your indentation is not correct. Should be:

- platform: template
  sensors:
    battery_level_main_phone:
      friendly_name: Battery Level Main Phone
      value_template: "{{ states('sensor.sm_n960u_battery_level') }}"
      icon_template: >
        {% set mainbattery = states('sensor.sm_n960u_battery_level') | int(0) | round(0, 'floor') %} 
        {% if mainbattery == 100 %}
          mdi:battery
        {% else %}
          {% set x = (mainbattery/5 +1) * 5 %}
          mdi:battery-{{x}}
        {% endif %}
      unit_of_measurement: "%"
- platform: template
  sensors:
    battery_level_work_phone:
      friendly_name: Battery Level Work Phone
      value_template: "{{states('sensor.sm_g781w_battery_level')}}"
      icon_template: >
        {% set workbattery = states('sensor.sm_g781w_battery_level') | int(0) | round(0, 'floor') %} 
        {% if workbattery == 100 %}
          mdi:battery
        {% else %}
          {% set y = (workbattery/5 +1) * 5 %}
          mdi:battery-{{y}}
        {% endif %}
      unit_of_measurement: "%"

Note, you should be using the new template sensor format for new sensors.
e.g. (configuration.yaml)

template:
  - sensor:
      - name: Battery Level Main Phone
        state: "{{ states('sensor.sm_n960u_battery_level') }}"
        unit_of_measurement: "%"
        icon: >
          {% set mainbattery = states('sensor.sm_n960u_battery_level')|int(0) | round(0, 'floor') %}
          {% if mainbattery == 100 %}
            mdi:battery
          {% else %}
            {% set x = (mainbattery/5 +1) * 5 %}
            mdi:battery-{{x}}
          {% endif %}

      - name: Battery Level Work Phone
        state: "{{states('sensor.sm_g781w_battery_level')}}"
        unit_of_measurement: "%"
        icon: >
          {% set workbattery = states('sensor.sm_g781w_battery_level')|int(0) | round(0, 'floor') %} {% if workbattery == 100 %}
            mdi:battery
          {% else %}
            {% set y = (workbattery/5 +1) * 5 %}
            mdi:battery-{{y}}
          {% endif %}

I also removed a lot of extraneous parentheses and int filters.

You can also simplify it considerably to this for a changing battery icon:

template:
  - sensor:
      - name: Battery Level Main Phone
        state: "{{ states('sensor.sm_n960u_battery_level') }}"
        unit_of_measurement: "%"
        device_class: battery

      - name: Battery Level Work Phone
        state: "{{states('sensor.sm_g781w_battery_level')}}"
        unit_of_measurement: "%"
        device_class: battery
1 Like

This passes restart when put into the configuration.yaml, but not in a separate configuration file? Why is that?

Also this does not allow for multiple battery icons, which is what the original code was meant to do, although I admit it still wouldn’t have worked perfectly, because there are only battery icons for percentage mulitples of 10. if I was using the original corrected code it would be:

{% set x = (mainbattery/5 -1) * 10 %}

etc

I got this to work:

template:
  - sensor:
      - name: Battery Level Main Phone
        state: "{{ states('sensor.sm_n960u_battery_level') }}"
        unit_of_measurement: "%"
        icon: mdi:battery-{{states('sensor.sm_n960u_battery_level')}}
        device_class: battery

      - name: Battery Level Work Phone
        state: "{{states('sensor.sm_g781w_battery_level')}}"
        unit_of_measurement: "%"
        icon: mdi:battery-{{states('sensor.sm_g78w_battery_level')}}
        device_class: battery

Which of course returns an ‘unkown’ value if the percentage is not a multiple of 10… Guess I’ll have to figure out how to stick the equation back in. Thanks

You don’t need the icon template. The battery device class includes a changing icon.

Hmmm I’m obviously using the entity cards for dashboards wrong then…

If i go to the state page in the developer tools, the original sensor has an icon class.

But if I type in the name of the sensor…I created, nothing… Maybe I’ll just see if i can find a way to rename the original sensor…

when I use the template mushroom card I wanted to use,

{{state_attr('sensor','icon')}}

doesn’t work


This should include an icon that changes with battery state:

template:
  - sensor:
      - name: Battery Level Main Phone
        state: "{{ states('sensor.sm_n960u_battery_level') }}"
        unit_of_measurement: "%"
        device_class: battery

      - name: Battery Level Work Phone
        state: "{{states('sensor.sm_g781w_battery_level')}}"
        unit_of_measurement: "%"
        device_class: battery

BUT, note you don’t need to duplicate the state like this. You can add the device class using customize.

From developer tools → states, what are the attributes of these sensors?

sensor.sm_g781w_battery_level
sensor.sm_n960u_battery_level
unit_of_measurement: "%"
device_class: battery
icon: mdi:battery-90
friendly_name: SM-G781W Battery level

and

unit_of_measurement: "%"
device_class: battery
icon: mdi:battery-30
friendly_name: SM-N960U Battery level

Which is NOT what the custom sensors show… But when I started using home assistant, I didn’t know about the battery device class so is there a way I can just change the friendly name of the original sensor so I can remember it better?

Yes. Click on the entity in Devices & Services → Integrations or in your dashboard and click the cog icon at the top of the pop up window. You can edit the name there.

1 Like