ddeconin
(Diether De Coninck)
February 7, 2023, 1:49pm
1
I’m trying to create a template sensor to calculate the difference between 2 states for an entity.
the entity itself just is an ever increasing value
I tried adding the template sensor below , but the sensor is allways unknown
`template:
trigger:
platform: state
entity_id:
sensor.airco_keuken_energy
sensor:
name: “Current Airco Keuken Power Current State”
state: “{{ trigger.to_state.state - trigger.from_state.state | float | round(2)}}”
`
You are missing a float(0)
… so your first state is a string
"{{ (trigger.to_state.state | float(0) - trigger.from_state.state | float(0) ) | round(2) }}"
123
(Taras)
February 7, 2023, 1:59pm
3
state: "{{ (trigger.to_state.state | float(0) - trigger.from_state.state | float(0)) | round(2) }}"
ddeconin
(Diether De Coninck)
February 7, 2023, 2:31pm
4
I did that now, but the result keeps being ‘Unknown’ state
123
(Taras)
February 7, 2023, 2:37pm
5
It will report unknown
until sensor.airco_keuken_energy
changes state.
ddeconin
(Diether De Coninck)
February 7, 2023, 3:25pm
6
After I changed the code to
state: "{{ (states('trigger.to_state') | float(0) - states('trigger.from_state') | float(0)) | round(2) }}"
I got something else then Unknown, but it still doesn’t update
123
(Taras)
February 7, 2023, 3:37pm
7
That’s not the template I suggested to you. Compare what I posted above to what you have and you’ll see they’re not the same.
1 Like
ddeconin
(Diether De Coninck)
February 8, 2023, 9:05am
8
Hi, Yes, I had done that first, waited until there was an update of the base entity and it didn’t seem to work.
So then I changed the code to what I had above and it didn’t work either.
Now I changed it back to what you wrote and now it seems to work! Looks like it was another USB-moment for me… )
Next step I’m trying to achieve…
The from_state & to_state are 2 values in kWh.
It seems to be possible to also get the from_state.last_changed & to_state.last_changed
So from that I want to try and calculate the average W used in that period…
Hi I tried to copy the above but tools i get ‘trigger’ is undefined and the sensor shows up as unavailable. I have confirmed the states of the entities defined are getting updated. Can someone please help?
#BMW Fuel Consumption
template:
- trigger:
- platform: state
entity_id: sensor.bmw_x5_remaining_fuel
sensor:
- name: "BMW X5 Trip Fuel Consumption"
state: "{{ (trigger.to_state.state | float(0) - trigger.from_state.state | float(0)) | round(2) }}"
unit_of_measurement: "L"
availability: "{{ states('sensor.bmw_x5_trip_fuel_consumption')|is_number }}"
- trigger:
- platform: state
entity_id: sensor.bmw_x5_mileage
sensor:
- name: "BMW X5 Trip Distance"
state: "{{ (trigger.to_state.state | float(0) - trigger.from_state.state | float(0)) | round(2) }}"
unit_of_measurement: "km"
availability: "{{ states('sensor.bmw_x5_trip_distance')|is_number }}"
123
(Taras)
November 19, 2023, 4:15am
10
Please post your example with proper formatting.
FAQ - Guideline 11 - Format it properly
Thanks @123 I just updated formatting of the code. Can you please advice what could be wrong.
123
(Taras)
November 19, 2023, 4:59am
12
The availability
template is incorrect.
For example, the first one should reference sensor.bmw_x5_remaining_fuel
not sensor.bmw_x5_trip_fuel_consumption
.
mekaneck
(Rick Auch)
November 19, 2023, 5:00am
13
Your availability
is self-referencing. There’s no way for it to get out of the unavailable state. Try referencing the source sensor instead:
availability: "{{ states(trigger.to_state.entity_id) | is_number }}"
@123 and @mekaneck that now makes sense. Thanks you I will change the code and give it another try.