David883
(David)
January 10, 2024, 8:19am
1
Hello everyone,
I have the following situation: I get a current m3 value from the gas meter.
This is converted into a kWh value using several helpers because I want to enter it under energy.
Content Template Helper:
{{ states('sensor.gaszahler_aktuell_helper') | float(0) * states('input_number.gas_burning_value') | float(0) * states('input_number.gas_state_number') | float(0) }}
So far everything works and I can include the value of the helper under Energy.
Now to my problem, usually after restarting the Home Assistant the value is not determined correctly by the Template Helper and shows 0.
So all the statistics are no longer correct. This now shows consumption that is higher then the current gas meter reading.
Can I change the helper so that it only calculates or changes a value if it is not 0?
koying
(Chris B)
January 10, 2024, 3:04pm
2
It’s likely because alt least one of the sensor you use is not available, yet, so the result is zero due to defaults.
Implement the availability
part of the template so that is any of the source sensor is unavailable, the template will be unavailable as well and the value ignored.
David883
(David)
January 10, 2024, 8:58pm
3
@koying
sounds good, but I have no idea how to convert my template sensor.
I have made several attempts to incorporate availability. I can not do it. can you please show me an example?
{{ states('sensor.gaszahler_aktuell_helper') | float(0) * states('input_number.gas_burning_value') | float(0) * states('input_number.gas_state_number') | float(0) }}
EDIT://
Thanks for your answer
koying
(Chris B)
January 11, 2024, 7:41am
4
This is an example template of mine
- name: "chambre_temperature"
unique_id: "chambre_temperature"
# friendly_name: 'Chambre temperature'
state: '{{ (states("sensor.temperature_humidity_sensor_0cb4_temperature")|round(1)) }}'
unit_of_measurement: "°C"
device_class: temperature
state_class: measurement
availability: '{{ states("sensor.temperature_humidity_sensor_0cb4_temperature")|is_number }}'
In your case, all sensors should be available, so something like
availability: "{{ states('sensor.gaszahler_aktuell_helper') |is_number and states('input_number.gas_burning_value') | isnumber and states('input_number.gas_state_number') | isnumber}}"
(although we can probably assume the input_number helpers will always be available)
David883
(David)
January 11, 2024, 9:21am
5
Hello, thanks. Ive tried to use this Template in my configuration.yaml
#-----------------------------------------------------
# Gaszählersensor von m3 nach kWh
#-----------------------------------------------------
- template: sensor
name: "Gaszähler_aktuell_in_kWh"
unique_id: "Gaszähler_aktuell_in_kWh"
friendly_name: "Gaszähler aktuell in kWh"
state: "{{ (states('sensor.gaszahler_aktuell_helper') | float(0) * states('input_number.gas_brennwert') | float(0) * states('input_number.gas_zustandszahl') | float(0)) | round(2) }}"
unit_of_measurement: "kWh"
device_class: energy
state_class: measurement
availability: "{{ states('sensor.gaszahler_aktuell_helper') |is_number and states('input_number.gas_brennwert') | isnumber and states('input_number.gas_zustandszahl') | isnumber}}"
But receive an Error:
koying
(Chris B)
January 11, 2024, 9:30am
6
In configuration.yaml, it should be
template:
- sensor:
- name: "Gaszähler_aktuell_in_kWh"
[...]
David883
(David)
January 11, 2024, 9:49am
7
where do you enter your code?
koying:
This is an example template of mine
- name: "chambre_temperature"
unique_id: "chambre_temperature"
# friendly_name: 'Chambre temperature'
state: '{{ (states("sensor.temperature_humidity_sensor_0cb4_temperature")|round(1)) }}'
unit_of_measurement: "°C"
device_class: temperature
state_class: measurement
availability: '{{ states("sensor.temperature_humidity_sensor_0cb4_temperature")|is_number }}'
koying
(Chris B)
January 11, 2024, 10:02am
8
I use, in configuration.yaml
template: !include_dir_merge_list platforms/templates/
Then, every yaml under the templates
subdirectory are merged as a list.
David883
(David)
January 11, 2024, 10:23am
9
My Config now:
And the Error after Restart:
I’m doing something wrong. I’m desperate.
koying
(Chris B)
January 11, 2024, 10:27am
10
in the templates.yaml, you need
- sensor:
- name: ...
But don’t use features you don’t understand.
I put above how to insert it directly in configuration.yaml
David883
(David)
January 11, 2024, 10:47am
11
Thank you very much, now it works!
I only switched to Home Assistant 2 months ago and am currently trying to find my way around. I still have a lot to learn because it is different than my old system. I would like to thank you and all other users who have the patience to help us newbies.