Increasing the accuracy of the History_Stats integration output data

In general, it is very strange and not logical to expect the output in hours when specifying the time period in seconds (or accuracy to seconds).

Look at this line in the history_stats source code.

Why round the result before output?

Is it impossible to round the value in the jinja template for the accuracy you need?

This is completely absurd!

If you give a history_stats entity a unique_id, then it will have full, unrounded precision (and you may change the display precision in the entity settings).

I guess this rounding is just to preserve backwards compatible output for entities which cannot set a display precision.

1 Like

Which parameter in the entity settings is used to set the accuracy?

My task looks too simple: I need to calculate how many total seconds an entity worked in a specified period of time.

History_stats does exactly what I need in the code: it counts the sum of seconds, but at the end, before returning the value, it converts seconds to hours and rounds to hundredths, and returns the value 0.0, as if the entity was not turned on at all.

What parameter should I use to increase the precision for the value 0 ?

In yaml, where you create the history_stats sensor, add the key unique_id. Give it any unique string you want.

  - platform: history_stats
    unique_id: history_stats_test_on_time
    name: history_stats_test_on_time
    entity_id: input_boolean.history_stats_test_boolean
    state: "on"
    type: time
    start: "{{ today_at() }}"
    end: "{{ now() }}"

After this, the entity state will have the full float precision.

1 Like