From 445247373595caa151d2a08c43b6795d7cb7d219 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Mon, 18 Mar 2019 15:07:20 +0100 Subject: [PATCH] Fix filter_out: nan filer (#486) * Fix filter_out nan filter Fixes https://github.com/esphome/issues/issues/138 * Add test --- esphome/cpp_generator.py | 3 +++ tests/test1.yaml | 1 + 2 files changed, 4 insertions(+) diff --git a/esphome/cpp_generator.py b/esphome/cpp_generator.py index 5b6f004bb7..f2f2f058ff 100644 --- a/esphome/cpp_generator.py +++ b/esphome/cpp_generator.py @@ -1,4 +1,5 @@ from collections import OrderedDict +import math from esphome.core import CORE, HexInt, Lambda, TimePeriod, TimePeriodMicroseconds, \ TimePeriodMilliseconds, TimePeriodSeconds, TimePeriodMinutes @@ -260,6 +261,8 @@ class FloatLiteral(Literal): self.float_ = value def __str__(self): + if math.isnan(self.float_): + return u"NAN" return u"{:f}f".format(self.float_) diff --git a/tests/test1.yaml b/tests/test1.yaml index 4cd77c1b07..ff75ac29eb 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -173,6 +173,7 @@ sensor: - 40.0 -> 45.0 - 100.0 -> 102.5 - filter_out: 42.0 + - filter_out: nan - sliding_window_moving_average: window_size: 15 send_every: 15