manustar
(Manuel)
March 5, 2021, 12:56pm
1
Hi to all, on every startup i recive the following error:
Logger: homeassistant.components.template.template_entity
Source: components/template/template_entity.py:71
Integration: template (documentation, issues)
First occurred: 12:55:50 (2 occurrences)
Last logged: 12:55:50
TemplateError('ZeroDivisionError: float division by zero') while processing template 'Template("{{ (states("sensor.shelly_em_assorbimento_ch1")|float / states("sensor.shelly_em_tensione")|float)|float|round(1) }}")' for attribute '_state' in entity 'sensor.shelly_em_corrente_ch1'
TemplateError('ZeroDivisionError: float division by zero') while processing template 'Template("{{ (states("sensor.shelly_em_assorbimento_ch2")|float / states("sensor.shelly_em_tensione")|float)|float|round(1) }}")' for attribute '_state' in entity 'sensor.shelly_em_corrente_ch2'
second
Logger: homeassistant.helpers.event
Source: helpers/template.py:370
First occurred: 12:55:50 (2 occurrences)
Last logged: 12:55:50
Error while processing template: Template("{{ (states("sensor.shelly_em_assorbimento_ch1")|float / states("sensor.shelly_em_tensione")|float)|float|round(1) }}")
Error while processing template: Template("{{ (states("sensor.shelly_em_assorbimento_ch2")|float / states("sensor.shelly_em_tensione")|float)|float|round(1) }}")
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 368, in async_render
render_result = compiled.render(kwargs)
File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 1090, in render
self.environment.handle_exception()
File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 832, in handle_exception
reraise(*rewrite_traceback_stack(source=source))
File "/usr/local/lib/python3.8/site-packages/jinja2/_compat.py", line 28, in reraise
raise value.with_traceback(tb)
File "<template>", line 1, in top-level template code
ZeroDivisionError: float division by zero
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 477, in async_render_to_info
render_info._result = self.async_render(variables, **kwargs)
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 370, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ZeroDivisionError: float division by zero
this error cause a malfunction of the system that monitors electricity.
m0wlheld
(Christoph Dahlen)
March 5, 2021, 1:00pm
2
So obviously sensor.shelly_em_tensione does not return a numeric state at the time of evaluation.
manustar
(Manuel)
March 5, 2021, 1:37pm
3
this the sensor
- platform: mqtt
name: "Shelly EM Tensione"
state_topic: "shellies/shellyem-CONSUMI/emeter/0/voltage"
unit_of_measurement: "Volt"
m0wlheld
(Christoph Dahlen)
March 5, 2021, 2:02pm
4
You must guard the case of sensor.shelly_em_tensione
not returning a numeric value of being unavailable, since "{{ states(‘sensor.my_missing_sensor’) | float }} will return 0, giving you a division by zero.
Btw:
It‘s probably just a copy 'n paste error, but indentation is wrong. It should be all on the same level.
Unit of measurement should be ‘V’, not Volt.
manustar
(Manuel)
March 5, 2021, 2:14pm
5
First of all, thank you very much.
now i’m not very smart about this, would you tell me where i got the code wrong?
- platform: template
sensors:
shelly_em_corrente_ch1:
friendly_name: "Shelly EM Corrente ch1"
value_template: '{{ (states("sensor.shelly_em_assorbimento_ch1")|float / states("sensor.shelly_em_tensione")|float)|float|round(1) }}'
unit_of_measurement: "Ampere"
icon_template: mdi:current-ac
shelly_em_corrente_ch2:
friendly_name: "Shelly EM Corrente ch2"
value_template: '{{ (states("sensor.shelly_em_assorbimento_ch2")|float / states("sensor.shelly_em_tensione")|float)|float|round(1) }}'
unit_of_measurement: "Ampere"
icon_template: mdi:current-ac
123
(Taras)
March 5, 2021, 2:28pm
6
- platform: template
sensors:
shelly_em_corrente_ch1:
friendly_name: "Shelly EM Corrente ch1"
value_template: >
{% set ch1 = states("sensor.shelly_em_assorbimento_ch1")|float %}
{% set tensione = states("sensor.shelly_em_tensione")|float %}
{% if tensione != 0 %} {{ (ch1 / tensione) | round(1) }}
{% else %} {{ states("sensor.shelly_em_corrente_ch1") }}
{% endif %}
unit_of_measurement: "Ampere"
icon_template: mdi:current-ac
shelly_em_corrente_ch2:
friendly_name: "Shelly EM Corrente ch2"
value_template: >
{% set ch2 = states("sensor.shelly_em_assorbimento_ch2")|float %}
{% set tensione = states("sensor.shelly_em_tensione")|float %}
{% if tensione != 0 %} {{ (ch2 / tensione) | round(1) }}
{% else %} {{ states("sensor.shelly_em_corrente_ch2") }}
{% endif %}
unit_of_measurement: "Ampere"
icon_template: mdi:current-ac
If tensione
is not zero, it reports the result of (ch1 / tensione) | round(1)
If tensione
is zero, it reports its existing value
manustar
(Manuel)
March 5, 2021, 3:12pm
7
Implemented now let’s see if it goes, thanks a lot.
I take this opportunity for a problem always inherent to electricity, I have implemented a package for the control of consumption, but at each update / restart of HA the values are wrong, instead of marking 4.2 kwh it marks 3456 kwh. if I don’t restart it the next day it works normally.
this the package
########## sensori monitor consumi ######
utility_meter:
consumo_giornaliero:
source: sensor.shelly_em_consumo
cycle: daily
tariffs:
- f1
- f2
consumo_mensile:
source: sensor.shelly_em_consumo
cycle: monthly
tariffs:
- f1
- f2
consumo_annuale:
source: sensor.shelly_em_consumo
cycle: yearly
tariffs:
- f1
- f2
###### input number per costi ######
input_number:
costo_f1:
name: Costo Energia elettrica Fascia 1
min: 0.001
max: 0.500
unit_of_measurement: '€'
initial: 0.068
mode: box
icon: mdi:currency-eur
costo_f2:
name: Costo Energia elettrica Fascia 2
min: 0.001
max: 0.500
unit_of_measurement: '€'
initial: 0.068
mode: box
icon: mdi:currency-eur
consumo_mese_precedente:
name: Consumo Mensile
min: 0
max: 1000000
step: 0.01
mode: box
unit_of_measurement: 'kWh'
consumo_mese_precedente_f1:
name: Consumo Mensile
min: 0
max: 1000000
step: 0.01
mode: box
unit_of_measurement: 'kWh'
consumo_mese_precedente_f2:
name: Consumo Mensile
min: 0
max: 1000000
step: 0.01
mode: box
unit_of_measurement: 'kWh'
costo_mese_precedente:
name: Costo Mensile
min: 0
max: 5000
step: 0.001
mode: box
unit_of_measurement: €
consumo_anno_precedente:
name: Consumo Annuale
min: 0
max: 1000000
step: 0.01
mode: box
unit_of_measurement: 'kWh'
costo_anno_precedente:
name: Costo Annuale
min: 0
max: 5000
step: 0.001
mode: box
unit_of_measurement: €
########## input_boolean per tariffa weekend ######
input_boolean:
tariffa_weekend:
name: Tariffa Week-end/festivi
icon: mdi:calendar-range
######## orari inizio F1 ed F2 #########
input_datetime:
f1_ora_inizio:
name: "Orario Inizio Fascia 1"
has_date: false
has_time: true
initial: '08:00'
f2_ora_inizio:
name: "Orario Inizio Fascia 2"
has_date: false
has_time: true
initial: '19:00'
###### sensore per giorni feriali e festivi #######
binary_sensor:
- platform: workday
name: giorni fascia elettrica
country: IT
###### sensori vari #####
sensor:
- platform: template
sensors:
costo_consumo_oggi_f1:
friendly_name_template: "Costo Consumo Oggi fascia 1"
icon_template: mdi:currency-eur
value_template: "{{((states.sensor.consumo_giornaliero_f1.state | float) * (states.input_number.costo_f1.state| float)) | round(2) }}"
unit_of_measurement: '€'
costo_consumo_oggi_f2:
friendly_name_template: "Costo Consumo Oggi fascia 2"
icon_template: mdi:currency-eur
value_template: "{{((states.sensor.consumo_giornaliero_f2.state | float) * (states.input_number.costo_f2.state| float)) | round(2) }}"
unit_of_measurement: '€'
consumo_totale_oggi:
friendly_name_template: " Consumo Oggi "
icon_template: mdi:counter
value_template: "{{((states.sensor.consumo_giornaliero_f1.state | float) + (states.sensor.consumo_giornaliero_f2.state | float)) | round(3) }}"
unit_of_measurement: 'KWh'
costo_consumo_oggi_totale:
friendly_name_template: "Costo Consumo Oggi"
icon_template: mdi:currency-eur
value_template: "{{((states.sensor.costo_consumo_oggi_f1.state | float) + (states.sensor.costo_consumo_oggi_f2.state| float)) | round(2) }}"
unit_of_measurement: '€'
#####
costo_consumo_mensile_f1:
friendly_name_template: "Costo Consumo mensile fascia 1"
icon_template: mdi:currency-eur
value_template: "{{((states.sensor.consumo_mensile_f1.state | float) * (states.input_number.costo_f1.state| float)) | round(2) }}"
unit_of_measurement: '€'
costo_consumo_mensile_f2:
friendly_name_template: "Costo Consumo mensile fascia 2"
icon_template: mdi:currency-eur
value_template: "{{((states.sensor.consumo_mensile_f2.state | float) * (states.input_number.costo_f2.state| float)) | round(2) }}"
unit_of_measurement: '€'
consumo_totale_mensile:
friendly_name_template: "Consumo mensile"
icon_template: mdi:counter
value_template: "{{((states.sensor.consumo_mensile_f1.state | float) + (states.sensor.consumo_mensile_f2.state | float)) | round(3) }}"
unit_of_measurement: 'KWh'
costo_consumo_mensile_totale:
friendly_name_template: "Costo Consumo mensile"
icon_template: mdi:currency-eur
value_template: "{{((states.sensor.costo_consumo_mensile_f1.state | float) + (states.sensor.costo_consumo_mensile_f2.state| float)) | round(2) }}"
unit_of_measurement: '€'
#####
costo_consumo_annuale_f1:
friendly_name_template: "Costo Consumo annuale fascia 1"
icon_template: mdi:currency-eur
value_template: "{{((states.sensor.consumo_annuale_f1.state | float) * (states.input_number.costo_f1.state| float)) | round(2) }}"
unit_of_measurement: '€'
costo_consumo_annuale_f2:
friendly_name_template: "Costo Consumo annuale fascia 2"
icon_template: mdi:currency-eur
value_template: "{{((states.sensor.consumo_annuale_f2.state | float) * (states.input_number.costo_f2.state| float)) | round(2) }}"
unit_of_measurement: '€'
consumo_totale_annuale:
friendly_name_template: "Consumo annuale"
icon_template: mdi:counter
value_template: "{{((states.sensor.consumo_annuale_f1.state | float) + (states.sensor.consumo_annuale_f2.state | float)) | round(3) }}"
unit_of_measurement: 'KWh'
costo_consumo_annuale_totale:
friendly_name_template: "Costo Consumo annuale"
icon_template: mdi:currency-eur
value_template: "{{((states.sensor.costo_consumo_annuale_f1.state | float) + (states.sensor.costo_consumo_annuale_f2.state| float)) | round(2) }}"
unit_of_measurement: '€'
# attenzione!!! questo sensore serve per sapere quando è l'ultimo giorno del mese io lo tengo commentato perchè lo tengo nel file sensor.yaml
giorno_del_mese:
# entity_id: sensor.date
value_template: >
{% set dd = now().day %}
{% set dm = now().month %}
{% set nm = (as_timestamp(now()) + (24*3600))| timestamp_custom('%m', True) | int %}
{% if (dm!=nm ) %}
ultimo
{% else %}
{{dd}}
{% endif %}
this automation
####### AUTOMAZIONI CAMBIO FASCIA##########
- alias: Cambio Tariffa F1 con festivi
initial_state: on
trigger:
platform: template
value_template: >-
{{ states('sensor.time') == (states.input_datetime.f1_ora_inizio.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}
condition:
condition: and
conditions:
- condition: state
entity_id: binary_sensor.giorni_fascia_elettrica
state: 'on'
- condition: state
entity_id: input_boolean.tariffa_weekend
state: 'on'
action:
- service: utility_meter.select_tariff
data:
entity_id: utility_meter.consumo_giornaliero
tariff: f1
- service: utility_meter.select_tariff
data:
entity_id: utility_meter.consumo_mensile
tariff: f1
- service: utility_meter.select_tariff
data:
entity_id: utility_meter.consumo_annuale
tariff: f1
- alias: Cambio Tariffa F2 con festivi
initial_state: on
trigger:
platform: template
value_template: >-
{{ states('sensor.time') == (states.input_datetime.f2_ora_inizio.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}
condition:
condition: and
conditions:
- condition: state
entity_id: binary_sensor.giorni_fascia_elettrica
state: 'on'
- condition: state
entity_id: input_boolean.tariffa_weekend
state: 'on'
action:
- service: utility_meter.select_tariff
data:
entity_id: utility_meter.consumo_giornaliero
tariff: f2
- service: utility_meter.select_tariff
data:
entity_id: utility_meter.consumo_mensile
tariff: f2
- service: utility_meter.select_tariff
data:
entity_id: utility_meter.consumo_annuale
tariff: f2
- alias: cambio tariffa f2 giorni festivi
initial_state: 'on'
trigger:
platform: state
entity_id: binary_sensor.giorni_fascia_elettrica
to: 'off'
condition:
- condition: state
entity_id: input_boolean.tariffa_weekend
state: 'on'
action:
- service: utility_meter.select_tariff
data:
entity_id: utility_meter.consumo_giornaliero
tariff: f2
- service: utility_meter.select_tariff
data:
entity_id: utility_meter.consumo_mensile
tariff: f2
- service: utility_meter.select_tariff
data:
entity_id: utility_meter.consumo_annuale
tariff: f2
- alias: Cambio Tariffa F1 senza festivi
initial_state: on
trigger:
platform: template
value_template: >-
{{ states('sensor.time') == (states.input_datetime.f1_ora_inizio.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}
condition:
- condition: state
entity_id: input_boolean.tariffa_weekend
state: 'off'
action:
- service: utility_meter.select_tariff
data:
entity_id: utility_meter.consumo_giornaliero
tariff: f1
- service: utility_meter.select_tariff
data:
entity_id: utility_meter.consumo_mensile
tariff: f1
- service: utility_meter.select_tariff
data:
entity_id: utility_meter.consumo_annuale
tariff: f1
- alias: Cambio Tariffa F2 senza festivi
initial_state: on
trigger:
platform: template
value_template: >-
{{ states('sensor.time') == (states.input_datetime.f2_ora_inizio.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}
condition:
- condition: state
entity_id: input_boolean.tariffa_weekend
state: 'off'
action:
- service: utility_meter.select_tariff
data:
entity_id: utility_meter.consumo_giornaliero
tariff: f2
- service: utility_meter.select_tariff
data:
entity_id: utility_meter.consumo_mensile
tariff: f2
- service: utility_meter.select_tariff
data:
entity_id: utility_meter.consumo_annuale
tariff: f2
- alias: Costi Consumi Mese Precedente
trigger:
platform: time
at: '23:59:45'
condition:
- condition: template
value_template: "{{(states.sensor.giorno_del_mese.state == 'ultimo')}}"
action:
- service: input_number.set_value
data_template:
entity_id: input_number.consumo_mese_precedente
value: '{{ (states.sensor.consumo_totale_mensile.state | float) }}'
- service: input_number.set_value
data_template:
entity_id: input_number.consumo_mese_precedente_f1
value: '{{ (states.sensor.consumo_mensile_f1.state | float) }}'
- service: input_number.set_value
data_template:
entity_id: input_number.consumo_mese_precedente_f2
value: '{{ (states.sensor.consumo_mensile_f2.state | float) }}'
- service: input_number.set_value
data_template:
entity_id: input_number.costo_mese_precedente
value: '{{ (states.sensor.costo_consumo_mensile_totale.state | float) }}'
- service: notify.jarvis
data:
message: >
Report Consumi Mensili
Consumo: {{(states.input_number.consumo_mese_precedente.state | float)| round(2) }} kWh
Spesa: {{(states.input_number.costo_mese_precedente.state | float)| round(2) }} €"
- alias: Costi Consumi Anno Precedente
trigger:
platform: time
at: '23:59:55'
condition:
- condition: template
value_template: '{{ now().strftime("%m") == "12" }}'
- condition: template
value_template: "{{(states.sensor.giorno_del_mese.state == 'ultimo')}}"
action:
- service: input_number.set_value
data_template:
entity_id: input_number.consumo_anno_precedente
value: '{{ (states.sensor.consumo_totale_annuale.state | float) }}'
- service: input_number.set_value
data_template:
entity_id: input_number.costo_anno_precedente
value: '{{ (states.sensor.costo_consumo_annuale_totale.state | float) }}'
- service: notify.jarvis
data:
message: >
Report Consumi Annuali
Consumo: {{(states.input_number.consumo_anno_precedente.state | float)| round(2) }} kWh
Spesa: {{(states.input_number.costo_anno_precedente.state | float)| round(2) }} €"