Help with value_template in sensor please

I have the below code in my sensors.yaml:

  - 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

You’re doing alot of things wrong here.

  1. {% %} 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 {{.
  2. 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.
  3. 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.
  4. 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 %}
1 Like

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 :stuck_out_tongue: )!

Again thank’s for your very fast help and feedback :slight_smile:

1 Like