Count state change from sensor

I’m not talking about the time range on your history. I’m referring to the configured start and end. It only counts state changes between those times. If your state is Aus entering that range, it is not counted because the state change didn’t occur between those times.

Sorry i don’t get it. I posted the time range. IMHO the state changes are between those times.

Or what do you mean? Where do you see the error?

My use case what i would like to achieve is clear ?

In the ranges you posted, I only see 1 change. So I’d expect a count of 1.

This are two screenshots with two different ranges, but if you except one, then there is an issue ;), because it’s 0

I can’t tell if you understand what I’m saying with ranges. I’m talking about your start and end in your yaml configuration. Your original templates had it starting at midnight going until now. That’s the only one I would expect a count of 1. The other attempts where you changed the range do not fall into that. I can also tell you that it’s working perfectly fine from a code standpoint, so it’s something on your end. Not a bug.

Please keep in mind that the history range you view in the UI has nothing to do with the sensors value.

This is the only one that would have a value of 1 based on your graphs.

And keep in mind, those values are only for midnight today until the current time. As soon as midnight is hit, the count will drop to 0.

Jip but first it starts only at 1 also direct after midnight it also 1 and will never be increased by 1.
How you see the utility meter is always zero.

I would just count for 1 day the state change to 0.0 or Aus. So I see no error on my config

I’m not sure how history stats isn’t working for you. It’s still not clear to me if you understand how it works.

As for utility meter

Also keep in mind that your sensor may require a unit of measurement and device_class, which your sensor does not have.

I don’t understand what your suggestion is? So what should I use?

Me too. I thought I got it, but I don’t understand why it doesn’t work. Because of that, I’m here.
I shared everything I have. So the question is why it doesn’t work.

Current behavior is.
The first state change is no longer counted, only the second. And this state is completely constant.
Today is not working

Yesterday worked the second change:

Which one do you have in mind?

and your first image shows that it’s working exactly how I described.

I don’t think you understand what a state change is. It’s when the state changes.

It does not count state changes that have already occurred outside your timeframe.

So, your configuration is telling the system to count all state changes when it changes to AUS, which is 1.

This what you said is my understanding, but this is not the behavior.

Then I understand it right, but maybe you my issue not. The state is always be 1. Always also direct after midnight it goes never to zero.

See:

And why is the utility meter zero. Should also be 1, correct?

You can also see it if there is no state change it always be 1 and the first valid state change it‘s not counter also not from the utility meter

This is what I want :slight_smile:

Off course not. Therefore I have a daily,monthly and yearly utility meter, but same issue first hit is not counted

Are you sure you’re looking at the correct entity and not something old and stale left over from the many times you’ve reconfigured.

utility meter only works with numerical values, i.e. you need a unit of measurement

Yes but i can try a new one from scratch, but my only change was the end timestamp.

The value of the source is a numeric value as far I understand. However, I don’t know what to put as a measurement. I find here nothing?

I reinstalled the sensors.

Which I also find interesting. If I monitor a status change with “0.0”, then this is NOT incremented after a restart. With “Aus” it is counted up after each restart (see explanation above)

I still don’t understand why a utility meter needs a measurement. In the documentary are also all without.
And {{ is_number(states('sensor.wp_taktung_status'))}} is true

@petro look today it worked (except that the value of history stats always starts at 1)


Hi there,
I have a similar problem: I want to turn on something only when I turned on a switch for 2 times within short time: eg. comming from off-state: on-off-on within 5 seconds. Is there an easy way to check this in automotions?

I already tried this sensor in my configuration.yaml:

sensor:
  - name: "countstatechange"
    platform: history_stats
    unique_id: "countstatechange"
    entity_id: input_boolean.testschalter
    state: "on"
    type: count
    duration: "00:00:05"
    end: "{{ now() }}"

In my automation I test, if this value is above 1.
But the counter doesn’t count correctly. Very often it does not count my turn on. Sometimes it works, especially when I switch for much more then 2 times.

Is it mybe a timing problem or the now time isn’t updated so quickly?
Is there a different solution to check the number of state change directly in the automation?

By the way:
I set up also a template yaml file where i defined my sensors. I also tried to define this sensor there. But it does not work. Platform is not allowed there and many more errors. I understood, that we should use the new template sensors instead of the “old” sensors…Why does this not work in my template yaml file?

1 Like

Here is my workaround:
Trigger when turned on. Then wait for turn off, max. for 2 seconds, and stop automation if no turn off occurs. Then the same again for turn on. If passes the turn on and off then turn on the pump and wait until turn off, but max. for 20 minutes.

alias: Pool Jetpumps
description: ""
trigger:
  - platform: state
    entity_id:
      - input_boolean.testschalter
    from: "off"
    to: "on"
condition: []
action:
  - wait_template: "{{ states('input_boolean.testschalter') == 'off' }}"
    continue_on_timeout: false
    timeout: "2"
  - wait_template: "{{ states('input_boolean.testschalter') == 'on' }}"
    continue_on_timeout: false
    timeout:
      seconds: "2"
  - service: input_boolean.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: input_boolean.pooljetpumps
  - wait_template: "{{ states('input_boolean.testschalter') == 'off' }}"
    continue_on_timeout: true
    timeout: "00:20:00"
  - service: input_boolean.turn_off
    target:
      entity_id:
        - input_boolean.pooljetpumps
    data: {}
mode: single