Using sensor template for history_stats sensor

I have an mqtt sensor in which state is the name of the bird that was last registered on my microphone. On the front end, I’d love to show the number of times that the species has occurred since today. I figured using history_stats is the best sensor, but the only thing I can’t figure out is if the state: parameter can be a template. This doesn’t result in another but a big goose egg of a result (pun heavily intended). As soon as I change the state to a static string (like “Black-capped Chickadee”) it works as expected.

sensor:
  - platform: history_stats
    name: Bird Sightings Number
    entity_id: sensor.bird_common_name
    state: "{{ states('sensor.bird_common_name') }}"
    type: count
    start: "{{ now().replace(hour=0, minute=0, second=0, microsecond=0) }}"
    end: "{{ now() }}"

I know templates don’t work everywhere, but I’d love to figure this one out. Any tips?

According to the documentation for History Stats sensor, the state option expects a string value, not a template.

That’s why it works when you enter a string, like Black-capped Chickadee, but not when you enter a template.

Yes, I figured as much given that the docs didn’t have the template link like start or end do, but I thought I read that all mqtt sensors come in as strings. I could also try to value_template the mqtt sensor to force it into a string.

But I may still face the issue with how history_stats are configured that it just won’t work unless it’s static?

There’s gotta be a workaround here, even if its adding another sensor and using an attribute or something.

It doesn’t matter how many other sensors you create, a History Stats sensor will not accept anything other than a string value for its state option. It would need to support a template in order to reference another entity’s value.

Right… alright. back to the drawing board.

If anyone else stumbles upon this and has ideas, I’m all ears. My next bet is to expand my appdaemon script to count and just dynamically create a species counter. Not my favorite as I was hoping for just a single counter entity, but better than nothing.

What’s the intended goal? To count the number of bird spotted in a single day, categorized by name? Anything else?

Yep, that’s it. Count the number of occurrences in a day and display that number. Super simple.

If it was super-simple, you would have been able to use a History Stats sensor.

What complicates the task is that the bird’s name comes from a sensor.

Tell me more about the behavior of sensor.bird_common_name. What’s the integration that creates this sensor and what determines its value?

Ha, I appreciate that. I mostly
meant simple in terms of the concept.

So the device running the project sends a single MQTT string and topic which is ingested and parsed by appdaemon. Appdaemon then sends out each value as a new sensor for HA. The values are all MQTT sensors right now, but I do have the same script creating a sensor by set_state() and adding attributes.

How many sensors? My impression was that there was only one sensor involved (sensor.bird_common_name).

Oh got it. 5 sensors. Common name, scientific name, time seen, date seen, confidence (%).

The science or common names are unique to the bird, the others are date, time, and percentage.

Have appdeamon dynamically create a sensor for each bird it encounters. On when it’s seen, off when it’s not. Then use history stats for that. Or simply make a rolling count w/ appdeamon in the attributes and reset it when you feel the need. You can also handle restoring states to make sure your counts persist using pickle or a json file.

Yeah, this is what I figured i would have to do.

On my dashboard, I have each of the sensors update with the most recent MQTT data - so it always shows the most recently recorded bird. I was trying to fit the counter into a single sensor because it slightly eludes me how I’d dynamically pull in the correct history sensor based on the name of the common bird.

The state of the sensor.bird_common_name is a string like “House Finch” where the appdaemon count sensor would likely be sensor.house_finch_count. So unless there’s something I’m missing, would I have to do something like this?

{% set common_name_sensor = ( states('sensor.bird_common_name').replace(" ", "_")) %}
return sensor.( {{ common_name_sensor }}.count )

I’ve never tried nested templating with jinja, so I’m shooting from the hip here. I’ll try to see if this works later on today.

EDIT: I forgot to add a | lower in there.