- platform: template
sensors:
steven_phone_charging:
value_template: "{{ state_attr('sensor.sm_g965f_battery_level', 'is_charging') }}"
friendly_name: Steven Phone Charging
icon_template: mdi:battery-charging
- platform: template
sensors:
steven_phone_charging_status:
value_template: >-
{% set plugged = states('{{ state_attr('sensor.sm_g965f_battery_level', 'is_charging') }}') }
{% set charge = states('state.sensor.sm_g965f_battery_level) %}
{% if plugged true and charge 100 %}
Charged
{% elif plugged true and charge < 100 %}
Charging
{% else %}
Unplugged
{% endif %}
friendly_name: Steven Phone Charging Actual
icon_template: mdi:battery-charging
The first one returns if the phone is plugged in or not and gives either âtrueâ if plugged in or âfalseâ if not - this works fine.
My issue is with the 2nd sersor where Iâm looknig to break this down using the 2nd sensor to check:
if plugged in and battery = 100 then give output of Charged
if plugged in and battery < 100 then give output of Charging
otherwise give output of Unplugged
Iâm getting invalid config for the 2nd sensor and looking for help with this as Iâm brand new to this templating joy
{% %} and {{ }} indicate a line of code. {% %} indicates a line of code that doesnât return a string. {{ }} indicates a line of code that does return a string. You cannot have {{ }} inside {% %} or vice versa. Also, every time you use a {% you need to have a closing %}. Same goes for {{.
Your quoting was all over the place. Quotes denote strings. if you put code inside quotes inside {{ }} or {% %} youâre making a string, not actual code.
Youâre passing functions inside functions. This is ok, but youâre doing it wrong. It seems you donât understand when to use these things and youâre copying and pasting from the forum and hoping it works. Youâll have constant trouble with this if you continue down this path. Itâs better to learn how to code, it will save you time.
Your if statements donât have tests in them. you have âif plugged trueâ. Thereâs no test there. Itâs either âif plugged is Trueâ, âif pluggedâ, or 'if plugged == True. Youâd learn these nuances by doing a beginner python tutorial course online, thereâs 100s of free ones out there.
Hereâs what the code youâre after looks like:
{% set plugged = state_attr('sensor.sm_g965f_battery_level', 'is_charging') %}
{% set charge = states('sensor.sm_g965f_battery_level') | int %}
{% if plugged and charge == 100 %}
Charged
{% elif plugged and charge < 100 %}
Charging
{% else %}
Unplugged
{% endif %}
Thanks Petro, that is exactly what I was looking for - and sadly I was closer to that on a few revisions but had missed the " | int" and == operators as well as the quotation/nesting(1) problems.
For (3) You are indeed correct - I had copied something similar and tweaked what I thought would do what I needed.
For (4) I will look into learning more on this as Iâve never touched python before so itâs all new to me.- sadly I did get the right 95% of the rightcode/layout across all my revisions but never all of them at the same time(yes Iâm aware that 95% isnt good enough )!
Again thankâs for your very fast help and feedback