Calculating a sensor value

I am attempting to read my tesla powerwall load power, then dividing this number (60/40) to then feed this number into my two solaredge inverters to have my solar production load follow my household load.
Just cant get the following code (in configuration.yaml) to return a value. I can see them in developer tools / states but state is always unknown.
My code seemingly is incorrect - can anyone assist with correcting the code?

template:

  • sensor:
    • name: inverter_1_load
      state: >
      {% set house_load = states(‘sensor.home_9_charles_load_power_2’) %}
      {% if house_load is number %} # Check if it’s a number
      {{ (house_load | float * 0.6) | round(0) }}
      {% elif house_load is none %} # Check if it’s None
      0 # Default value if None
      {% else %} # all other cases such as unavailable, unknown or strings
      0 # Default value if not a number
      {% endif %}
      unit_of_measurement: “W”
    • name: inverter_2_load
      state: >
      {% set house_load = states(‘sensor.home_9_charles_load_power_2’) %}
      {% if house_load is number %} # Check if it’s a number
      {{ (house_load | float * 0.4) | round(0) }}
      {% elif house_load is none %} # Check if it’s None
      0 # Default value if None
      {% else %} # all other cases such as unavailable, unknown or strings
      0 # Default value if not a number
      {% endif %}
      unit_of_measurement: “W”

Please could you paste your code as preformatted text (</> in the cogwheel menu). This makes it easy for others to copy it and try it out.

template:
  - sensor:
      - name: inverter_1_load
        state: >
          {% set house_load = states('sensor.home_9_charles_load_power_2') | float %}
          {% if house_load > 0 %}  # Avoid division by zero
            {{ (house_load * 0.6) | round(0) }}  # 60% to inverter 1 (adjust as needed)
          {% else %}
            0
          {% endif %}
        unit_of_measurement: "W"
      - name: inverter_2_load
        state: >
          {% set house_load = states('sensor.home_9_charles_load_power_2') | float %}
          {% if house_load > 0 %} # Avoid division by zero
            {{ (house_load * 0.4) | round(0) }}  # 40% to inverter 2 (adjust as needed)
          {% else %}
            0
          {% endif %}
        unit_of_measurement: "W"
  - sensor:
      - name: Ainverter_1_load
        state: >
          {% set house_load = states('sensor.home_9_charles_load_power_2') %}
          {% if house_load is number %}  # Check if it's a number
            {{ (house_load | float * 0.6) | round(0) }}
          {% elif house_load is none %} # Check if it's None
            0  # Default value if None
          {% else %} # all other cases such as unavailable, unknown or strings
            0 # Default value if not a number
          {% endif %}
        unit_of_measurement: "W"
      - name: AAinverter_2_load
        state: >
          {% set house_load = states('sensor.home_9_charles_load_power_2') %}
          {% if house_load is number %}  # Check if it's a number
            {{ (house_load | float * 0.4) | round(0) }}
          {% elif house_load is none %} # Check if it's None
            0  # Default value if None
          {% else %} # all other cases such as unavailable, unknown or strings
            0 # Default value if not a number
          {% endif %}
        unit_of_measurement: "W"type or paste code here

You cannot put YAML comments within a Jinja2 template.

{% if house_load > 0 %} # Avoid division by zero
                        ^^^^^^^^^^^^^^^^^^^^^^^^

Either remove them or convert them to Jinja2 comments.

This is a YAML comment.

# sample text

This is a Jinja2 comment.

{# sample text #}

Thank you! This has solved the unknown state issue. Now I need to work out why the sensors always have a zero value. Obviously I have very little Jinja2 experience and am learning as I go…

After much learning, and not mangling yaml and Jinja2 together it seems to work.
Working code is below for anyone interested :slight_smile:

template:
  - sensor:
      - name: inverter_1_load
        state: >
          {% set house_load_str = states('sensor.home_9_charles_load_power_2') %}
          {% if house_load_str | is_number %}
            {% set house_load = house_load_str | float %}
            {{ (((house_load * 1000 * 0.6) | round(0)) | int) }}  
          {% else %}
            0
          {% endif %}
        unit_of_measurement: "W"
      - name: inverter_2_load
        state: >
          {% set house_load_str = states('sensor.home_9_charles_load_power_2') %}
          {% if house_load_str | is_number %}
            {% set house_load = house_load_str | float %}
            {{ (((house_load * 1000 * 0.4) | round(0)) | int) }}  
          {% else %}
            0
          {% endif %}
        unit_of_measurement: "W"
        

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 indicates to others that the topic has been solved. This helps users find answers to similar questions