Logging a number's changes

Consider some numerical entity which is increasing:

изображение

I may create a sensor to keep changes - like this simple one:

  - trigger:
      - platform: state
        entity_id: ...some entity...
        to:

    sensor:
      - name: ...
        state: >-
          {{ trigger.to_state.state - trigger.from_state.state }}

(for simplicity: assume that the entity is never unavailable or unknown).

Now I need to see a “log” keeping these changes - like this:

...
17.12.23 19:40: 120
17.12.23 19:30: 1
17.12.23 19:27: 14
...

where “120”, “14” etc - a value of a change.
i.e. the log must have all changes of the monitored entity (may be only for a defined period).

Since Logbook does not support numerical entities, a possible way is creating a template sensor which keeps obvious STRINGS like “delta: 123” instead of “123”.
Then I may use a Logbook card for this sensor, this will be like:

...
17.12.23 19:40: delta:120
17.12.23 19:30: delta:1
17.12.23 19:27: delta:14
...

But if the monitored entity have 2 or more SAME consecutive changes - only the 1st change is stored.
A possible workaround is to add a timestamp to a state which makes it too cumbersome.

But probably I am reinventing a wheel?
May be there are better ways to create such a log?

If you don’t care about showing the data in the dashboard (but I believe you can do that too although I’ve never done it) you can create a text file and have HA write the data to that file.

Here is an example I have that will keep a record of the states of some weather entities I want to know what the possible different states could be so I can use them in my automations.

First set up the notify integration for the file that you want to write to:

notify:
  - platform: file
    name: ENV Weather Condition Data Record
    filename: weather_condition_data.txt
    timestamp: true

then create the automation to write the data to the file:

automation:
  - alias: ENV Weather Condition Data Record
    id: env_weather_condition_data_record
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: 
        - sensor.pirateweather_icon
        - weather.kgwb_hourly
        - weather.openweathermap
        - weather.pirateweather
    action:
      - service: notify.env_weather_condition_data_record
        data:
          message: >
            pirateweather icon: {{ states('sensor.pirateweather_icon' ) }}, 
            kgwb: {{ states('weather.kgwb_hourly') }}, 
            openweathermap: {{ states('weather.openweathermap') }}, 
            pirateweather:  {{ states('weather.pirateweather') }}

you can add pretty much any data you want and format it how you want.

Very interesting problem, I’m going to give it more though, but one hack comes to mind:

You can perturb the current change by adding a small but insignificant value to it (depending on how you’ll be using it).

For example, add a very tiny random value to it. This will make at least consecutive values unique (but odds are that there might still be a numeric collision, but small, depending on the choice of randomness). For the most part, this tiny value would be unnoticeable, except when viewing raw value, but in many cases you’d be able to round the value to get rid of it.

(My idea comes from certain algorithms in machine learning where you do this kind of thing to avoid an algorithm getting stuck.)

Interesting way, never tried files I/O operations in HA.
Will need to find out how to show a content in UI.

I wonder is it possible to create a Google sheet & then show it in UI?

A similar idea came to my mind.
Assume we have this state:

state: > -
  ...
  delta: {{ VALUE }}

I may probably use different letters - Latin “a” & Cyrillic “a” for consecutive SAME values.
Also, if states are composed as “+ 123” - then we can probably also use different unicode symbols which are displayed similarly. The 1st thought is about a whitespace (I could be wrong): I think there are different codes for it.
Also (just a guess, could be wrong) - if states are like “123.456” (i.e. with a delimeter) - may be there are also different chars for it?

Here is a way to compose different but similarly looking states:

            {%- set COLON_1 = '\u003a' -%}
            {%- set COLON_2 = '\u003a\u0020' -%}
            {%- if states(this.entity_id) is search(COLON_2 + " ") -%}
              {%- set COLON = COLON_1 -%}
            {%- else -%}
              {%- set COLON = COLON_2 -%}
            {%- endif -%}
            {{LABEL}}{{COLON}} {{DELTA}}

where
DELTA - some numerical value (“change”)
LABEL - any prefix (could be “income”, “outcome”)
COLON_xxx - “colon” symbol with a possible whitespace.

This results:

изображение

where states look same - but in fact they are different:

Good, that’s the kind of thing I had in mind. Super hacky, but works. I originally assumed single numerical values — not labels/text too.

BTW, I think your timestamp idea might be similar to this: https://community.home-assistant.io/t/trigger-automation-when-sensor-is-updated-even-to-the-same-value/48390. It seems a little b8t cleaner.

Thanks for a link, will read it.

Gonna check a custom:logbook-card - if it can be used to show values “123.0001” & “123” as same “123”.

1 Like