MQTT sensor always unknown

This is my configuration.yaml file, I am having this issue, the calculated value of fv_power_total is always -1, this because the value of fv_batt_power is always “unknown”, but the same sensor is normally updated inside HASSIO, I can see the values on my pages… I don’t understund why… thanks.

# Loads default set of integrations. Do not remove.
default_config:

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

# ==============================================================================
# MQTT Sensors (Riceve dati da MQTT)
# ==============================================================================
mqtt:
    #broker: localhost        // questi settaggi sono stati inseriti in automatico in MQTT config dal sistema
    #username: mqtt_user      // il 03-11-2022, in seguito ad un aggiornamento del modulo mqtt. 
    #password: xxxxxx         // queste righe sono state deprecate in seguito all'aggiornamento.
    sensor:
        - name: "FV battery Voltage"
          unique_id: fv_batt_voltage
          state_topic: "fvBatteryEM/fvBattVoltage"
          qos: 2
          unit_of_measurement: "V"
          value_template: "{{ value | float  | round (2) }}"
          
        - name: "FV battery current"
          unique_id: fv_batt_current
          state_topic: "fvBatteryEM/fvBattCurrent"
          qos: 2
          unit_of_measurement: "A"
          value_template: "{{ value | float  | round (2) }}"
          
        - name: "FV battery Power"
          unique_id: fv_batt_power
          state_topic: "fvBatteryEM/fvBattPower"
          qos: 2
          unit_of_measurement: "W"
          value_template: "{{ value | float  | round (2) }}"
          
        - name: "FV battery energy charged"
          unique_id: fv_batt_energy_charged
          state_topic: "fvBatteryEM/fvBattEnergyCharged"
          qos: 2
          unit_of_measurement: "kWh"
          value_template: "{{ value | float  | round (3) }}"
        
        - name: "FV battery energy discharged"
          unique_id: fv_batt_energy_discharged
          state_topic: "fvBatteryEM/fvBattEnergyDischarged"
          qos: 2
          unit_of_measurement: "kWh"
          value_template: "{{ value | float  | round (3) }}"
          
        - name: "FV battery SOC"
          unique_id: fv_batt_SOC
          state_topic: "fvBatteryEM/fvBattSOC"
          qos: 2
          unit_of_measurement: "kWh"
          value_template: "{{ value | float  | round (2) }}"

# ==============================================================================
# Calcolo energia giornaliera FV ed ENEL
# ==============================================================================
utility_meter:
    fv_energy_daily:
      name: "FV Energy Daily"
      unique_id: fv_energy_daily
      source: sensor.fv_energy
      cycle: daily

    enel_energy_daily:
      name: "ENEL Energy Daily"
      unique_id: enel_energy_daily
      source: sensor.enel_energy
      cycle: daily

# ==============================================================================
# Calcolo somma energia FV+ENEL= ENERGIA TOTALE
# ==============================================================================
template:
    sensor:
        # Potenza totale che entra a casa inverter + ENEL
        - name: "Total Power"
          unique_id: total_power
          unit_of_measurement: "W"
          state: >
            {% set enel_power = states('sensor.enel_power') | float %}
            {% set fv_power = states('sensor.fv_power') | float %}
            {{ enel_power + fv_power }}
        
        - name: "Total Energy"
          unique_id: total_energy
          unit_of_measurement: "kWh"
          state: >
            {% set enel_energy = states('sensor.enel_energy') | float %}
            {% set fv_energy = states('sensor.fv_energy') | float %}
            {{ enel_energy + fv_energy }}
        
        - name: "FV Energy total %"
          unique_id: fv_energy_total_perc
          unit_of_measurement: "%"
          state: >
            {% set fv_energy = states('sensor.fv_energy') | float %}
            {% set total_energy = states('sensor.total_energy') | float %}
            {{ (fv_energy / total_energy * 100) | round(2) }}

        - name: "FV Energy daily (%)"
          unique_id: fv_energy_daily_perc
          unit_of_measurement: "%"
          state: >
            {% set fv_energy_daily = states('sensor.fv_energy_daily') | float %}
            {% set enel_energy_daily = states('sensor.enel_energy_daily') | float %}
            {{ (fv_energy_daily / (fv_energy_daily + enel_energy_daily) * 100) | round(2) }}

        - name: "FV Totale"
          unique_id: fv_power_total
          unit_of_measurement: "W"
          state: >
            {% set fv_power = states('sensor.fv_power') | float %}
            {% set fv_batt_power = states('sensor.fv_batt_power') %}
            {% if fv_batt_power != 'unknown' %}
                {% set fv_batt_power = fv_batt_power | float %}
                {{ (fv_power - fv_batt_power) | round(2) }}
            {% else %}
                -1
            {% endif %}

The problem is only in the last template sensor

In the menu, go to Developer Tools > States and find sensor.fv_batt_power

What is its current state value?

Goodmorning, thanks to your question I solved my problem…
The value in Developer Tools > States was a correct value cominq from MQTT (ex. 800 Watt, etc) , but the problem was that for some strange reason the name of the variable in tools\state was:
“sensor.fv_battery_power”
While in my configuration.yaml file the name was:
“sensor.fv_batt_power”
The solution was:

  1. comment all the MQTT lines in configuration.yaml
  2. delete all sensors (sensor.fv_battery_xxx) in Settings/device and services/entities
  3. Re-enable commented lines in configuration.yaml
    Obviously rebooting after each operation.
    At the end now i have the same name (ex. fv_battery_power) both in configuration.yaml and in HASSIO sensors names…
    Thanks !

@emidale75 , good you got it solved.

Can you please mark your post as the solution so nobody waste his time/energy to try to help you and others might benefit from this topic.

Why do you say it’s strange? It’s literally what you entered into your configuration.yaml as shown in your post above:

It clearly shows that the sensor’s name is “FV battery Power” which Home Assistant uses to create the sensor’s entity_id which is:

sensor.fv_battery_power

Not according to the example you posted above. This line in your sensor’s configuration is not used to create the sensor’s entity_id:

unique_id: fv_batt_power

Hi Taras,
thank youfor your answer,
I think i don’t know enought about the way HASSIO uses names and unique_id…
so if I well undestund if I create a sensor named “My Sensor Number One” (look capital letters…) the automatic unique_id HASSIO generates will be “my_sensor_number_one” ?
If it is true, maybe i did some errors in my sensors declarations…
What appens if , for example, the name is like “My value %” ? what is the unique_id created in this case ?
If this is the way HASSIO works, maybe it is better to use simpler names and NOT declare unique_id explicitly…
isn’t it ?

Maybe i understood my great mistake !

  1. i did not understund that the unique_id is generater from friendly name changing spaces with “_”
  2. I used the special character % in some sensor names , and it was removed…
    for example i had:
    “FV Power %” → generates → sensor.fv_power
    “FV Power % AVG” → generates → sensor.fv_power_2 (instead of fv_power_avg)

No.

As I explained above, Home Assistant will use the value of name to automatically generate the sensor’s entity_id (not unique_id).

It will automatically generate a value for entity_id and it will be:

sensor.my_value

Although unique_id is optional, it serves to uniquely identify the entity. So if later you decide to change the entity’s entity_id, internally Home Assistant understands it’s the same entity (because its unique_id is unchanged).

Correct. As I explained above, Home Assistant uses the value of name to generate a value for entity_id. The resulting value is limited to lowercase alphabetic characters, numbers, and underscores. Any other kinds of characters (accented, symbols, spaces, etc) are converted to underscores.

Perfect, I understand a lot, thank you. the last thing I don’t understand is the necessity to have unique_id, since the entity_id I immagine is already unique…
Thanks

A car has a unique identifier created by the manufacturer (Vehicle Identification Number). When the car is purchased and registered by the owner, it gets a license plate displaying another identifier.

Over the years, the car may be sold to new owners and get new license plates with different identifiers. However, its VIN remains unchanged. The car’s history is easily tracked by its VIN, as opposed to its license plate which may change over time.

In Home Assistant, unique_id is like the VIN. entity_id is like the license plate.

The difference between an entity and the car metaphor is that an entity isn’t obligated to have a unique_id. Without a unique_id, an entity’s entity_id can serve to identify it. However, should you ever decide to rename the entity’s entity_id, its recorded history is no longer associated with the renamed entity.


EDIT

Here’s more information about unique_id.

1 Like

Thank you again for your answer!
I only cannot figure in which situation should I need to change the entity_id of ex. a temperature sensor that remains the same retaining it’s unique id…
Moreover, If I create a template sensor that is calculated from other sensors, what I use in the calculations is the unique or the entity I’d ?
Can they be the same ?
Thanks again for your help.

It depends on the user’s needs. For example, at some future date they may want to adopt a different naming scheme for their entities

You reference an entity by its entity_id.

Yes, but they don’t have to be.

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

2 Likes

@emidale75

You appear to have misunderstood the purpose of the Solution tag.

Only one post in the entire topic can be marked with the Solution tag and it should contain the answer/solution to the question/problem in the topic’s first post.

The purpose of the Solution tag is to guide other users to the post that best serves to answer/solve the question/problem.

At one point you had marked one of my posts with the Solution tag then changed it to a post that contains no information for explaining why the sensor’s value is unknown. That doesn’t serve to help other users.

The answer is, as I have explained in several posts, you have confused the usage of an entity’s unique_id and entity_id option. You were referencing the entity by the wrong option.