# Multiply and adding fields in logstsh

**URL:** <https://discuss.elastic.co/t/multiply-and-adding-fields-in-logstsh/198767>\
**Category:** Logstash\
**Created:** [September 9, 2019, 6:35pm UTC](https://discuss.elastic.co/t/multiply-and-adding-fields-in-logstsh/198767 "2019-09-09T18:35:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew22](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@Andrew22](https://discuss.elastic.co/u/Andrew22)\
**Post date:** [September 9, 2019, 6:35pm UTC](https://discuss.elastic.co/t/multiply-and-adding-fields-in-logstsh/198767/1 "2019-09-09T18:35:57Z")

</div>

Hello,

I am trying to convert three separate fields for hour minute and second to one in seconds so I have dome some research and found

```
             if [hour] and [minute] and [second] {
        ruby {
              code => "event['duration_in_seconds'] = event['hour'].to_i * 3600 + event['minute'].to_i * 60 + event['second']"
           }
      }

```

but when I run logstash I get the error

[ERROR][logstash.filters.ruby] Ruby exception occurred: undefined method `[]' for #\<LogStash::Event:

any help would be appreciated

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [September 9, 2019, 7:00pm UTC](https://discuss.elastic.co/t/multiply-and-adding-fields-in-logstsh/198767/2 "2019-09-09T19:00:38Z")

</div>

You need to use [event API](https://www.elastic.co/guide/en/logstash/current/event-api.html) -- event.get and event.set.

---

<div class="post-metadata">

**Author:** ![Andrew22](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@Andrew22](https://discuss.elastic.co/u/Andrew22)\
**Post date:** [September 9, 2019, 7:12pm UTC](https://discuss.elastic.co/t/multiply-and-adding-fields-in-logstsh/198767/3 "2019-09-09T19:12:03Z")

</div>

That helped a lot thank you!

My solution for anyone who cares.

```
       if [hour] and [minute] and [second] {
        ruby {
              code => "event.set('duration_in_seconds' , ((event.get('hour').to_f * 3600) + (event.get('minute').to_f * 60) + event.get('second').to_f))"
           }
      }
```

---

<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:** [October 7, 2019, 7:12pm UTC](https://discuss.elastic.co/t/multiply-and-adding-fields-in-logstsh/198767/4 "2019-10-07T19:12:04Z")

</div>

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