Assign area to sensor in configuration.yaml

I’m working on creating a sensor that will show me how long a fan has ran, and let me reset the hour meter when I replace the filter.

I am using this conifg:

    #History:
  - platform: history_stats
    name: Filter Fan Hour Meter
    entity_id: switch.filter_fan_switch
    room: "Living Room"
    state: "off"
    type: time
    start: "{{ now().replace(year=2020) }}"
    end: "{{ now() }}"

My issue right now, is that the sensor appears but can not be assigned to any area. Normally I would use the UI, but I get this message:

This entity (‘sensor.filter_fan_hour_meter’) does not have a unique ID, therefore its settings cannot be managed from the UI.

My understanding is that means I have to use the yaml to set it up, but I have been unable to find out how to assign an area to his sensor.

Is there a way for me to do that?

1 Like

Add a unique_id…

  - platform: history_stats
    name: Filter Fan Hour Meter
    unique_id: filter_fan_hour_meter_01
    entity_id: switch.filter_fan_switch
    room: "Living Room"
    state: "off"
    type: time
    start: "{{ now().replace(year=2020) }}"
    end: "{{ now() }}"

Then you will be able to assign an area in the UI.

Awesome, thank you. That does it. Is this something I may have messed up necessitating it, or if I create a sensor in yaml do I always need to assign it a unique ID?

unique_id is an optional configuration key for many YAML-configured sensors. But it’s not even an available optional for all sensor integrations. You haven’t messed anything up. It’s only necessary if you want to manage the entity from the UI.