Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Preprocess Data #5

Closed
lein1013 opened this issue Nov 15, 2021 · 7 comments
Closed

Feature Request: Preprocess Data #5

lein1013 opened this issue Nov 15, 2021 · 7 comments
Labels
enhancement New feature or request

Comments

@lein1013
Copy link

Would it be possible to enable a normaliz function on the data on the latest value? (or on the first/average value of the selected timeframe 1day, 1 week,..)
e.g. if you have a 5 stock charts with a large difference in their stock value, this would allow to compare the trends of the stocks

@dbuezas
Copy link
Owner

dbuezas commented Nov 15, 2021

Interesting idea.
Can you post examples as you imagine the configuration would look like?

The other two alternatives I see are:

  • To guide people towards the statistics sensor
  • To allow for an arbitrary JavaScript mapping function

But I do like the convinice your idea would bring

@lein1013
Copy link
Author

Let's take the normalization on the latest value and assume to have N lines (e.g. different stocks) in the chart. Then I'd like to see all lines in the chart ending up to 100%
n=1,..,N
values[n][:] = values[n][:]/values[n][-1]
by that kind of normalizsation you can see the trend of the stocks, independent from the selected timeframe (4h, 1d, 1w)
alternatively we can also normalize with the first value (values[n][:]/values[n][0]), then we see the trend based on the selected timeframe -> how much percent did the stock perform better than the other one

allowing mapping functions sounds interessting

@dbuezas
Copy link
Owner

dbuezas commented Nov 17, 2021

I'm thinking of something like this:

example of normalisation wrt to last

type: custom:plotly-graph
entities:
  - entity: sensor.my_sensor
     lambda: |-
        (ys) => ys.map(y => y/ys[ys.length-1])

example of normalisation wrt to first fetched value

  - entity: sensor.my_sensor
     lambda: |-
        (ys) => ys.map(y => y/ys[0])

So basically a js function that receives all the data for that entity, and you can do whatever you want. The problem is that the unit_of_measurement would also need to be tweakable.


Here some more examples, including unit_of_measurement (to show along the y axis and in the tooltips).

example of accumulated value

  - entity: sensor.my_sensor
     unit_of_measurement: "total pulses"
     lambda: |-
        (ys) => {
          let accumulator = 0;
          return ys.map(y => accumulator + y)
        }

example of derivative

  - entity: sensor.my_sensor
     unit_of_measurement: "pulses / second"
     lambda: |-
        (ys, xs) => {
          let last = {
            x: new Date(),
            y: 0,
          }
          return ys.map((y, index) => {
            const x = xs[index];
            const dateDelta = x - last.x;
            accumulator += (y - last.y) / dateDelta;
            last = { x, y };
            return accumulator;
          })
        }

example of right hand riemann integration

  - entity: sensor.my_sensor
     unit_of_measurement: "kWh"
     lambda: |-
        (ys, xs) => {
          let accumulator = 0;
          let last = {
            x: new Date(),
            y: 0,
          }
          return ys.map((y, index) => {
            const x = xs[index]
            const dateDelta = x - last.x;
            accumulator += last.y * dateDelta;
            last = { x, y };
            return accumulator;
          })
        }

What do you think?

Regarding normalising against the first value, you should consider that the "first value" will be just the 1st known value, which will change if you scroll the plot and older data is fetched

edit: changed variable names to x and y

@dbuezas dbuezas added the enhancement New feature or request label Nov 18, 2021
@lein1013
Copy link
Author

Looks great!
With these examples, it looks like a real nice playground to experiment with visualisations directly in the lovelace config!

@dbuezas
Copy link
Owner

dbuezas commented Nov 20, 2021

Cool, then I'll give it a go tomorrow :)
Thanks for the cool ideas!
I use my own hardcoded "hacked" version to do things like these.
With your feedback, I think we nailed a configurable way 👍

@dbuezas dbuezas changed the title Feature Request: Normalize Data Feature Request: Preprocess Data Nov 20, 2021
dbuezas referenced this issue Nov 21, 2021
@dbuezas
Copy link
Owner

dbuezas commented Nov 21, 2021

@lein1013 done in v0.5.0!
try it out and let me know if it works as you needed :)

@dbuezas dbuezas closed this as completed Nov 21, 2021
@lein1013
Copy link
Author

Thanks for implementation!
Works as specified :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants