Create own sensor with calculation with 2 sensor vallues how?

Hi All,

I am trying to create a custom sensor to enable to negate a vallue.
Wow that doesn’t explain much now does it ?
I have an electricity meter (P1) entity in HA giving me allmost all vallues that I need except the current vallaues on my phases have no direction (positive when I am using from the grid negative when I am producing to the grid with my solar).

But I need this for the intergration with my Smart EVSE (open source charging station for my electric car)

What I got this far is added to my configuration.yml

template:
 - sensor:
   - name: "nettocurrent_l1"
     unit_of_measurement: A
     device_class: current
     state: >
       {{ ('sensor.electricity_meter_power_production_phase_l1' >0 - 'sensor.electricity_meter_current_phase_l1') | int }}

What I want to get going here is that vallue of “sensor.electricity_meter_current_phase_l1” is published as “nettocurrent_l1” and gets a - added (so the vallue is negative) when the vallue of “sensor.electricity_meter_power_production_phase_l1” is bigger than 0 (meaning I am producing power to the grid).

So when “sensor.electricity_meter_power_production_phase_l1” is 0w (no solar production) the vallue of “sensor.electricity_meter_current_phase_l1” should be directly translated to “nettocurrent_l1
But as soon as I start producing power and this is published in Watt on “sensor.electricity_meter_power_production_phase_l1” as for example 250watt (so bigger than zero) the vallue of “nettocurrent_l1” should be -1 Ampere

As you can see I tried but I am a complete noob at coding so this doesn’t work hope one of you great HA coding gods can help me out here.
I realy tried but I did not find any (readable) documentation that points me in the right direction.

[edit1]
Think I am getting closer

template:
 - sensor:
  - name: "nettocurrent_l1"
    unit_of_measurement: A
    device_class: current
    state: >
      {% set state = states('sensor.electricity_meter_power_production_phase_l1') %}
        {% if is_number(state) and state | float > 0 %}
           - 'sensor.electricity_meter_power_production_phase_l1'
        {% else %}
	       'sensor.electricity_meter_power_production_phase_l1' 
  
        {% endif %}
template:
  - sensor:
      - name: "nettocurrent_l1"
        unit_of_measurement: A
        device_class: current
        state: >
          {% set solar = states('sensor.electricity_meter_power_production_phase_l1') | float(0) %}
          {{ solar * -1 if solar > 0 else solar }}
        availability: "{{ states('sensor.electricity_meter_power_production_phase_l1') | is_number }}"
1 Like

Thank you sir
Allmost there, I made a misstake in my second Code and set you of on the wrong foot …

I need to check if sensor.electricity_meter_power_production_phase_l1 is bigger then zero … and then do the times -1 with the sensor output of sensor.electricity_meter_current_phase_l1.

I think this should do the trick :

template:
  - sensor:
      - name: "nettocurrent_l1"
        unit_of_measurement: A
        device_class: current
        state: >
          {% set solar = states('sensor.electricity_meter_power_production_phase_l1') | float(0) %}
		  {% set current = states('sensor.electricity_meter_current_phase_l1.') | float(0) %}
          {{ current * -1 if solar > 0 else current }}
        availability: "{{ states('sensor.electricity_meter_power_production_phase_l1') | is_number }}"

No problem; it happens.

I suggest you enhance the availability template so that it confirms the values of both sensors are numeric (and not unavailable or unknown).

template:
  - sensor:
      - name: "nettocurrent_l1"
        unit_of_measurement: A
        device_class: current
        state: >
          {% set solar = states('sensor.electricity_meter_power_production_phase_l1') | float(0) %}
		  {% set current = states('sensor.electricity_meter_current_phase_l1') | float(0) %}
          {{ current * -1 if solar > 0 else current }}
        availability: >
          {{ states('sensor.electricity_meter_power_production_phase_l1') | is_number and 
            states('sensor.electricity_meter_current_phase_l1') | is_number }}
1 Like

Thanks so far, as I need some solar production to do the final test I will let you know tomorow if it works … for now it’s looking good.

Thank you SO much your help is really appriciated !

1 Like

Hahaha really happy with this (actualy litterly makes my day getting help to solve this )
So here;s a quick snapshot of a temp dashboard showing it’s looking good here !

And then the developper in me finaly woke a bit more … I am able to test with the developper tools so no need to wait … I can just alter states manyaly … so I did …and WOW WOW WOW it works !


@123 you are my hero ! Thank you sir !

1 Like