How to convert sensor to read negative values?

I have an emporia vue and the power that is being returned to the grid from my solar reads as a positive value, so I want to try and flip that to read negative to mimic NEM as closely as possible…

for now I have this to show me the “mean” value but that’s not exactly what I want…

sensor:
- platform: min_max
  type: mean
  entity_ids:
  - sensor.mainsfromgrid_mainsfromgrid_1mon
  - sensor.mainstogrid_mainstogrid_1mon

How about a template sensor?

template:
  sensor:
    - name: "Grid Negative"
      unique_id: "Grid Negative"
      state: {{ -1 * states('sensor.mainsfromgrid_mainsfromgrid_1mon') | float(0) }}

So I have my config split up, but anytime I try to add additional things to my config, it only breaks things…
configuration.yaml


# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Text to speech
tts:
  - platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
template: !include templates.yaml
sensor: !include sensors.yaml
#binary_sensor: !include binary_sensors.yaml
#switch: !include switch.yaml

homeassistant:
  customize: !include customize.yaml

logger:
  default: info

hydrawise:
 access_token: 4D5C-73EC-0A7E-0A7E
#sensor:
#- platform: min_max
#  type: mean
#  entity_ids:
#  - sensor.mainsfromgrid_mainsfromgrid_1mon
#  - sensor.mainstogrid_mainstogrid_1mon
binary_sensor:
  - platform: hydrawise
switch:
  - platform: hydrawise

sensors.yaml

- platform: mqtt
  state_topic: "metermon/77742465"
  name: "Gas Meter Reading"
  qos: 0
  value_template: "{{ value_json.Consumption }}"
  unit_of_measurement: ft³
  device_class: "gas"

- platform: hydrawise
- platform: min_max
  entity_ids:
  - sensor.mainsfromgrid_mainsfromgrid_1mon
  - sensor.mainstogrid_mainstogrid_1mon

templates.yaml

- sensor:
    name: Gas Energy Metric
    unique_id: sensor.gas_meter_reading
    state: >-
      {{ states('sensor.gas_meter_reading')|float(0)*0.0103412 }}
      #{{ states('sensor.gas_meter_reading')|float(0)/96.7 }}
    icon: mdi:fire
    unit_of_measurement: "ft^3"
    state_class: "total_increasing"
    device_class: "gas"

So I’d just append your template into the end of my templates.yaml?

image

I was able to get it in HA but it’s showing a 0 value…

- sensor:
    name: Gas Energy Metric
    unique_id: sensor.gas_meter_reading
    state: >-
      {{ states('sensor.gas_meter_reading')|float(0)*0.0103412 }}
      #{{ states('sensor.gas_meter_reading')|float(0)/96.7 }}
    icon: mdi:fire
    unit_of_measurement: "ft^3"
    state_class: "total_increasing"
    device_class: "gas"
- sensor:
    name: "Grid Negative"
    unique_id: "Grid Negative"
    state: >-
      {{ -1 * states('MainsToGrid MainsToGrid 1MON') | float }}

That is not a entity ID. So I expect states to return None converted to float is zero and -1*0 is 0

A good way to test templates is in developer tools / templates.

Trying to add the float(0) to the end to see if that works - same issue even with float(0) - value still reads 0

Yes - Append to end of templates.yaml. You will obviously have to forgo copy and paste on ```template:````