# Substract last value from second last

**URL:** https://community.influxdata.com/t/substract-last-value-from-second-last/12288
**Category:** Welcome & Getting Started
**Tags:** influxdb, time-series, grafana, query
**Created:** [December 11, 2019, 7:52am UTC](https://community.influxdata.com/t/substract-last-value-from-second-last/12288 "2019-12-11T07:52:59Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Manuel](https://avatars.discourse-cdn.com/v4/letter/m/67e7ee/32.png) [@Manuel](https://community.influxdata.com/u/Manuel)
#### Post date: [December 11, 2019, 7:52am UTC](https://community.influxdata.com/t/substract-last-value-from-second-last/12288/1 "2019-12-11T07:52:59Z")

</div>

Hello I’m completely new in influxdb. I have a time series which looks like this.

1576050398 8268.212  
1576050411 8268.213  
1576050424 8268.214  
1576050437 8268.215  
1576050450 8268.216  
1576050463 8268.217  
1576050476 8268.218  
1576050489 8268.219

It represents the total of electric power consumption from my smart meter. I would like to get the difference between the last two values and make a graph in grafana.

8268.219 - 8268.218 = 0.001

How would you do that. Is there a nice query I can use in grafana or do I have to create a InfluxDB Task and make the calculation there?

Thank you very much for your answer.

Greetings Manuel

---

<div class="post-metadata">

### Author: ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)
#### Post date: [December 11, 2019, 3:32pm UTC](https://community.influxdata.com/t/substract-last-value-from-second-last/12288/2 "2019-12-11T15:32:46Z")

</div>

@Manuel, if you’re using [Flux in Grafana](https://grafana.com/grafana/plugins/grafana-influxdb-flux-datasource), this would be pretty simple. You’d just use the `difference()` function to calculate the difference between subsequent points, then use the `last()` selector function to return only the last record.

```auto
from(bucket: "database/retention-policy"
  |> range(start: -1h)
  |> filter(fn: (r) => r._measurement == "exampleMeasurement" and r._field == "exampleField")
  |> difference()
  |> last()

```

---

<div class="post-metadata">

### Author: ![Manuel](https://avatars.discourse-cdn.com/v4/letter/m/67e7ee/32.png) [@Manuel](https://community.influxdata.com/u/Manuel)
#### Post date: [December 11, 2019, 5:05pm UTC](https://community.influxdata.com/t/substract-last-value-from-second-last/12288/3 "2019-12-11T17:05:52Z")

</div>

Hell Scott  
Thank you very much for your quick answer. Well, for a rookie this seems to be a InfluxDB task, right? I will follow this [instruction](https://v2.docs.influxdata.com/v2.0/process-data/get-started/) and create a task option, a data source and a data transformation.

This functions are also available for Version 1.7.8?

Regards Manuel

---

<div class="post-metadata">

### Author: ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)
#### Post date: [December 11, 2019, 5:19pm UTC](https://community.influxdata.com/t/substract-last-value-from-second-last/12288/4 "2019-12-11T17:19:32Z")

</div>

Flux is InfluxData’s new functional data query/scripting language. This is just a Flux query, not a task. And yes, Flux is packaged with 1.7.8, you just need to [enable it in your InfluxDB config](https://docs.influxdata.com/flux/v0.50/introduction/installation/).

---

<div class="post-metadata">

### Author: ![Manuel](https://avatars.discourse-cdn.com/v4/letter/m/67e7ee/32.png) [@Manuel](https://community.influxdata.com/u/Manuel)
#### Post date: [December 12, 2019, 7:02am UTC](https://community.influxdata.com/t/substract-last-value-from-second-last/12288/5 "2019-12-12T07:02:46Z")

</div>

Hello Scott, really cool. Thank you very much for your help. Will check Flux out this weekend 👍

I didn’t get your point first, because I thought that Flux is just an abbreviation for InfluxDB 😂

Greetings Manuel

---

<div class="post-metadata">

### Author: ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)
#### Post date: [December 12, 2019, 5:17pm UTC](https://community.influxdata.com/t/substract-last-value-from-second-last/12288/6 "2019-12-12T17:17:37Z")

</div>

Happy to help. And no worries. I should’ve given a little more context to Flux up front. If you have any other questions, feel free to send them my way.

---

<div class="post-metadata">

### Author: ![Manuel](https://avatars.discourse-cdn.com/v4/letter/m/67e7ee/32.png) [@Manuel](https://community.influxdata.com/u/Manuel)
#### Post date: [December 17, 2019, 10:06pm UTC](https://community.influxdata.com/t/substract-last-value-from-second-last/12288/7 "2019-12-17T22:06:36Z")

</div>

Hello Scott  
Maybe this is not the right forum to ask this question but I have difficulty to add Flux as a data source to grafana.

```
   show databases
name: databases
name
----
openhab_db

   show series
key
---
HighRate
Humidity
HumidityHome
LowRate
Temperature
TemperatureHome
TotalRate

  select * from TotalRate
name: TotalRate
time value
---- -----
1576619160022000000 16958.838
1576619220058000000 16958.842
1576619280039000000 16958.847
1576619340045000000 16958.852
1576619400035000000 16958.857
1576619460055000000 16958.861
1576619520042000000 16958.865
1576619580045000000 16958.869
1576619640039000000 16958.872

```

After Save & Test I’m getting the Error

> Data source connected, but has no data. Verify the “bucket” field and make sure the bucket has data.

What is wrong with my data source configuration?

Thank you very much for your help.

Greetings Manuel!

[FluxDataSource|416x499](https://community.influxdata.com/uploads/short-url/sABuOu9PkdPIDvBuFWewbyw9e3T.jpeg)

---

<div class="post-metadata">

### Author: ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)
#### Post date: [December 17, 2019, 10:24pm UTC](https://community.influxdata.com/t/substract-last-value-from-second-last/12288/8 "2019-12-17T22:24:38Z")

</div>

Did you enable Flux in your InfluxDB config and restart InfluxDB?

---

<div class="post-metadata">

### Author: ![Manuel](https://avatars.discourse-cdn.com/v4/letter/m/67e7ee/32.png) [@Manuel](https://community.influxdata.com/u/Manuel)
#### Post date: [December 17, 2019, 10:38pm UTC](https://community.influxdata.com/t/substract-last-value-from-second-last/12288/9 "2019-12-17T22:38:48Z")

</div>

Hello Scott  
you are so fast with answering 🙂

Yes that’s what I did.

```
[23:36:20] openhabian@openHABianPi:~$ grep flux-enabled /etc/influxdb/influxdb.conf
  # flux-enabled = false
  flux-enabled = true

```

And I restarted also influxdb.

Manuel

---

<div class="post-metadata">

### Author: ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)
#### Post date: [December 17, 2019, 10:55pm UTC](https://community.influxdata.com/t/substract-last-value-from-second-last/12288/10 "2019-12-17T22:55:17Z")

</div>

Ok, and just to make sure – `openhab_db` is the database and `TotalRate` is the retention policy within that database? When using Flux with InfluxDB 1.7.x, Flux expects the bucket to be a combination of the database and the retention policy: `db_name/rp_name`.

You may also need to deselect basic auth. InfluxDB supports basic auth, but Flux doesn’t currently handle any of the authorizations. This may not make a difference. I’m not sure.

---

<div class="post-metadata">

### Author: ![Manuel](https://avatars.discourse-cdn.com/v4/letter/m/67e7ee/32.png) [@Manuel](https://community.influxdata.com/u/Manuel)
#### Post date: [December 18, 2019, 5:12am UTC](https://community.influxdata.com/t/substract-last-value-from-second-last/12288/11 "2019-12-18T05:12:48Z")

</div>

Hello Scott  
Hm…this might be the point. As you can see in my code snippet openhab\_db is the database, that’s right. TotalRate is a time series and represents the total of electric power consumption from my smart meter in kWh.

```
> use openhab_db
Using database openhab_db
> show retention policies
name duration shardGroupDuration replicaN default
---- -------- ------------------ -------- -------
autogen 0s 168h0m0s 1 true

```

My retention policy is called autogen. So the bucket must be openhab\_db/autogen? If I use this I still get

`Data source connected, but has no data. Verify the "bucket" field and make sure the bucket has data.`

Will try to enable logging/debugging in influxdb to see more when I make a Save & Test.

Greetings Manuel

---

<div class="post-metadata">

### Author: ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)
#### Post date: [December 18, 2019, 4:12pm UTC](https://community.influxdata.com/t/substract-last-value-from-second-last/12288/12 "2019-12-18T16:12:51Z")

</div>

Yeah, with your setup, your bucket name would be `openhab_db/autogen`. I’m not really sure why it wouldn’t detect any data in the bucket. You may also try walking through the process [here](https://community.hiveeyes.org/t/using-flux-from-grafana/1362).

---

<div class="post-metadata">

### Author: ![Manuel](https://avatars.discourse-cdn.com/v4/letter/m/67e7ee/32.png) [@Manuel](https://community.influxdata.com/u/Manuel)
#### Post date: [December 24, 2019, 11:31pm UTC](https://community.influxdata.com/t/substract-last-value-from-second-last/12288/13 "2019-12-24T23:31:52Z")

</div>

Hello Scott  
Thank you for your answer. I followed your instruction and also installed yarn although I don’t know what exactly what I’m doing. I’m still getting the same error and I can’t connect to openhab\_db/autogen.

Manuel
