"options" for sensor device_class: enum

I’m trying to set up a sensor with a couple of strings as available states. The documentation says that I can use device_class: enum to do this, using options to define the possible states:

SensorDeviceClass.ENUM: The sensor has a limited set of (non-numeric) states. The options property must be set to a list of possible states when using this device class.

The problem I have is that I’m not sure how to specify the options. My sensor currently looks like this:

  - sensor:
      - name: Our House Consumption W or kW
        unique_id: our_house_consumption_w_or_kw
        state: >
          {% if states('sensor.myenergi_our_house_home_consumption')|float(1) < 1000 %}
            "W"
          {% else %}
            "kW"
          {% endif %}
        device_class: enum

If I put options at the same level as device_class then Studio Code Server tells me Property options is not allowed. But if I try to indent it from device_class then it sees it as continuation of enum and complains that I have to choose a correct device_class.

If anyone’s able to give me an example of how to use enum to define a list of possible states for a sensor, please, that would be great. I haven’t been able to find anything on this forum, or more widely.

10 Likes

Yeah, is super confusing.

'options' is an invalid option for 'template'

Options is an attribute. Example:

template:
  - sensor:
    - unique_id: thisorthat
      name: "This or That"
      device_class: enum
      state: "This"
      attributes:
        options: "{{ ['This', 'That'] }}"

1 Like

Unfortunately, this does NOT work with RESTful sensor. This is an example what I’m trying to achieve but the configuration is invalid (‘attributes’ is an invalid option for ‘rest’, check: rest->0->sensor->2->attributes). Any solution to this? Thank you.

rest:
  - authentication: basic
    scan_interval: 60
    resource: https://...
    sensor:
      - name: "EcoVolter Charging Status"
        unique_id: sensor.ecovolter.charging_status
        value_template: '{{ value_json.chargingStatus }}'
        device_class: enum
        attributes:
          options: "{{ ['INIT', 'IDLE', 'CONNECTED', 'CHARGING', 'CHARGING_WITH_VENTILATION', 'CONTROL_PILOT_ERROR', 'ERROR'] }}"