project Utilities / Python Influxdb Downsample avatar

utilities/python_influxdb_downsample#1: Allow custom flux calls to be passed in



Issue Information

Issue Type: issue
Status: closed
Reported By: btasker
Assigned To: btasker

Milestone: vnext
Created: 04-Feb-23 16:19



Description

I have a flux task in Kapacitor which calculates the state of our heating:

from(bucket: "home_assistant/autogen", host: host, token: token)
 |> range(start: -1h)
 |> filter(fn: (r) => r._measurement == "climate.downstairs" and (r._field == "hvac_action_str"))
 |> map(fn: (r) => ({ 
    _time: r._time,
    _measurement: "heating",
    _field: "heating_state",
    hour: date.hour(t: r._time),
    _value: if r._value == "heating" then 
               1 
            else 
               0
            }))
 |> aggregateWindow(every: 15m, fn: max, createEmpty: false)
 |> drop(columns: ["_start", "_stop"])

Currently, it can't be ported over because there's no way to implement that map().

What'd be good is if we could provide that map within the yaml config with something like

custom_calls:
    - >
      |> map(fn: (r) => ({ 
      _time: r._time,
      _measurement: "heating",
      _field: "heating_state",
      hour: date.hour(t: r._time),
      _value: if r._value == "heating" then 
               1 
            else 
               0
            }))

in order to have it appended to the end of the query



Toggle State Changes

Activity


assigned to @btasker

mentioned in issue #4

changed the description

verified

mentioned in commit 28fd36007bc44674012dc1c95a8d9fa89c18e3cb

Commit: 28fd36007bc44674012dc1c95a8d9fa89c18e3cb 
Author: B Tasker                            
                            
Date: 2023-02-05T12:40:34.000+00:00 

Message

Implement support for attribute flux_calls for utilities/python_influxdb_downsample#1

+43 -1 (44 lines changed)
verified

mentioned in commit 35f837599370b63d7a250a31c1e150f9ea8d88ef

Commit: 35f837599370b63d7a250a31c1e150f9ea8d88ef 
Author: B Tasker                            
                            
Date: 2023-02-05T12:40:58.000+00:00 

Message

docs: update README to give example with custom flux calls (utilities/python_influxdb_downsample#1)

+59 -0 (59 lines changed)

As of the commits above, we now support a list of flux calls via the flux_calls attribute

   downsample_custom_function:
        # Name for the task
        name: "Downsample power stats with custom calls"
        influx: home1x
        period: 10
        window: 10
        bucket: Systemstats
        measurement: power_watts
        fields: 
            - consumption
        group_by: []
        filters:
            - 'r.host == "power-meter"'

        # Define some additional flux calls to be appended to the
        # generated query
        # 
        # Define as a list:
        flux_calls:
            - // Convert to kWh
            - >
                |> map(fn: (r) => ({
                    r with _value: float(v: r._value) / 1000.0
                }))
        aggregates: 
            mean:
              as: 
                consumption: "usage_mean_kWh"
        # Which influx instance are we writing to
        output_influx: home1x
        # Which bucket?
        output_bucket: testing_db

These are appended to the end of the query.

I decided it was better to append after the generated window() call because this allows things like reduce() to be used.