removed as bad advice.
Here is an alternative Availability Sensor method for your consideration.
thanks for this, we should point to this wherever the question/issue arises⦠and it does a lot.
btw, hereās my example for calculating the unaccounted energy daily:
- unique_id: ongemeten_energie_per_dag
state: >
{{states('sensor.energy_consumed_daily')|float(0) -
states('sensor.gemeten_energie_per_dag')|float(0)}}
<<: &energy_total
unit_of_measurement: kWh
device_class: energy
state_class: total
availability: >
{{has_value('sensor.energy_consumed_daily') and
has_value('sensor.gemeten_energie_per_dag')}}
ofc, you need to set up the 2 main entities, and everyone can do that as detailed as they would like, I have a lot setup via Powercalc integration for fixed and un smart-plugged devices.
Powercalc does all the work for you, only need to feed it with a device or a fixed value.
btw, having these availability templates make the ādefaultā requirement superfluous (when uses as a regular template, not when used as a trigger based template.
Iāve never liked to set a default because it obfuscates the truth in case of issues⦠this is better
- unique_id: ongemeten_energie_per_dag
state: >
{{states('sensor.energy_consumed_daily')|float -
states('sensor.gemeten_energie_per_dag')|float}}
<<: &energy_total
unit_of_measurement: kWh
device_class: energy
state_class: total
availability: >
{{has_value('sensor.energy_consumed_daily') and
has_value('sensor.gemeten_energie_per_dag')}}
Iāve also found the is_number() test to be useful for availability templates
- unique_id: saldo_totaal_dag_energie_afname_of_levering
state: >
{{states('sensor.grid_energy_total')|float|abs }}
attributes:
phrase: >
{{'levering' if states('sensor.grid_energy_total')|float < 0 else 'afname'}}
saldo: >
{{states('sensor.grid_energy_total')}}
<<: *energy_total
availability: >
{{is_number(states('sensor.grid_energy_total'))}}
-
What is the learning of has_value filter not consistent in its output Ā· Issue #91607 Ā· home-assistant/core Ā· GitHub regarding the āhas_valueā usage?
-
According to 2023.4: Custom template macros, and many more new entity dialogs! - Home Assistant
has_valueāonlyā filtersunavailableandunknown- right? Is that really sufficient for an availability template? So far I always checked with several other states like:
{{ states('sensor.abc') not in ['NULL', 'null', 'none', 'undefined', 'unavailable', 'unknown', ''] }}
Got it. Unless @Sir_Goodenough has good arguments to check for other states
Iāll likely migrate all my availability templates to the way more simple has_value function.
If you use it in other places that for instance may not be available at startup, or that might be from a zigbee sensor that might go offline, etc.
I am actually testing ATM has_value to replace the āunknown or unavailableā part of what I had, leaving the other 3 possibles.
is_number would not work in mine as it tests booleans and about every other kind of entity.
this might be outdated by edits, but sorry I missed it, somehow I didnt get a tag.
where in my examples do I use the incorrect syntax you paste there?
In multiple places you are using:
{{ ('binary_sensor.boiler_on')|has_value }}
That is incorrect. You need to use the states() function with the has_value filter, like this:
{{ states('binary_sensor.boiler_on')|has_value }}
Only if you use the has_value() function can you do:
{{ has_value('binary_sensor.boiler_on') }}
Hmm. I canāt find the templates you say I am using .
Do seem to recall I had made that mistake before, but not in this thread ? Odd.
just checked my complete config, but dont even use a has_value in either syntax at all on the binary_sensor.boiler_on
(I just like to know, because I can then correct a fluke)
rightā¦
that is oldā¦
I was looking in this topic, and checking my contributions hereā¦
and yes, that was what I tried to remember, but couldnt find.
not sure why you would link to that though, as it wasnt part of this discussion at all?
anyways, glad weāve got that out of the way.
Yes it was, in the post I was directly replying to:
Is it possible to use a list of entities with the has_value function? or is the solution from Sir_Goodenough the best option?
I canāt believe theyād release this half-baked.
Finish the baking then
Thanks for the suggestion. I had seen availability in some examples but didnāt know what it was for.
I know itās a contrived example, but you cause the problem by using default zero in your float conversions. Leave out the zeros and the template would be unavailable when either sensor is unavailable.
state: "{{ states('sensor.one') | float() + states('sensor.two') | float() }}"
That would fill your log with errors and may disable your sensor. The availability template method wonāt.
A post was split to a new topic: Filtering utility meter

