How can I request and import cgi data as a sensor

How can I request and retrieve CGI data from a local url and show it as a sensor in HA?

http://192.168.1.196/Dev_status.cgi?&Port=0

{"devstatus": {
"Sys_Time": 1730723351,
"Sys_Batt_V": 54.4,
"ports": [
{ "Port": 1, "Dev": "FX","Type": "120V","Inv_I": 19,"Chg_I": 0,"Buy_I": 0,"Sell_I": 19,"VAC_in": 125,"VAC_out": 124,"Batt_V": 54.4,"AC_mode": "AC USE","INV_mode": "Sell","Warn": ["none"],"Error": ["none"],"AUX": "disabled"},
{ "Port": 2, "Dev": "CC","Type": "FM80","Out_I": 53.7,"In_I": 34,"Batt_V": 54.4,"In_V": 89.0,"Out_kWh": 8.3,"Out_AH": 153,"CC_mode": "Bulk  ","Error": ["none"],"Aux_mode": "Manual","AUX": "disabled"}
]}}

@tom_l Thanks! Iā€™ve been looing at this and I cant seem to pull in the information. Iā€™m not getting any errors in HA Iā€™m probably missing something small can you see any this I may have missed?

rest:
  - scan_interval: 120
    resource: http://192.168.1.196/Dev_status.cgi?&Port=0
    sensor:
      - name: Watts
        value_template: "{{ value_json['Out_kWh'] }}"
        unit_of_measurement: "w"

sensor:
  - platform: rest
    resource: http://192.168.1.196/Dev_status.cgi?&Port=0
    name: Watts2
    value_template: "{{ value_json['Out_kWh'] }}"
    scan_interval: 120

You need the full json path to the data you are interested in:

rest:
  - scan_interval: 120
    resource: http://192.168.1.196/Dev_status.cgi?&Port=0
    sensor:
      - name: Watts
        value_template: "{{ value_json.devstatus.ports[1].Out_kWh }}"
        unit_of_measurement: "w"

Use this tool: https://jsonpathfinder.com/ to find other data value paths. You can add as many sensors as you want to that rest integration. They will all be populated with the one call to the resource.

YOUR THE MAN!
Thank you so much!

1 Like

Small suggestion, something more descriptive than this would be worthwhile, especially as you add more sensors later.

name: Watts

e.g.

name: <Location> <Device> Power 

Also this should be a capital W

unit_of_measurement: "w"

Thanks for the advice. Two more questions for you. How do I bring in more data point? I tried this and nothing. Second how do I delete the old sensors out that where created when I was doing the trial and error?

rest:
  - scan_interval: 120
    resource: http://192.168.1.196/Dev_status.cgi?&Port=0
    sensor:
      - name: Daily KWH
        value_template: "{{ value_json.devstatus.ports[1].Out_kWh }}"
        unit_of_measurement: "kWh"
      - name: Amps
        value_template: "{{ value_json.devstatus.ports[1].Sell_I }}"
        unit_of_measurement: "AAC"
      - name: Volts
        value_template: "{{ value_json.devstatus.ports[1].VAC_in }}"
        unit_of_measurement: "V"
``

Use that link I provided to work out the paths to the data you want. Also be careful with your units.

rest:
  - scan_interval: 120
    resource: http://192.168.1.196/Dev_status.cgi?&Port=0
    sensor:
      - name: Daily Energy
        value_template: "{{ value_json.devstatus.ports[1].Out_kWh }}"
        unit_of_measurement: "kWh"
        state_class: total_increasing
        device_class: energy
      - name: Current
        value_template: "{{ value_json.devstatus.ports[0].Sell_I }}"
        unit_of_measurement: "A"
        state_class: measurement
        device_class: current
      - name: Voltage
        value_template: "{{ value_json.devstatus.ports[0].VAC_in }}"
        unit_of_measurement: "V"
        state_class: measurement
        device_class: voltage

State and device classes can be helpful. Particularly for energy sensors.

I think I figured out how to delete them. Settings/Entities

Awesome! I tried playing around with the link, I couldnā€™t get a grip on it, Iā€™ll try to see if there is and youtube tutorials I see it could be me a useful tools. Thanks again for your help!!

1 Like

Step 3: ā€œvalue_jsonā€ not ā€œjalue_jsonā€ :roll_eyes:

Note this tool does not work well on mobile devices.

1 Like

Well thatā€™s easy!!

1 Like

How come ā€˜restā€™ doesnā€™t like ā€˜idā€™? Iā€™m trying to create a sensor off that data.

rest:
  - scan_interval: 120
    resource: http://192.168.1.196/Dev_status.cgi?&Port=0
    sensor:
      - name: Solar Daily Energy
        id: SolarDailyEnergy
        value_template: "{{ value_json.devstatus.ports[1].Out_kWh }}"
        unit_of_measurement: "kWh"
        state_class: total_increasing
        device_class: energy
      - name: Solar Current
        id: SolarCurrent
        value_template: "{{ value_json.devstatus.ports[0].Sell_I }}"
        unit_of_measurement: "A"
        state_class: measurement
        device_class: current
      - name: Solar Voltage 
        id: SolarVoltage
        value_template: "{{ value_json.devstatus.ports[0].VAC_in }}"
        unit_of_measurement: "V"
        state_class: measurement
        device_class: voltage
      - Name: Solor Producing
        id: SolorProducing
        lambda: return id(SolarVoltage).state * id(HouseL2Watts).state ;
        accuracy_decimals: 1
        unit_of_measurement: "W"
        state_class: measurement 
unique_id: SolarDailyEnergy
1 Like

The ā€˜restā€™ is not liking the lambda or template. How do I build this the custom sensor?

lambda: return id(SolarVoltage).state * id(SolarCurrent).state ;
rest:
  - scan_interval: 120
    resource: http://192.168.1.196/Dev_status.cgi?&Port=0
    sensor:
      - name: Solar Daily Energy
        unique_id: SolarDailyEnergy
        value_template: "{{ value_json.devstatus.ports[1].Out_kWh }}"
        unit_of_measurement: "kWh"
        state_class: total_increasing
        device_class: energy
      - name: Solar Current
        unique_id: SolarCurrent
        value_template: "{{ value_json.devstatus.ports[0].Sell_I }}"
        unit_of_measurement: "A"
        state_class: measurement
        device_class: current
      - name: Solar Voltage 
        unique_id: SolarVoltage
        value_template: "{{ value_json.devstatus.ports[0].VAC_in }}"
        unit_of_measurement: "V"
        state_class: measurement
        device_class: voltage
    #   - name: Solar Producing
    #     unique_id: SolarProducing
    #   - platform: template
    #     lambda: return id(SolarVoltage).state * id(SolarCurrent).state ;
    #     accuracy_decimals: 1
    #     unit_of_measurement: "W"
    #     state_class: measurement
        
sensor:
  - platform: template
    name: Solar Producing
    unique_id: SolarProducing
    lambda: return id(SolarVoltage).state * id(SolarCurrent).state ;
    accuracy_decimals: 1
    unit_of_measurement: "W"
    state_class: measurement

HA does not use lambdas. It uses jinja templates.

It also has a newer integration that you should use instead of the old sensor platform:

configuration.yaml

template:
  - sensor:
      -  name: Solar Producing
         unique_id: SolarProducing
         state: "{{ states('sensor.solar_voltage')|float * states('sensor.solar_current')|float }}"
         availability: "{{ has_value('sensor.solar_voltage') and has_value('sensor.solar_current') }}"
         unit_of_measurement: "W"
         state_class: measurement
         device_class: power
1 Like

Hey @tom_l,
I have another question for you. How come I canā€™t use a template sensor in HA Energy dash board? The CGI data does not give me currant watts it only give me accumulating total and thatā€™s does work right, that why we needed to make that template V*A=W. Is there something I need to do to use those sensors in the Energy Dash Board? I believe I have the state and device class correct.

rest:
  - scan_interval: 60
    resource: http://192.168.1.196/Dev_status.cgi?&Port=0
    sensor:
      - name: Solar Daily Energy
        unique_id: SolarDailyEnergy
        value_template: "{{ value_json.devstatus.ports[1].Out_kWh }}"
        unit_of_measurement: "kWh"
        state_class: total_increasing
        device_class: energy

template:
  - sensor:
      -  name: Solar Producing
         unique_id: SolarProducing
         state: "{{ states('sensor.solar_voltage')|float * states('sensor.solar_current')|float }}"
         availability: "{{ has_value('sensor.solar_voltage') and has_value('sensor.solar_current') }}"
         unit_of_measurement: "kWh"
         state_class: total_increasing
         device_class: energy

Opps. My mistake.

That is a power sensor. Not an energy sensor. The Energy dashboard only uses energy sensors. So the template should be:

template:
  - sensor:
      -  name: Solar Producing
         unique_id: SolarProducing
         state: "{{ states('sensor.solar_voltage')|float * states('sensor.solar_current')|float }}"
         availability: "{{ has_value('sensor.solar_voltage') and has_value('sensor.solar_current') }}"
         unit_of_measurement: "W"
         state_class: measurement
         device_class: power

Then feed that to an integral helper to get energy.

Be careful with the method option. You need to set it correctly to minimise approximation errors. Only use trapezoidal method for smoothly changing sensor values. Otherwise use method: left.

1 Like