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?