Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AC voltage and calibration
#23
Thanks for the good idea posted above, nickdd. 
I took that concept and wrote the nodejs code below to generate the sensor section of my yaml, it is working pretty well for me. 

This code generates the sensor and interval yaml blocks, I combine it with the base example provided by Kincony for the rest of the yaml.

Code:
const fs = require('fs');
const entries = [
  [[1,2],50,"power_downstairs_hvac_compressor","Downstairs HVAC Compressor"],
  [[3],20,"power_garage_outlets_and_fridges","Garage Outlets and Fridges"],
  [[4],20,"power_kitchen_fridge","Kitchen Fridge"],
  // this inverter/compressor has a weird phantom load which registers as high idle current
  // but since we don't measure true reactive power etc we need to just clip it.
  // Seems to read true once turned on
  [[5,6],50,"power_upstairs_hvac","Upstairs HVAC",{clamp_amps:1}],
  [[7],50, "power_left_laundry","Left Laundry"],
  [[8],20,"power_office_plugs","Office Plugs"],
  [[9],20,"power_kitchen_plugs","Kitchen Plugs 1"],
  [[10],20,"power_right_laundry","Right Laundry"],
  [[11],20,"power_dishwasher","Dishwasher"],
  [[12],20,"power_instant_hot_water_heater","Instant Hot Water Heater"],
  [[13],20,"power_kitchen_plugs_2","Kitchen Plugs 2"],
];
let conf = `

sensor:
`;
let interval_conf = `
interval:
  - interval: 10s
    then:`;
for (let i=0;i<entries.length;i++) {
  const entry = entries[i];
  const ct_list = [];
  const [ct_ids, CTAMPS, POWERID, POWERNAME, options={}] = entry;
  for (let j=0;j<ct_ids.length;j++) {
    const INDEX = ct_ids[j];
    ct_list.push(`Measured_Current_${INDEX}`);
    const ct_template = `
  - platform: ct_clamp
    sensor: ai${INDEX}
    id: Measured_Current_${INDEX}
    internal: true
    sample_duration: 500ms
    update_interval: never
    accuracy_decimals: 5   
    filters: !include { file: inc/ct${CTAMPS}.yaml }`;
    conf += ct_template;
    const ct_interval_template = `
    - lambda: |-
        id(Measured_Current_${INDEX}).update();
    - delay: 600ms`;
    interval_conf += ct_interval_template;
  }
  const opts = Object.assign({
    clamp_amps:0,
    prefix: 'Power - ',
  },options);
  const power_template = `
  - platform: template
    id: ${POWERID}
    name: "${opts.prefix}${POWERNAME}"
    lambda: if (id(${ct_list[0]}).state < ${opts.clamp_amps}) { return 0; } return (${ct_list.map(r => 'id(' + r + ').state').join(' + ')}) * 120;
    device_class: power
    accuracy_decimals: 3
    unit_of_measurement: 'W'
    update_interval: never`
  conf += power_template;
  const power_interval_template = `
    - lambda: |-
        id(${POWERID}).update();
    - delay: 25ms`;
  interval_conf += power_interval_template;
}
console.log(conf.replace(/(\s*\n){2,}/gm,'\n'));
console.log(interval_conf.replace(/(\s*\n){2,}/gm,'\n'));
const this_code = fs.readFileSync(process.argv[1]).toString();
console.log("# generated by the following code:");
console.log(this_code.replaceAll(/^(.*)/gm,'#$1'));


My ct20.yaml contains this:

Code:
- clamp:
    min_value: 0
    max_value: 20
- calibrate_linear:
    method: exact # least_squares
    datapoints:
    - 0 -> 0
    - 0.006 -> 0 # calibrated 20231213
    - 0.016 -> 0.45 # calibrated 20231213
    - 0.040 -> 1.23 # calibrated 20231213
    - 0.146 -> 5.51 # calibrated 20231213
    - 0.238 -> 7.60 # calibrated 20231213
    - 0.340 -> 10.3 # calibrated 20231213
Reply


Messages In This Thread
AC voltage and calibration - by nickdd - 12-04-2023, 06:25 PM
RE: AC voltage and calibration - by nickdd - 12-04-2023, 10:03 PM
RE: AC voltage and calibration - by admin - 12-06-2023, 06:24 AM
RE: AC voltage and calibration - by admin - 12-09-2023, 01:26 PM
RE: AC voltage and calibration - by nickdd - 12-17-2023, 10:52 PM
RE: AC voltage and calibration - by admin - 12-18-2023, 12:36 AM
RE: AC voltage and calibration - by nickdd - 12-18-2023, 02:17 PM
RE: AC voltage and calibration - by admin - 12-18-2023, 11:05 PM
RE: AC voltage and calibration - by nickdd - 12-18-2023, 11:11 PM
RE: AC voltage and calibration - by admin - 12-18-2023, 11:33 PM
RE: AC voltage and calibration - by nickdd - 12-19-2023, 09:54 AM
RE: AC voltage and calibration - by admin - 12-19-2023, 11:12 AM
RE: AC voltage and calibration - by nickdd - 12-19-2023, 03:19 PM
RE: AC voltage and calibration - by admin - 12-20-2023, 12:15 AM
RE: AC voltage and calibration - by nickdd - 12-21-2023, 09:05 PM
RE: AC voltage and calibration - by admin - 12-31-2023, 02:03 PM
RE: AC voltage and calibration - by nickdd - 01-02-2024, 04:59 PM
RE: AC voltage and calibration - by admin - 01-03-2024, 12:42 AM
RE: AC voltage and calibration - by nickdd - 01-07-2024, 10:29 AM
RE: AC voltage and calibration - by admin - 01-07-2024, 11:10 AM
RE: AC voltage and calibration - by nickdd - 01-21-2024, 11:36 PM
RE: AC voltage and calibration - by admin - 01-22-2024, 12:32 AM
RE: AC voltage and calibration - by hamilton - 02-04-2024, 06:27 PM
RE: AC voltage and calibration - by sanurss - 02-15-2024, 04:20 PM
RE: AC voltage and calibration - by nickdd - 02-15-2024, 04:58 PM
RE: AC voltage and calibration - by sanurss - 02-16-2024, 11:55 AM
RE: AC voltage and calibration - by admin - 02-16-2024, 12:11 PM
RE: AC voltage and calibration - by nickdd - 02-16-2024, 07:25 PM
RE: AC voltage and calibration - by sanurss - 02-16-2024, 07:46 PM
RE: AC voltage and calibration - by nickdd - 02-16-2024, 10:51 PM
RE: AC voltage and calibration - by sanurss - 02-17-2024, 08:11 PM
RE: AC voltage and calibration - by nickdd - 02-17-2024, 09:51 PM
RE: AC voltage and calibration - by sanurss - 02-18-2024, 07:25 AM
RE: AC voltage and calibration - by sanurss - 03-22-2024, 03:42 PM
RE: AC voltage and calibration - by admin - 03-22-2024, 11:29 PM

Forum Jump:


Users browsing this thread: