Making a binary attribute available as a word

I have a FordPass integration that returns an attribute of a 0 if unplugged and 1 if plugged in. I want it to return Unplugged/Plugged In. I have extracted the “Plug Status” attribute with the below in my /config/configuration.yaml:

      - name: Eva - Plug Status
          state: "{{ state_attr('sensor.fordpass_elveh', 'Plug Status')  }}"'

That works, creating ‘sensor.eva_plug_status’. Now I want to go the next step and make the 0 return “Unplugged” and a 1 “Plugged in”.

I added the below to my configuration.yaml. The template below produces the desired return in the developer tools Template Editor.

'      - name: Eva - Plugged in to EVSE
        device_class: connected
        value_template: >-
            {%- if is_state("sensor.eva_plug_status", "on") -%}
            Unplugged
            {%- else -%}
            Plugged in
            {%- endif -%}'

That generates the error below:

Logger: homeassistant.config
Source: config.py:820
First occurred: 16:30:51 (1 occurrences)
Last logged: 16:30:51

Invalid config for [template]: expected SensorDeviceClass or one of ‘apparent_power’, ‘aqi’, ‘battery’, ‘carbon_monoxide’, ‘carbon_dioxide’, ‘current’, ‘date’, ‘duration’, ‘energy’, ‘frequency’, ‘gas’, ‘humidity’, ‘illuminance’, ‘monetary’, ‘nitrogen_dioxide’, ‘nitrogen_monoxide’, ‘nitrous_oxide’, ‘ozone’, ‘pm1’, ‘pm10’, ‘pm25’, ‘power_factor’, ‘power’, ‘pressure’, ‘reactive_power’, ‘signal_strength’, ‘sulphur_dioxide’, ‘temperature’, ‘timestamp’, ‘volatile_organic_compounds’, ‘voltage’ for dictionary value @ data[‘sensor’][3][‘device_class’]. Got ‘connected’ extra keys not allowed @ data[‘sensor’][3][‘value_template’]. Got ‘{%- if is_state(“sensor.eva_plug_status”, “on”) -%} Unplugged {%- else -%} Plugged in {%- endif -%}’ required key not provided @ data[‘sensor’][3][‘state’]. Got None. (See /config/configuration.yaml, line 34).

What am I doing wrong?

Look to me that you have lots of ‘-‘ that should not be there, can you try and format that code correctly as it’s difficult to read like this.

as mentioned you need to properly post your code properly using three back ticks (```) on the line above and below the code snippets.

but the error kind of tells you one error:

‘connected’ isn’t in that list.

try removing that whole line and see where you end up.

next why are you using ‘on’ and ‘off’ in the template when you said the value of the attribute is 0 and 1?

if that doesn’t fix things then come back and (properly) post the current non-working code.

The use of {%- and -%} does not make sense in a configuration file.
These are instead used in lovelace cards to prevent the lines from generating a line break.

Thanks for the response - Still pulls an error with the line removed, but it is a different error:
Logger: homeassistant.config
Source: config.py:820
First occurred: 20:54:27 (1 occurrences)
Last logged: 20:54:27

Invalid config for [template]: [value_template] is an invalid option for [template]. Check: template->sensor->3->value_template. (See /config/configuration.yaml, line 34).

As to why I am using ‘on’ and 'off" - because it returns a working response in the Developer Tools Template Editor.

Now let’s see if I can (properly) post the non-working code:

     - name: Eva - Plugged in to EVSE
        value_template: >-
            {%- if is_state("sensor.eva_plug_status", "on") -%}
            Unplugged
            {%- else -%}
            Plugged in
            {%- endif -%}

Thanks Wally - doing this on a Lovelace card would work, but I don’t have that figured out, yet.

But for now the {%- and -%} are there because it doesn’t work in the Template Editor without them.

are you using the new style template sensor or the legacy?

if you are using the new style the line shouldn’t be “value_template:” but instead “state:”

if using the legacy style then you don’t use “name:”.

you need to look at the template sensor docs and make sure all of your config aligns completely with one or the other style.

Thanks for the dialogue - it really helped:

Working code:

      - name: Eva - Plug Status
        state: "{{ state_attr('sensor.fordpass_elveh', 'Plug Status')  }}"
      - name: Eva - Plugged in to EVSE
        state: >-
            {%- if is_state("sensor.eva_plug_status", "on") -%}
            Unplugged
            {%- else -%}
            Plugged in
            {%- endif -%}
1 Like