Custom counter component

Hi guys,

I was looking for a simple way to implement a counter sensor, which I could increment based on an event, and reset every day. My use case is my motion sensor at home, when it senses motion, it saves the motion event picture to my Plex library and now I wanted to have a counter so I could so how many motion events got triggered (so later on I can build automations on it, since everybody leaves for work between certain hours, it would be ok to have that many motion events during the timeframe etc). This counter should get reset at midnight every day. I couldn’t find any direct solution, without using any other components (like the statistics one), so I created my own, and I wanted to share this with the community, as I’m sure other will be able to use this as well.

What this does:
Basically it registers 2 services in your HA instance, counter.increment and counter.reset.
Calling the first one, increments the counter.simple entity by 1.
Calling the second one, obviously resets the counter.simple entity.
Currently it’s not configurable at all, and if you want to change the entity name, you’ll have to modify the code itself, I might add this later on.

The code:

def setup(hass, config):
    """Setup the sensor platform."""
    hass.states.set('counter.simple', 0)

    def handle_increment(call):
        state = hass.states.get('counter.simple')
        new_state = int(state.state) + 1
        hass.states.set('counter.simple', new_state)

    def handle_reset(call):
        hass.states.set('counter.simple', 0)

    hass.services.register('counter', 'increment', handle_increment)
    hass.services.register('counter', 'reset', handle_reset)
    return True

Place this inside the counter.py file inside your custom_components directory, enable it by adding counter: to your config, and you’re done.

as you can see, this is really simplistic, and probably error prone :stuck_out_tongue:
But now, I can let my Rpi with my motion camera fire a custom event in my HA, and act on that to trigger the increment service.

3 Likes

I’ve modified the code, and instead of being a separate component, it is now used as a sensor. This makes it show up as a badge in the front end by default. I’ve also added a decrement service (the calls are now done by using sensor.XXX-counter)
The code can be found here: Counter

I’m planning to add some extra config options this weekend, like a start value, and step sizes.
If all goes well, I might put out a PR later next week.

1 Like

Hello,

This looks cool as I was looking for a way to carry out a counter in my system, there by creating an alarm or something.

Based on what both of you wrote, can I assume the below?

  1. I copy the upgraded one by @tmatheussen from Github
  2. Put it in a file could “counter.py” and place it among where the other files are in my system
  3. I can access it as a sensor by simple adding “counter:” under sensor with a “friendly name”? Is this possible?
  4. If no friendly name or alias, how can I possible have multiple of this in my code?
  5. I can increment or decrement the value by simply calling “sensor.increment-counter”? I am assuming here there is no friendly name
  6. As stated, can I now have an initial value or something?
  7. if no initial value, does it mean to use the decrement function, I have to like count up a couple of times or something?

Your responses will be very much appreciated pls.

regards

The file should be placed in the normal location for custom components (which is in you configuration directory:
custom_components/sensor/counter.py)
Currently it only supports having 1 counter, meaning calling increment_counter or decrement_counter will update the 1 counter, I haven’t really tried with 2 or more yet.
Initial value can be set like this:

platform: counter
  initial: 10

In the same style, you can set the step sizes using the step attribute.
As for the decrement function, I think it’s possible for the sensor to go negative, I only used it for positive counting for now

Many thanks for the quick reply.

Kind regards

Hello @tmatheussen,

Please another quick one.

Is it possible to set the value of the initial counter value using a template? As I have a slider, and I want to use the value inputed by a user as the initial, then start counting down to run a function at the end.

I am sorry if the question is basic, as I am one of the newbies :grin:

Regards

No problem :wink:

I’m not sure at which moment HA processes the templates, but I’m not entirely sure this would be possible.
You could try to specify the template in the attribute option, but I’m guessing it will complain that it’s not an int value.

What could be done, is add another service, which sets the value for the counter to a specific value, I actually thought about that at one point.

Currently I can’t access my code (on a broken down laptop), but I hope I get some time in the following weeks to get back in the HA game, and finish some of my projects, like this one :smiley:

Thanks for writing this, I just got it up and running with an Amazon Dash button for counting trips to Home Depot on my construction project.

Is there anyway to make a counter that doesn’t reset when Home Assistant restarts?

Maybe this is something for you.

Cool thanks,

I ended up looking deeper into the google sheets integration with a custom script i found on the forums. now i’ve got a running total and a graph of the trips

http://goo.gl/63AnVJ