Assistance crafting custom template sensors

Hi All

My solar inverter integration has an entity called sensor.power_meter_active_power reports positive values if the system is importing power from the grid, and negative if its sending power to the grid.

Similarly, my battery integration has an entity sensor.battery_charge_discharge_power which reports positive values when charging the battery, and negative values when the battery is discharging.

I want to split out these two entities out into four entities:

New entity name                                 |       Unit        |   Function
------------------------------------------------|-------------------|-----------------------------------
sensor.power_meter_active_power_grid_export     |       Watts       |   Power exported to grid
sensor.power_meter_active_power_grid_import     |       Watts       |   Power imported from grid
sensor.battery_charge                           |       Watts       |   Power drawn from battery
sensor.battery_discharge                        |       Watts       |   Power used to charge battery

To accomplish this, I set up these entities (via the documentation from the Tesla style solar power card installed via HACS):

      battery_discharging_power:
        value_template: '{% set batt_dischg = sensor.battery_charge_discharge_power | int %}
                        {% if batt_dischg > 0 %}
                            {{ batt_dischg | int }}
                        {% else %}
                            0
                        {% endif %}'
        device_class: power
        unit_of_measurement: W
        friendly_name: Battery Discharging Power
        icon_template: mdi:battery-arrow-down-outline
      battery_charging_power:
        value_template: '{% set batt_chg = sensor.battery_charge_discharge_power | int %}
                        {% if batt_chg < 0 %}
                            {{ batt_chg | int }}
                        {% else %}
                            0
                        {% endif %}'
        device_class: power
        unit_of_measurement: W
        friendly_name: Battery Charging Power
        icon_template: mdi:battery-arrow-up
      power_meter_exported_to_grid:
        value_template: '{% set grid_export = sensor.power_meter_active_power | int %}
                        {% if grid_export < 0 %}
                            {{ grid_export | int }}
                        {% else %}
                            0
                        {% endif %}'
        device_class: power
        unit_of_measurement: W
        friendly_name: Power Exported to Grid
        icon_template: mdi:home-lightning-bolt
      power_meter_imported_from_grid:
        value_template: '{% set grid_import = sensor.power_meter_active_power | int %}
                        {% if grid_import > 0 %}
                            {{ grid_import | int }}
                        {% else %}
                            0
                        {% endif %}'
        device_class: power
        unit_of_measurement: W
        friendly_name: Power Imported from Grid
        icon_template: mdi:transmission-tower-import

However, all entities show unknown. No syntax errors turn up. What am I doing wrong.

There are a number of issues with your templates:

  • Multi-line templates should not be quoted, only single line templates require this (see the availability template syntax below).
  • You need to indicate it is a multi-line template with >
  • You need to get the state of the entity, not just the entity_id.
  • You should specify a default value when converting the state (string) to a number.
  • Use the |abs (absolute value) filter to convert a negative number to a positive one.
  • You should supply an availability template in case the state can not be converted to a number.
  • You should use the new template sensor format.

So for the first templates,

template:
  - sensor:
      - name: Battery Discharging Power
        state: >
          {% set batt_dischg = states('sensor.battery_charge_discharge_power')|float(0) %}
          {% if batt_dischg < 0 %}
            {{ batt_dischg|abs }}
          {% else %}
            0
          {% endif %}
        availability: "{{ states('sensor.battery_charge_discharge_power')|is_number }}"
        unit_of_measurement: W
        device_class: power
        state_class: measurement

      - name: Battery Charging Power
        state: >
          {% set batt_chg = states('sensor.battery_charge_discharge_power')|float(0) %}
          {% if batt_dischg > 0 %}
            {{ batt_chg }}
          {% else %}
            0
          {% endif %}
        availability: "{{ states('sensor.battery_charge_discharge_power')|is_number }}"
        unit_of_measurement: W
        device_class: power
        state_class: measurement

      - name: Imported Grid Power
        state: > ...etc

See if you can apply that to the grid templates. Ask if you have trouble with it.

1 Like