RASBR
(Rami)
February 25, 2023, 8:01am
1
Hi,
template:
sensor:
- name: MySensor
OR
sensor:
- platform: template
name: MySensor
trying to do the following but doesn’t seem to work:
template:
sensor:
- name: Diesel Tank
unique_id: 2ea76723-bca3-47bd-9547-3e548e56dcd8
attributes:
height: >
{{states('input_number.diesel_tank_h')}}
length: >
{{states('input_number.diesel_tank_l')}}
width: >
{{states('input_number.diesel_tank_w')}}
max_v: >
{{states('input_number.diesel_tank_max_v')}}
volt_per_cm: >
{{states('input_number.diesel_tank_volt_per_cm')}}
adc_offset: >
{{states('input_number.diesel_tank_adc_offset')}}
state: >
{{this.attributes.height * this.attributes.length * this.attributes.width}}
unit_of_measurement: "m³"
Any advice?
Thanks in advance
R.S.
States are always strings so you need to convert them. Since attributes are not limited in the same way, I would do the conversions at the attribute level.
template:
sensor:
- name: Diesel Tank
unique_id: 2ea76723-bca3-47bd-9547-3e548e56dcd8
attributes:
height: >
{{ states('input_number.diesel_tank_h') | float(0) }}
length: >
{{ states('input_number.diesel_tank_l') | float(0) }}
width: >
{{ states('input_number.diesel_tank_w') | float(0) }}
max_v: >
{{ states('input_number.diesel_tank_max_v') | float(0) }}
volt_per_cm: >
{{ states('input_number.diesel_tank_volt_per_cm') | float(0) }}
adc_offset: >
{{ states('input_number.diesel_tank_adc_offset') | float(0) }}
state: >
{{ (this.attributes.height * this.attributes.length * this.attributes.width) | default('Undefined') }}
state_class: measurement
unit_of_measurement: "m³"
1 Like
RASBR
(Rami)
February 26, 2023, 5:59am
3
Hi @Didgeridrew
Thanks for taking the time to reply. I tried your code but I don’t think it made any difference.
OR am I missing something?
template:
sensor:
- name: Diesel Tank
attributes:
height: >
{{ states('input_number.diesel_tank_h') | float(1) }}
length: >
{{ states('input_number.diesel_tank_l') | float(1) }}
width: >
{{ states('input_number.diesel_tank_w') | float(1) }}
max_v: >
{{ states('input_number.diesel_tank_max_v') | float(0) }}
volt_per_cm: >
{{ states('input_number.diesel_tank_volt_per_cm') | float(2) }}
adc_offset: >
{{ states('input_number.diesel_tank_adc_offset') | float(2) }}
state: >
{{ this.attributes.height * this.attributes.length * this.attributes.width }}
unit_of_measurement: "m³"
123
(Taras)
February 26, 2023, 1:46pm
4
I believe a Template Sensor’s state
option is computed before its attributes
option.
That means your state
template attempts to reference an attribute that will only be computed after the state
is computed. The state
can’t be computed unless the attribute exists but it can’t be computed unless the state
finishes its calculation. The deadlocked situation is reported as an error message in the Log.
Make the template for state
refer to all theinput_number
entities directly as opposed to via its attributes.
state: >
{{ states('input_number.diesel_tank_h') | float(1) *
states('input_number.diesel_tank_l') | float(1) *
states('input_number.diesel_tank_w') | float(1) }}
1 Like
RASBR
(Rami)
February 26, 2023, 6:49pm
5
YESSS. Thiat’s it. Thank you
1 Like