Has_value in template sensor - has_value not working?

I had sensor.upstairs_consumed_energy_2 defined as (and yes, I should use modern format)

  - platform: template
    sensors:
      upstairs_consumed_energy_2:
        friendly_name: "Upstairs Consumed Energy 2"
        value_template: >
         {% if has_value('sensor.upstairs_consumed_energy') %}
           {{ states('sensor.upstairs_consumed_energy') }}       
         {%- else -%}
             states("sensor.upstairs_consumed_energy_2")         
         {%- endif %}
        unit_of_measurement: 'kWh'
        unique_id: upstairs_consumed_energy_2
        device_class: energy

This morning, I fiddled with a access point and a switch, and after a few hours I saw this

afbeelding

resulting in this

I now have replaced the template with this :


    sensors:
      upstairs_consumed_energy_2:
        friendly_name: "Upstairs Consumed Energy 2"
        value_template: >
         {% if (states('sensor.upstairs_consumed_energy') | float(0)) > (states('sensor.upstairs_consumed_energy_2') | float(0)) %}
           {{ states('sensor.upstairs_consumed_energy') }}       
         {%- else -%}
             states('sensor.upstairs_consumed_energy_2')         
         {%- endif %}
        unit_of_measurement: 'kWh'
        unique_id: upstairs_consumed_energy_2
        device_class: energy

But why was the sensor with has_value not working ?

for complete information, source is a Tasmota device


    - name: "Upstairs Consumed Energy"
      state_topic: "electricity2/tele/RESULT"
      value_template: >- 
        {% set message = value_json.SerialReceived %}
        {% set payload = message[6:14] %}
        {% set payload_len = (payload | length) %}
        {% set result = namespace(value='') %}
      
        {% for i in range(0, payload_len + 1) | reverse -%}
          {%- if i is divisibleby 2 -%}
            {%- set result.value = result.value + payload[i:i+2] -%}
          {%- endif -%}
        {%- endfor -%}
      
        {{ (result.value|float(0)) / 100 }}
      unit_of_measurement: 'kWh'
      unique_id: "upstairs_consumed_energy"
      device_class: energy
      state_class: total_increasing
      device:
        identifiers:
          - C21FF1
        name: electricity2
        model: Generic
        sw_version: 8.4.0.3(0f1e4fc-tasmota)
        manufacturer: Tasmota
      

and house consumption is defined as

    - name: "House electricity energy"
      unique_id: '0a7476cc-d6c1-40ba-8ae1-606518c3497f'
      unit_of_measurement: 'kWh'
      state: >
        {{ ((states("sensor.mains_consumed_energy_2") | float(0)) + (states("sensor.upstairs_consumed_energy_2") | float(0)) + (states("sensor.extra_consumed_energy_2") | float(0)) )| round(2) }}       
      availability: >
        {{ not states('sensor.mains_consumed_energy_2') in  ["unavailable","unknown"] and 
          not states('sensor.upstairs_consumed_energy_2') in  ["unavailable","unknown"] and 
          not states('sensor.extra_consumed_energy_2')  in  ["unavailable","unknown"] }}        
      device_class: energy
      state_class: total_increasing  

i don’t know what’s up with the has_value… my brain cell isn’t seeing the issue.

you didn’t post what sensor.upstairs_consumed_energy’s value history was. during that period did it report unavailable?

but what i’m really posting about is this:

did you just do that as a test? or did you do that as your new alternative. if it’s your new alternative… doesn’t this only every increate? sensor 2 will never drop in value. is that what you want?

Indeed, an energy sensor should never drop in value. You can’t have used less energy then before.

1 Like

Good question.

afbeelding

image

You’re missing the curly braces, and unit_of_measurement is defined… so the string causes the sensor to fail.

3 Likes

:person_facepalming:
can’t believe i missed that.

Can’t believe I missed that. Sometimes you look over the obvious.

Somewhere deep in the recesses of the HA Facebook group there is a very similar question asked by me… :grin:

1 Like