Trying to create a sensor for Power to and from grid biased on usage

Trying to calculate a diffidence and sensor between two sensors. Energy production - Energy usage = Energy to Grid. It works in template under developer tools but cant make it work in configuration.YAML, After hours of research, this is what I have. I understand that there has been a change to formatting and I am new, so the research may be flawed. Please Take a look and see where I am going wrong. I am providing the template and results. This is exactly why I never got into programing. The rules change constantly.

Template:

#Calculate Grid Power
sensor
platform template
 sensors
    grid_power
     frendly name "Grid Power
     device class "power"
      value_template >
       {{(states('sensor.energy_production') | float - states('sensor.energy_usage') | float) | round(0)}}

Results:

#Calculat Grid Power
sensor
platform template
  sensors
    grid_power
      frendly name "Grid Power
      device class "power"
      value_template >
        -430

configuration.yaml

template:
  - sensor:
      - name "Grid Power
        device_class "power"
        state_class: measurement 
        state: "{{ ( states('sensor.energy_production') | float(0) - states('sensor.energy_usage') | float(0) ) | round(0) }}"
        availability: "{{ states('sensor.energy_production') | is_number and states('sensor.energy_usage') | is_number }}"

Pay close attention to the indentation and punctuation (particularly _ and : ) they are important.

1 Like

Thank you, It didn’t work at first but after a little crash coarse in formatting, I finally got it working.