#Challenge of the day

1 messages · Page 1 of 1 (latest)

proper hinge
#

I can't get my brains around this, I'm not that strong in templating yet, nor defining custom/virtual sensors. If anyone wants to take it as a challenge, here is what I'm looking for:

A virtual sensor that can count all events from all my sensors and devices, and calculate a minute average, so that I can both see at which times of day my house is the most chatty, and also to serve as a fun tile on my dashboard

safe wraith
#
template:
  - trigger:
     - platform: event
       event_type: "*"
    sensor:
      - name: "events fired"
        state: "{{ (states('sensor.events_fired')| int(0)) + (0 if trigger.event.data.entity_id == 'sensor.events_fired' else 1) }}"
        unit_of_measurement: "1"```
#

That will give you an increasing number

proper hinge
#

Ooh, excitement!

#

So when you only provide a name for a sensor, the id becomes that name in lowercase with underscores? I have a few million events already, I think it counts itself

safe wraith
#

Yes, that's how the name works. And you need to update the template so it filters itself out.

proper hinge
#

It is still decimating the performance of HASS currently. I did a straight copy/paste

#
template:
  - trigger:
     - platform: event
       event_type: "*"
       filters:
        - not:
          trigger.event.data.entity_id: 'sensor.events_fired'

    sensor: 
      - name: "events fired"        
        state: "{{ (states('sensor.events_fired')| int(0)) + (0 if trigger.event.data.entity_id == 'sensor.events_fired' else 1) }}"
        unit_of_measurement: "1"   

Something like this?

#

Yikes, it is dead, we killed HASS!

safe wraith
#

I don't think filters exist on event triggers. It's not in the documentation.

proper hinge
#

aah, it's on the template itself?

safe wraith
#

That's what the (0 if trigger.event.data.entity_id == 'sensor.events_fired' else 1) part does

#

So make sure that's your actual entity id

proper hinge
#

I believe that didn't work then, it quickly spun up to millions of events, and made HASS completely unresponsive

#

ok, forget that it DID work, what created the millions of events was when I added a Riemann helper to group those counts into minutes. Hmm.. Isn't there a filter that I can add to reset the counter every minute, i.e. a sliding window?

safe wraith
#

state: "{{ (states('sensor.events_fired')| int(0)) + (0 if trigger.event.data.entity_id in ['sensor.events_fired', 'sensor.your_riemann_sensor', 'sensor.anything_else_you_do_not_want_to_count'] else 1) }}"

proper hinge
#

Aah!

safe wraith
#

That could work

proper hinge
#

It DOES work, however, one remaining thing:
The counter only increases, so the riemann average only goes up. Ideally, I'd reset the count every minute..

safe wraith
#

Maybe you could try the utility meter instead?

proper hinge
#

Isn't that for power only?

#

I'm not a template guru, so I am unsure on how to write this but:
"every time seconds MOD 59 == 0" - set counter to 0
else
-- your expression --