I’m newbie and I want to understand how things work instead of copying and pasting without knowing the meaning.
Can someone explain me the “availability” function in these codes?
- name: "ZCS Scarica Totale"
unit_of_measurement: "kWh"
state: >
{% set energy = states.sensor.modbus_zcs_energia_scaricata_totale.state | float | default(0) %}
{{ energy | round(2) }}
availability: "{{ states('sensor.modbus_zcs_energia_scaricata_totale') | float(0) > states.input_number.filtro_scarica.state | float(0) }}"
state_class: total_increasing
device_class: energy
icon: mdi:battery-low
- name: "ZCS Scarica Totale"
unit_of_measurement: "kWh"
state: >
{% set energy = states.sensor.modbus_zcs_energia_scaricata_totale.state | float | default(0) %}
{{ energy | round(2) }}
state_class: total_increasing
device_class: energy
icon: mdi:battery-low
availability: "{{ states('sensor.modbus_zcs_energia_scaricata_totale') | int(default=-100000) > -100000 }}"
1 Like
As described here , the availability attribute of a template sensor defines a template used to decide if the sensor should announce that it is unavailable. It’s important to know when things are unavailable so that you can let the user know that the sensor isn’t working instead of just reporting the last known value or something that doesn’t make sense.
In this case, your first sensor has an availability template that will be true as long as the from the sensor is larger than the number from the helper (input_number.filtro_scarica
). I don’t know why it does this, I guess values below that number are invalid for some reason?
You second sensor reports as available as long as the value from the sensor can be parsed as a number. If it wasn’t a number, then the int(-100000)
filter will return its default (-100000) which won’t be greater than -100000.
If you want to learn more about templates and try out different ideas, use the template tab on the developers tool page .
2 Likes
tom_l
August 15, 2023, 4:01am
3
Yeah the template sensor will be available if the availability template resolves as true
, this is also particularly important for energy sensors used in the energy dashboard. Using a filter (e.g. |float
) to convert state value strings to numbers so you can do math requires a default value for the filter, |float(0)
. If this default value is zero then it can play havock with the recorded energy:
total → 0 → total : add the entire total to the energy dashboard for this hour (bad)
total → unavailable → total : keep counting up from the previous total (good).
Your availability template can be simplified with new additions to HA functionality, e.g. this:
availability: "{{ states('sensor.modbus_zcs_energia_scaricata_totale') | int(default=-100000) > -100000 }}"
Can now be written as:
availability: "{{ has_value('sensor.modbus_zcs_energia_scaricata_totale') }}"
2 Likes
That’s exactly why I need it. I have several sensors that have been going crazy for some time now.
So I have to study the way to adapt the availability to each sensor. E.g. the energy sensors (KWh) are different from the sensor that indicates the percentage of the battery (also it has spikes) or from the sensors that indicate voltage and current. Here are some examples:
Energy sensor:
Battery sensors:
String voltage sensor:
As you can see, the situation is dramatic. I don’t know why I get these absurd data (it didn’t do it before) but I have to fix it because it affects my home automation.
I would like to avoid availability depending on input_number.
Can you give me some examples of how to set the availabilities?
tom_l
August 15, 2023, 5:30am
5
Sure, if you share your sensor configs.
This is the modbus.yaml which manages my inverter data. Obviously copied from a friend, but clearly needs improvement
sensor:
- platform: template
sensors:
zcs_modalita_operativa_inverter:
friendly_name: "Modalità operativa inverter"
value_template: >-
{% set mapper = {
'0' : 'In attesa',
'1' : 'Controllo Rete',
'2' : 'Collegato',
'3' : 'Grid UFP',
'4' : 'Emergency Power Supply EPS',
'5' : 'Errore risolvibile',
'6' : 'Errore permanente',
'7' : 'Autocaricamento' } %}
{% set state = states.sensor.modbus_zcs_inverter_stato_operativo.state %}
{{ mapper[state] if state in mapper else 'Sconosciuto' }}
prelievo_istantaneo_corrente_modbus:
friendly_name: Prelievo istantaneo modbus
device_class: power
value_template: >
{% set linea = states('sensor.modbus_inverter_rete_immisione_prelievo')|int(0) %}
{% if linea < 0 %}
{{ 0 - linea }}
{% else %} {{ 0 | int }}
{% endif %}
unit_of_measurement: "W"
immissione_istantanea_corrente_modbus:
friendly_name: Immissione istantanea modbus
device_class: power
value_template: >
{% set linea = states('sensor.modbus_inverter_rete_immisione_prelievo')|int(0) %}
{% if linea >= 0 %}
{{ linea }}
{% else %} {{ 0 | int }}
{% endif %}
unit_of_measurement: "W"
#####convertitore
modbus:
- name: hub2
type: tcp
host: 192.168.1.52
port: 502
sensors:
- name: "Modbus ZCS Temperatura Inverter"
slave: 1
address: 0x0418
input_type: holding
data_type: int16
unit_of_measurement: "°C"
device_class: temperature
scale: 1
precision: 0
scan_interval: 60
lazy_error_count: 3
unique_id: "temperatura inverter"
- name: "Modbus ZCS Temperatura Radiatore"
slave: 1
address: 0x041A
input_type: holding
data_type: int16
unit_of_measurement: "°C"
device_class: temperature
scale: 1
precision: 0
scan_interval: 60
lazy_error_count: 3
unique_id: "temperatura radiatore"
- name: "Modbus ZCS Inverter Tempo produzione oggi"
slave: 1
address: 0x0426
input_type: holding
data_type: uint16 ####corretto uint####
unit_of_measurement: minuti
scale: 1
precision: 0
scan_interval: 60
lazy_error_count: 3
- name: "Modbus ZCS Potenza Rete"
slave: 1
address: 0x0485
input_type: holding
data_type: int16
unit_of_measurement: W
device_class: power
scale: 10
precision: 0
scan_interval: 5
lazy_error_count: 3
- name: "Modbus ZCS Frequenza Rete"
slave: 1
address: 0x0484
input_type: holding
data_type: uint16
unit_of_measurement: Hz
scale: 0.01
precision: 0
scan_interval: 60
lazy_error_count: 3
unique_id: "frequenza rete"
- name: "Modbus Inverter Rete Immissione Prelievo"
slave: 1
address: 0x0488
data_type: int16
device_class: power
state_class: measurement
unit_of_measurement: "W"
scale: 10
scan_interval: 5
precision: 2
lazy_error_count: 3
unique_id: "immissione prelievo rete"
- name: "Modbus ZCS Voltaggio Rete"
slave: 1
address: 0x048D
input_type: holding
data_type: uint16
unit_of_measurement: V
device_class: voltage
scale: 0.1
precision: 1
scan_interval: 60
lazy_error_count: 3
unique_id: "voltaggio rete"
- name: "Modbus ZCS Corrente Rete"
slave: 1
address: 0x048E ###in sostituzione di 499, modifica 020623###
input_type: holding
data_type: uint16
unit_of_measurement: A
device_class: current
scale: 0.01
precision: 1
scan_interval: 60
lazy_error_count: 3
unique_id: "corrente rete"
- name: "Modbus ZCS potenza istantanea carichi"
slave: 1
address: 0x04AF
input_type: holding
data_type: uint16
device_class: power
state_class: measurement
unit_of_measurement: W
scale: 10
precision: 0
scan_interval: 5
lazy_error_count: 3
unique_id: "consumo istantaneo"
- name: "Modbus ZCS FV potenza istantanea" #### QUESTO NELLA TABELLA NON ESISTE####
slave: 1
address: 0x05C4
input_type: holding
data_type: uint16
unit_of_measurement: W
device_class: power
scale: 100
precision: 0
scan_interval: 5
lazy_error_count: 3
- name: "Modbus ZCS FV1 Voltaggio"
slave: 1
address: 0x0584
input_type: holding
data_type: uint16
unit_of_measurement: V
device_class: voltage
scale: 0.1
precision: 1
scan_interval: 5
lazy_error_count: 3
unique_id: "voltaggio fv1"
- name: "Modbus ZCS FV1 Corrente"
slave: 1
address: 0x0585
input_type: holding
data_type: uint16
unit_of_measurement: A
device_class: current
scale: 0.01
precision: 2
scan_interval: 5
lazy_error_count: 3
unique_id: "corrente fv1"
- name: "Modbus ZCS FV1 Potenza"
slave: 1
address: 0x0586
input_type: holding
data_type: uint16
unit_of_measurement: W
device_class: power
scale: 10
precision: 1
scan_interval: 5
lazy_error_count: 3
unique_id: "potenza fv1"
- name: "Modbus ZCS FV2 Voltaggio"
slave: 1
address: 0x0587
input_type: holding
data_type: uint16
unit_of_measurement: V
device_class: voltage
scale: 0.1
precision: 1
scan_interval: 5
lazy_error_count: 3
unique_id: "voltaggio fv2"
- name: "Modbus ZCS FV2 Corrente"
slave: 1
address: 0x0588
input_type: holding
data_type: uint16
unit_of_measurement: A
device_class: current
scale: 0.01
precision: 2
scan_interval: 5
lazy_error_count: 3
unique_id: "corrente fv2"
- name: "Modbus ZCS FV2 Potenza"
slave: 1
address: 0x0589
input_type: holding
data_type: uint16
unit_of_measurement: W
device_class: power
scale: 10
precision: 1
scan_interval: 5
lazy_error_count: 3
unique_id: "potenza fv2"
- name: "Modbus ZCS Batteria Voltaggio"
slave: 1
address: 0x0604
input_type: holding
data_type: uint16
unit_of_measurement: V
device_class: voltage
scale: 0.1
precision: 1
scan_interval: 60
lazy_error_count: 3
unique_id: "voltaggio batteria"
- name: "Modbus ZCS Batteria Corrente"
slave: 1
address: 0x0605
input_type: holding
data_type: int16
unit_of_measurement: A
device_class: current
scale: 0.01
precision: 1
scan_interval: 5
lazy_error_count: 3
unique_id: "corrente batteria"
- name: "Modbus ZCS Batteria Potenza"
slave: 1
address: 0x0606
input_type: holding
data_type: int16
unit_of_measurement: W
device_class: power
scale: 10
precision: 0
scan_interval: 5
lazy_error_count: 3
unique_id: "potenza batteria"
- name: "Modbus ZCS Batteria Percentuale" #NELLA TABELLA NON ESISTE#
slave: 1
address: 0x0668
input_type: holding
data_type: uint16
device_class: battery
unit_of_measurement: "%"
scale: 1
precision: 0
scan_interval: 60
lazy_error_count: 3
unique_id: "% carica batteria"
- name: "Modbus ZCS Batteria Temperatura"
slave: 1
address: 0x0607
input_type: holding
data_type: int16
unit_of_measurement: "°C"
device_class: temperature
scale: 1
precision: 1
scan_interval: 60
lazy_error_count: 3
- name: "Modbus ZCS Batteria Numero Cicli"
slave: 1
address: 0x060A
input_type: holding
data_type: uint16
unit_of_measurement: cicli
scale: 1
precision: 0
scan_interval: 600
lazy_error_count: 3
- name: "Modbus ZCS Energia generata oggi"
slave: 1
address: 0x0684
input_type: holding
data_type: uint32
unit_of_measurement: kWh
device_class: energy
scale: 0.01
precision: 1
scan_interval: 60
lazy_error_count: 3
unique_id: "energia generata oggi"
- name: "Modbus ZCS Energia generata totale"
slave: 1
address: 0x0686
input_type: holding
data_type: uint32
unit_of_measurement: kWh
device_class: energy
scale: 0.1
precision: 1
scan_interval: 60
lazy_error_count: 3
unique_id: "energia generata tot"
- name: "Modbus ZCS Energia consumata oggi"
slave: 1
address: 0x0688
input_type: holding
data_type: uint32
unit_of_measurement: kWh
device_class: energy
scale: 0.01
precision: 1
scan_interval: 60
lazy_error_count: 3
unique_id: "energia consumata oggi"
- name: "Modbus ZCS Energia consumata totale"
slave: 1
address: 0x068A
input_type: holding
data_type: uint32
unit_of_measurement: kWh
device_class: energy
scale: 0.1
precision: 1
scan_interval: 60
lazy_error_count: 3
unique_id: "energia consumata totale"
- name: "Modbus ZCS Energia acquistata oggi"
slave: 1
address: 0x068C
input_type: holding
data_type: uint32
unit_of_measurement: kWh
device_class: energy
scale: 0.01
precision: 1
scan_interval: 60
lazy_error_count: 3
unique_id: "energia acquistata oggi"
- name: "Modbus ZCS Energia acquistata totale"
slave: 1
address: 0x068E
input_type: holding
data_type: uint32
unit_of_measurement: kWh
device_class: energy
scale: 0.1
precision: 1
scan_interval: 60
lazy_error_count: 3
unique_id: "energia acquistata totale"
- name: "Modbus ZCS Energia venduta oggi"
slave: 1
address: 0x0690
input_type: holding
data_type: uint32
unit_of_measurement: kWh
device_class: energy
scale: 0.01
precision: 1
scan_interval: 60
lazy_error_count: 3
unique_id: "energia venduta oggi"
- name: "Modbus ZCS Energia venduta totale"
slave: 1
address: 0x0692
input_type: holding
data_type: uint32
unit_of_measurement: kWh
device_class: energy
scale: 0.1
precision: 1
scan_interval: 60
lazy_error_count: 3
unique_id: "energia venduta totale"
- name: "Modbus ZCS Energia caricata oggi"
slave: 1
address: 0x0694
input_type: holding
data_type: uint32
unit_of_measurement: kWh
device_class: energy
scale: 0.01
precision: 1
scan_interval: 60
lazy_error_count: 3
unique_id: "energia caricata oggi"
- name: "Modbus ZCS Energia caricata totale"
slave: 1
address: 0x0696
input_type: holding
data_type: uint32
unit_of_measurement: kWh
device_class: energy
scale: 0.1
precision: 1
scan_interval: 60
lazy_error_count: 3
unique_id: "energia caricata totale"
- name: "Modbus ZCS Energia scaricata oggi"
slave: 1
address: 0x0698
input_type: holding
data_type: uint32
unit_of_measurement: kWh
device_class: energy
scale: 0.01
precision: 1
scan_interval: 60
lazy_error_count: 3
unique_id: "energia scaricata oggi"
- name: "Modbus ZCS Energia scaricata totale"
slave: 1
address: 0x069A
input_type: holding
data_type: uint32
unit_of_measurement: kWh
device_class: energy
scale: 0.1
precision: 1
scan_interval: 60
lazy_error_count: 3
unique_id: "energia scaricata totale"
template:
- sensor:
##MODBUS FILTRATI
- name: "ZCS Cicli Batteria"
state: >
{% set energy = states.sensor.modbus_zcs_batteria_numero_cicli.state | int | default(0) %}
{{ energy }}
state_class: total_increasing
icon: mdi:battery-sync
availability: "{{ states('sensor.modbus_zcs_batteria_numero_cicli') | int(default=-100000) > -100000 }}" #aggiunto 14.08.2023 prova spikes
- name: "ZCS Percentuale Batteria"
unit_of_measurement: "%"
state: >
{% set energy = states.sensor.modbus_zcs_batteria_percentuale.state | int | default(0) %}
{{ energy | round(2) }}
state_class: measurement
device_class: battery
icon: mdi:battery
#POTENZA ISTANTANEA
- name: "ZCS Potenza Generata"
unit_of_measurement: "W"
state: >
{% set power1 = states.sensor.modbus_zcs_fv1_potenza.state | int(0) %}
{% set power2 = states.sensor.modbus_zcs_fv2_potenza.state | int(0) %}
{{ power1 + power2 }}
state_class: measurement
device_class: power
icon: mdi:solar-power
unique_id: "potenza generata"
- name: "ZCS Potenza CaricaScarica"
unit_of_measurement: "W"
state: >
{% set power = states.sensor.modbus_zcs_batteria_potenza.state | int(0) %}
{{ power | round(2) }}
state_class: measurement
device_class: power
icon: mdi:battery-high
- name: "ZCS Potenza Carica"
unit_of_measurement: "W"
state: >
{% set power = states.sensor.zcs_potenza_caricascarica.state | int(0) %}
{% if power >= 0 %}
{{ power }}
{% else %} 0
{% endif %}
state_class: measurement
device_class: power
icon: mdi:battery-high
- name: "ZCS Potenza Scarica"
unit_of_measurement: "W"
state: >
{% set power = states.sensor.zcs_potenza_caricascarica.state | int(0) %}
{% if power < 0 %}
{{ 0 - power }}
{% else %} 0
{% endif %}
state_class: measurement
device_class: power
icon: mdi:battery-low
- name: "ZCS Stato Batteria" ###aggiunto 080623###
icon: mdi:battery
unique_id: "stato_batteria"
state: |-
{% if states('sensor.modbus_zcs_batteria_potenza') | int(0) > 0 %}
Charging
{% elif states('sensor.modbus_zcs_batteria_potenza') | int(0) < 0 %}
Discharging
{% else %}
Standby
{% endif %}
# Energia giornaliera
- name: "ZCS Energia Prodotta oggi"
unit_of_measurement: "kWh"
state: >
{% set energy = states.sensor.modbus_zcs_energia_generata_oggi.state | float | default(0) %}
{{ energy | round(2) }}
state_class: total_increasing
device_class: energy
icon: mdi:solar-power
- name: "ZCS Energia Caricata oggi"
unit_of_measurement: "kWh"
state: >
{% set energy = states.sensor.modbus_zcs_energia_caricata_oggi.state | float | default(0) %}
{{ energy | round(2) }}
state_class: total_increasing
device_class: energy
icon: mdi:battery-high
- name: "ZCS Energia Scaricata oggi"
unit_of_measurement: "kWh"
state: >
{% set energy = states.sensor.modbus_zcs_energia_scaricata_oggi.state | float | default(0) %}
{{ energy | round(2) }}
state_class: total_increasing
device_class: energy
icon: mdi:battery-low
- name: "ZCS Energia Immessa oggi"
unit_of_measurement: "kWh"
state: >
{% set energy = states.sensor.modbus_zcs_energia_venduta_oggi.state | float | default(0) %}
{{ energy | round(2) }}
state_class: total_increasing
device_class: energy
icon: mdi:transmission-tower-import
- name: "ZCS Energia Prelevata oggi"
unit_of_measurement: "kWh"
state: >
{% set energy = states.sensor.modbus_zcs_energia_acquistata_oggi.state | float | default(0) %}
{{ energy | round(2) }}
state_class: total_increasing
device_class: energy
icon: mdi:transmission-tower-export
- name: "ZCS Energia Consumata oggi"
unit_of_measurement: "kWh"
state: >
{% set energy = states.sensor.modbus_zcs_energia_consumata_oggi.state | float | default(0) %}
{{ energy | round(2) }}
state_class: total_increasing
device_class: energy
icon: mdi:lightbulb
- name: "ZCS Energia Autoconsumata oggi"
unit_of_measurement: "kWh"
state: >
{% set prelievo = states.sensor.modbus_zcs_energia_acquistata_oggi.state | float | default(0) %}
{% set consumo = states.sensor.modbus_zcs_energia_consumata_oggi.state | float | default(0) %}
{{ consumo - prelievo | round(2) }}
state_class: total #_increasing
device_class: energy
unique_id: zcs_energy_selfcons_today
icon: mdi:power-plug
##### total
- name: "ZCS Produzione Totale"
unit_of_measurement: "kWh"
state: >
{% set energy = states.sensor.modbus_zcs_energia_generata_totale.state | float | default(0) %}
{{ energy | round(2) }}
#availability: "{{ states('sensor.modbus_zcs_energia_generata_totale') | float(0) > states.input_number.filtro_produzione.state | float(0) }}"
state_class: total_increasing
device_class: energy
icon: mdi:weather-sunny
availability: "{{ states('sensor.modbus_zcs_energia_generata_totale') | int(default=-100000) > -100000 }}" #aggiunto 14.08.2023
- name: "ZCS Prelievo Totale"
unit_of_measurement: "kWh"
state: >
{% set energy = states.sensor.modbus_zcs_energia_acquistata_totale.state | float | default(0) %}
{{ energy | round(2) }}
#availability: "{{ states('sensor.modbus_zcs_energia_acquistata_totale') | float(0) > states.input_number.filtro_prelievo.state | float(0) }}"
state_class: total_increasing
device_class: energy
icon: mdi:transmission-tower
availability: "{{ states('sensor.modbus_zcs_energia_acquistata_totale') | int(default=-100000) > -100000 }}" #aggiunto 14.08.2023
- name: "ZCS Immissione Totale"
unit_of_measurement: "kWh"
state: >
{% set energy = states.sensor.modbus_zcs_energia_venduta_totale.state | float | default(0) %}
{{ energy | round(2) }}
#availability: "{{ states('sensor.modbus_zcs_energia_venduta_totale') | float(0) > states.input_number.filtro_immissione.state | float(0) }}"
state_class: total_increasing
device_class: energy
icon: mdi:transmission-tower
availability: "{{ states('sensor.modbus_zcs_energia_venduta_totale') | int(default=-100000) > -100000 }}" #aggiunto 14.08.2023
- name: "ZCS Scarica Totale"
unit_of_measurement: "kWh"
state: >
{% set energy = states.sensor.modbus_zcs_energia_scaricata_totale.state | float | default(0) %}
{{ energy | round(2) }}
#availability: "{{ states('sensor.modbus_zcs_energia_scaricata_totale') | float(0) > states.input_number.filtro_scarica.state | float(0) }}"
state_class: total_increasing
device_class: energy
icon: mdi:battery-low
availability: "{{ states('sensor.modbus_zcs_energia_scaricata_totale') | int(default=-100000) > -100000 }}" #aggiunto 14.08.2023
- name: "ZCS Carica Totale"
unit_of_measurement: "kWh"
state: >
{% set energy = states.sensor.modbus_zcs_energia_caricata_totale.state | float | default(0) %}
{{ energy | round(2) }}
#availability: "{{ states('sensor.modbus_zcs_energia_caricata_totale') | float(0) > states.input_number.filtro_carica.state | float(0) }}"
state_class: total_increasing
device_class: energy
icon: mdi:battery-high
availability: "{{ states('sensor.modbus_zcs_energia_caricata_totale') | int(default=-100000) > -100000 }}" #aggiunto 14.08.2023
- name: "ZCS Consumo Totale"
unit_of_measurement: "kWh"
state: >
{% set energy = states.sensor.modbus_zcs_energia_consumata_totale.state | float | default(0) %}
{{ energy | round(2) }}
#availability: "{{ states('sensor.modbus_zcs_energia_consumata_totale') | float(0) > 100 }}"
state_class: total_increasing
device_class: energy
icon: mdi:battery-high
availability: "{{ states('sensor.modbus_zcs_energia_consumata_totale') | int(default=-100000) > -100000 }}" #aggiunto 14.08.2023
- name: "ZCS Autoconsumo Totale"
unit_of_measurement: "kWh"
state: >
{% set prelievo = states.sensor.modbus_zcs_energia_acquistata_totale.state | float | default(0) %}
{% set consumo = states.sensor.modbus_zcs_energia_consumata_totale.state | float | default(0) %}
{{ consumo - prelievo | round(2) }}
state_class: total #_increasing
device_class: energy
icon: mdi:power-plug
tom_l
August 15, 2023, 10:05am
7
Like this (I’m not doing all of them):
- name: "ZCS Potenza Generata"
unit_of_measurement: "W"
state: >
{% set power1 = states('sensor.modbus_zcs_fv1_potenza') | int(0) %}
{% set power2 = states('sensor.modbus_zcs_fv2_potenza') | int(0) %}
{{ power1 + power2 }}
availability: >
{{ has_value('sensor.modbus_zcs_fv1_potenza') and
has_value('sensor.modbus_zcs_fv2_potenza') }}
state_class: measurement
device_class: power
icon: mdi:solar-power
unique_id: "potenza generata"
Also take note of this warning:
https://www.home-assistant.io/docs/configuration/templating/#states
vinceg0267
(Vinceg0267)
August 15, 2023, 11:08am
8
Many thanks for the reply. Yes, I replaced all states.sensor… with states(‘sensor…’)
About my problem, I copied the code in the yaml but, strangely, I get unknown. In the template, however, it’s fine.
- name: "ZCS Potenza Generata"
unit_of_measurement: "W"
state: >
340
availability: >
True
state_class: measurement
device_class: power
icon: mdi:solar-power
unique_id: "potenza generata"
vinceg0267
(Vinceg0267)
August 15, 2023, 11:13am
9
But if has_value(‘sensor.my_sensor’) will test if the given entity is not unknown or unavailable, should I assume that the spikes come from unknown or unavailable states? Even if I have a spike like 144,599,864.3 kWh?