From ea6dd73836c7198d9bf6ea581b9402ffa714be70 Mon Sep 17 00:00:00 2001 From: Chris Turra Date: Tue, 26 Oct 2021 10:40:53 -0700 Subject: [PATCH] updating utility_meter example to use new template format (#19826) Co-authored-by: Franck Nijhof --- source/_integrations/utility_meter.markdown | 36 ++++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/source/_integrations/utility_meter.markdown b/source/_integrations/utility_meter.markdown index ca0e87bd808..817aaba49b1 100644 --- a/source/_integrations/utility_meter.markdown +++ b/source/_integrations/utility_meter.markdown @@ -226,22 +226,34 @@ utility_meter: cycle: monthly ``` -Additionally, you can add template sensors to compute daily and monthly total usage. +Additionally, you can add template sensors to compute daily and monthly total usage. Important note, in these examples, +we use the `is_number()` [function](/docs/configuration/templating/#numeric-functions-and-filters) to verify the values +returned from the sensors are numeric. If this evalutes to false, `None` is returned. {% raw %} ```yaml -sensor: - - platform: template - sensors: - daily_energy: - friendly_name: Daily Energy - unit_of_measurement: kWh - value_template: "{{ states('sensor.daily_energy_offpeak')|float + states('sensor.daily_energy_peak')|float }}" - monthly_energy: - friendly_name: Monthly Energy - unit_of_measurement: kWh - value_template: "{{ states('sensor.monthly_energy_offpeak')|float + states('sensor.monthly_energy_peak')|float }}" +template: + - sensor: + - name: 'Daily Energy Total' + device_class: energy + unit_of_measurement: kWh + state: > + {% if is_number(states('sensor.daily_energy_offpeak')) and is_number(states('sensor.daily_energy_peak')) %} + {{ (states('sensor.daily_energy_offpeak') + states('sensor.daily_energy_peak')) | float }} + {% else %} + None + {% endif %} + + - name: 'Monthly Energy Total' + device_class: energy + unit_of_measurement: kWh + state: > + {% if is_number(states('sensor.monthly_energy_offpeak')) and is_number(states('sensor.monthly_energy_peak')) %} + {{ (states('sensor.monthly_energy_offpeak') + states('sensor.monthly_energy_peak')) | float }} + {% else %} + None + {% endif %} ``` {% endraw %}