History stats sensors not appearing

I’m trying to get a history stats sensor on how many hours per day the TV was on. I have everything set up in configuration.yaml like this:

sensor:
  - platform: history_stats
    name: "TV On Today"
    entity_id: media_player.living_room_tv
    state: "on"
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"

However, even after multiple restarts no new sensors appear. I feel like I’m doing something wrong in the config but I can’t figure it out.

Where did you look for it?

Developer Tools → States

Or

Settings → Devices & Services → Entities tab

Also, does your media player ever have the state ‘on’?

Or is it ‘playing’ instead?

Check dev tools states.

I checked both Developer Tools and Entities. There’s no sensor visible in either. It does have the state “on”. I’m using it in a bunch of automations as well. I tried the same with a light for good measure with the same result.

Errors in your log?

A bunch :smiley: . But none related to hisrory_stats or sensors.

Is the media player excluded from your recorder?

I don’t exclude anything, which I probably should.

I’m out of ideas then.

:man_shrugging:

Me too :smiley: . Thanks anyway! Really appreciate the help!

Do you have recorder: or default_config: in your configuration.yaml file?

I have default_config:, yes. Could that be messing it up?

No, that is required. If you have that then it is not the problem.

Where are you putting your sensor configuration?

If you are putting this in sensors.yaml remove the first line sensor: and move everything left two spaces. So just this:

- platform: history_stats
  name: "TV On Today"
  entity_id: media_player.living_room_tv
  state: "on"
  type: time
  start: "{{ now().replace(hour=0, minute=0, second=0) }}"
  end: "{{ now() }}"

Thanks! It’s directly in configuration.yaml.

Ok, so does sensor: only appear once in your configuration.yaml file?

Oh man… I feel so stupid :man_facepalming:. I had another sensor: at the bottom. Fixed it and everything appears! Thank you so much for your help and persistence!

1 Like

Hello, I have the same problem, can I please ask your help?
I’ve added the following to my configuration.yaml file:

# next lines are for Dyson plug history stats
sensor:
  - platform: history_stats
    name: "Dyson charge time"
    entity_id: switch.plug_dyson_lumi_lumi_plug_maeu01_schakelaar
    state: "on"
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"

But I cannot get this sensor to appear in dev tools > States or as trigger in an automation.
I don’t have any other sensor: in my configuration.yaml, but I do have:

template:
  - trigger:
      platform: event
      event_type: tasker_event
    sensor:
      - name: Tasker State
        state: "{{trigger.event.data.state}}"

and

binary_sensor:
  - platform: hikvision
    host: 192.168.2.82
    username: admin
    password:
    name: HikvisionDS2CD2

in there. Can they be the problem?
The switch.plug_dyson_lumi_lumi_plug_maeu01_schakelaar definitely goes to state on, when I switch it.

It is better to start a new topic than dig up old solved ones.

How many times does this appear in your configuration.yaml file?

sensor:

What errors do you see in Settings → System → Logs?

Every mention of the word sensor in my configuration.yaml file is in the three blocks in my previous post. I decided to remove the second and third block, letting the first block be the only mention of sensor. Then the sensor finally appeared in dev tools > status. I don’t know why.

I then pasted blocks two and three back to the exact same place where they were before and reloaded the yaml. It kept working since, which is even weirder.

Did you restart or reload the first time?

Every time you add a new integration (e.g. template:) you need to restart. After that you can reload when adding to the integration.

1 Like