How to count the number of state changes of a sensor for "today"?

I have quite a lot of experience in “traditional” programming, but just started learning the setup for ha and there are too many questions, I’m sure my question is “noob”:

I have a sensor (binary_sensor.m_zaporizhzhia_ta_zaporizka_teritorialna_gromada_air) - this is an air raid sensor, we are regularly shelled and air raid alerts are announced several times a day

I’m making a card in lovelace, where I display various states (time / weather / alarm, etc) and I want to count how many times an alarm has already been announced today (if anyone is interested, today is already 6 times and it’s not evening yet), but I don’t understand yet how to approach this task

actually i need to get a list of sensor values, filter only “on” states, filter by date “today” and get the number of items in the list

sorry for my english & noob question, tia

I’m really sorry for your situation. That would be a terrible way to exist day to day.

But to your request…

use a history stats sensor.

I think this should get you what you want:

sensor:
  - platform: history_stats
    name: Air Raid Alerts Today
    entity_id: binary_sensor.m_zaporizhzhia_ta_zaporizka_teritorialna_gromada_air
    state: 'on'
    type: count
    start: '{{ now().replace(hour=0, minute=0, second=0) }}'
    end: '{{ now() }}'

Or

start: '{{ today_at() }}'

more modern.

2 Likes

guys this is cool, thnks!!!

now i have history stat + visual sign (diy-wled-based lamp with “red” palette if air raid alert)
hope these automations will become unnecessary soon…

air_on|527x499