# Logstash filter add new calculated float value

**URL:** <https://discuss.elastic.co/t/logstash-filter-add-new-calculated-float-value/311724>\
**Category:** Logstash\
**Created:** [August 9, 2022, 12:20pm UTC](https://discuss.elastic.co/t/logstash-filter-add-new-calculated-float-value/311724 "2022-08-09T12:20:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![azeiner](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@azeiner](https://discuss.elastic.co/u/azeiner)\
**Post date:** [August 9, 2022, 12:20pm UTC](https://discuss.elastic.co/t/logstash-filter-add-new-calculated-float-value/311724/1 "2022-08-09T12:20:59Z")

</div>

I've got Data from a Json Object and what i want is to calculate some Values of the same Object  
e.g.

```auto
   filter {
      json {
        source => "message"
		
      }

	  mutate {
		add_field => { 
		"xxx1" => "Float(%{[BME680][Temperature]}*1.634*%{[BME680][Pressure]})"
                "xxx2" => "Float(%{[BME680][Temperature1]}*1.634*%{[BME680][Pressure1]})"
                "xxx3" => "Float(%{[xxx1]}-%{[xxx2]})"
						}
	  }
    }

```

The calculation is hier just for demo purposes - it's just multipling some fields and devide them.

The action should be that:  
xxx1 should be calculated from some fields  
xxx2 should be calculated from some fields  
xxx3 should be calculated from both fields

ist this possible in logstash and how can it be done?

i only get the following

Aug 9, 2022 @ 14:13:39.104 Float(31.9_1.634_10\*993.0)

so i get the value as a String not as a number!

---

<div class="post-metadata">

**Author:** ![vilman](https://avatars.discourse-cdn.com/v4/letter/v/51bf81/32.png) [@vilman](https://discuss.elastic.co/u/vilman)\
**Post date:** [August 9, 2022, 12:35pm UTC](https://discuss.elastic.co/t/logstash-filter-add-new-calculated-float-value/311724/2 "2022-08-09T12:35:19Z")

</div>

I would use ruby:

```auto
 ruby {
            code => "event.set('[xxx1]' , event.get('[BME680][Temperature]').to_f * 1.634 * event.get('[BME680][Pressure]').to_f )"
          }

```

The '.to\_f' in ruby converts those fields from string to float. Ideally that would be done earlier with mutate { convert =\> {"[BME680][Temperature]" =\> "float" }}.

---

<div class="post-metadata">

**Author:** ![azeiner](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@azeiner](https://discuss.elastic.co/u/azeiner)\
**Post date:** [August 9, 2022, 1:02pm UTC](https://discuss.elastic.co/t/logstash-filter-add-new-calculated-float-value/311724/3 "2022-08-09T13:02:18Z")

</div>

> [@vilman](#):
>
> ```auto
> ruby {
> code => "event.set('[xxx1]' , event.get('[BME680][Temperature]').to_f * 1.634 * event.get('[BME680][Pressure]').to_f )"
> }
> 
> ```

Thank you, i'll give it a try and will report back soon!

---

<div class="post-metadata">

**Author:** ![azeiner](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@azeiner](https://discuss.elastic.co/u/azeiner)\
**Post date:** [August 9, 2022, 1:07pm UTC](https://discuss.elastic.co/t/logstash-filter-add-new-calculated-float-value/311724/4 "2022-08-09T13:07:21Z")

</div>

Thank you very much, it works perfectly!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 6, 2022, 1:08pm UTC](https://discuss.elastic.co/t/logstash-filter-add-new-calculated-float-value/311724/5 "2022-09-06T13:08:19Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
