Calculate solar power self-consumption

Calculate solar power self-consumption

  • name: “solarstrom_eigenverbrauch”
    unique_id: solarstrom_eigenverbrauch
    unit_of_measurement: “W”
    state: >
    “{% if states(‘sensor.tasmota_power_power_curr’) | float < 0 %}
    {% set state = states(‘sensor.solarstrom_energy_power’) %}
    {% else %}
    {% set state = (states(‘sensor.solarstrom_energy_power’) +
    states(‘sensor.tasmota_power_power_curr’)) %}
    {% endif %}”

Doesn’t work. Why?
image

Remove the outer quotes, because > in YAML already make it a string. You only need quotes on the same line.

Also, format your code, please. YAML is sensitive to spacing and important information will go missing. Check the forum guidelines for more info.

Link to the formatting guidelines:

name: solarstrom_eigenverbrauch
unique_id: solarstrom_eigenverbrauch
unit_of_measurement: W
state: >
  {% if states('sensor.tasmota_power_power_curr') | float < 0 %}
    {{ states('sensor.solarstrom_energy_power') }}
  {% else %}
    {{ states('sensor.solarstrom_energy_power') + states('sensor.tasmota_power_power_curr') }}
  {% endif %}

Thank you for help, it works:

name: "solarstrom_eigenverbrauch"
unique_id: solarstrom_eigenverbrauch
unit_of_measurement: "W"
state: >
  {% if states('sensor.tasmota_power_power_curr') | float > 0 %}
    {{ states('sensor.solarstrom_energy_power') }}
  {% else %}
    {{ (states('sensor.solarstrom_energy_power') | int) + (states('sensor.tasmota_power_power_curr') | int) }}
  {% endif %}