Advertisement
gio_aggiustatutto

Example code

Oct 14th, 2024
1,202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 8.60 KB | None | 0 0
  1. #This is just an example of how the code should look like in the end.
  2.  
  3. esphome:
  4.   name: energy-meter
  5.   friendly_name: Energy meter
  6.  
  7. esp32:
  8.   board: esp32dev
  9.   framework:
  10.     type: arduino
  11.  
  12. # Enable logging
  13. logger:
  14. # Enable Home Assistant API
  15. api:
  16.   encryption:
  17.     key: "xxx"
  18.  
  19. ota:
  20.   password: "xxx"
  21.  
  22. wifi:
  23.   ssid: wifi_ssid
  24.   password: wifi_password
  25.  
  26.   manual_ip:
  27.    # Set this to the IP of the ESP
  28.     static_ip: 192.168.1.97
  29.     # Set this to the IP address of the router. Often ends with .1
  30.     gateway: 192.168.1.1
  31.     # The subnet of the network. 255.255.255.0 works for most home networks.
  32.     subnet: 255.255.255.0
  33.  
  34.   # Enable fallback hotspot (captive portal) in case wifi connection fails
  35.   ap:
  36.     ssid: "Energy-Meter Fallback Hotspot"
  37.     password: "password"
  38.  
  39. captive_portal:
  40. #JSY energy meter protocol
  41. uart:
  42.   id: mod_bus
  43.   tx_pin: 17
  44.   rx_pin: 16
  45.   baud_rate: 4800
  46.   stop_bits: 1
  47.  
  48. modbus:
  49.   id: modbus1
  50.  
  51. modbus_controller:
  52.   - id: jsymk
  53.     #modbus device address
  54.     address: 0x1
  55.     modbus_id: modbus1
  56.     update_interval: 12s
  57.     command_throttle: 50ms
  58.  
  59. i2c:
  60.   sda: 21
  61.   scl: 22
  62.  
  63. font:
  64.   - file:
  65.       type: gfonts
  66.       family: Baloo+Bhaijaan+2
  67.       weight: 500
  68.     id: baloo_18_500
  69.     size: 18
  70.  
  71.   - file:
  72.       type: gfonts
  73.       family: Baloo+Bhaijaan+2
  74.       weight: 700
  75.     id: baloo_32_700
  76.     size: 32
  77.  
  78.   - file:
  79.       type: gfonts
  80.       family: Baloo+Bhaijaan+2
  81.       weight: 700
  82.     id: baloo_18_700
  83.     size: 18
  84.  
  85.   - file: "gfonts://Material+Symbols+Outlined"
  86.     id: icons_18
  87.     size: 18
  88.     glyphs: ["\ue63e", "\ue648"] # mdi-wifi, mdi-wifi-off
  89.  
  90. display:
  91.   - platform: ssd1306_i2c
  92.     model: "SSD1306 128x64"
  93.     address: 0x3C
  94.     id: oled
  95.     rotation: 180°
  96.     lambda: |-
  97.       it.print(-1, -4, id(baloo_18_500), "Energy Meter");
  98.  
  99.       if(id(connection_status).state == 1) {
  100.         it.print(it.get_width(), 0, id(icons_18), TextAlign::TOP_RIGHT, "\ue63e");        
  101.       }
  102.       else {
  103.         it.print(it.get_width(), 0, id(icons_18), TextAlign::TOP_RIGHT, "\ue648");
  104.       }
  105.  
  106.       it.printf(it.get_width()/2, 38, id(baloo_32_700), TextAlign::CENTER, "%.1f W", id(power1).state);
  107.  
  108.       it.printf(0, it.get_height() + 12, id(baloo_18_700), TextAlign::BOTTOM_LEFT, "%.1f V", id(voltage1).state);
  109.  
  110.       it.printf(it.get_width(), it.get_height() + 12, id(baloo_18_700), TextAlign::BOTTOM_RIGHT, "%.1f A", id(current1).state);
  111.  
  112.  
  113. binary_sensor:
  114. # Connection status
  115.   - platform: status
  116.     name: "Status"
  117.     id: connection_status
  118.  
  119. sensor:
  120. # First channel voltage
  121.   - platform: modbus_controller
  122.     modbus_controller_id: jsymk
  123.     id: voltage1
  124.     name: "Voltage"
  125.     icon: mdi:alpha-v-box
  126.     device_class: energy
  127.     address: 0x0048
  128.     unit_of_measurement: "V"
  129.     register_type: holding
  130.     value_type: U_DWORD
  131.     accuracy_decimals: 1
  132.     filters:
  133.       - multiply: 0.0001
  134.     register_count: 1
  135.     response_size: 4
  136.  
  137. # First channel current
  138.   - platform: modbus_controller
  139.     modbus_controller_id: jsymk
  140.     id: current1
  141.     name: "Current 1"
  142.     icon: mdi:current-ac
  143.     device_class: energy
  144.     address: 0x0049
  145.     unit_of_measurement: "A"
  146.     register_type: holding
  147.     value_type: U_DWORD
  148.     accuracy_decimals: 1
  149.     filters:
  150.       - multiply: 0.0001
  151.     register_count: 1
  152.     response_size: 4
  153.  
  154. # First channel power
  155.   - platform: modbus_controller
  156.     modbus_controller_id: jsymk
  157.     id: power1
  158.     name: "Power 1"
  159.     icon: mdi:lightning-bolt
  160.     device_class: energy
  161.     address: 0x004A
  162.     unit_of_measurement: "W"
  163.     register_type: holding
  164.     value_type: U_DWORD
  165.     accuracy_decimals: 1
  166.     filters:
  167.       - multiply: 0.0001
  168.     register_count: 1
  169.     response_size: 4
  170.  
  171. # First channel energy
  172.   - platform: modbus_controller
  173.     modbus_controller_id: jsymk
  174.     id: energy1
  175.     name: "Energy 1"
  176.     icon: mdi:lightning-bolt
  177.     device_class: energy
  178.     address: 0x004B
  179.     unit_of_measurement: "kWh"
  180.     state_class: total
  181.     register_type: holding
  182.     value_type: U_DWORD
  183.     accuracy_decimals: 1
  184.     filters:
  185.       - multiply: 0.0001
  186.     register_count: 1
  187.     response_size: 4
  188.  
  189. # First channel power factor
  190.   - platform: modbus_controller
  191.     modbus_controller_id: jsymk
  192.     id: powerfactor1
  193.     name: "Power Factor 1"
  194.     icon: mdi:alpha-p-box
  195.     device_class: energy
  196.     address: 0x004C
  197.     register_type: holding
  198.     value_type: U_DWORD
  199.     accuracy_decimals: 1
  200.     filters:
  201.       - multiply: 0.001
  202.     register_count: 1
  203.     response_size: 4
  204.  
  205. # First channel negative active energy
  206.   - platform: modbus_controller
  207.     modbus_controller_id: jsymk
  208.     id: negativeactiveenergy1
  209.     #name: "Negative Active Energy 1"
  210.     icon: mdi:lightning-bolt
  211.     device_class: energy
  212.     address: 0x004D
  213.     unit_of_measurement: "kWh"
  214.     state_class: total
  215.     register_type: holding
  216.     value_type: U_DWORD
  217.     accuracy_decimals: 1
  218.     filters:
  219.       - multiply: 0.0001
  220.     register_count: 1
  221.     response_size: 4
  222.  
  223. # First channel current direction
  224.   - platform: modbus_controller
  225.     modbus_controller_id: jsymk
  226.     id: direction1
  227.     #name: "Current Direction 1"
  228.     icon: mdi:arrow-right
  229.     device_class: energy
  230.     address: 0x004E
  231.     register_type: holding
  232.     value_type: U_DWORD
  233.     accuracy_decimals: 0
  234.     bitmask: 0X01000000
  235.     filters:
  236.       - multiply: 1
  237.     register_count: 1
  238.     response_size: 4
  239.  
  240. # Frequency
  241.   - platform: modbus_controller
  242.     modbus_controller_id: jsymk
  243.     id: frequency
  244.     name: "Frequency"
  245.     icon: mdi:sine-wave
  246.     device_class: energy
  247.     address: 0x004F
  248.     unit_of_measurement: "Hz"
  249.     register_type: holding
  250.     value_type: U_DWORD
  251.     accuracy_decimals: 1
  252.     filters:
  253.       - multiply: 0.01
  254.     register_count: 1
  255.     response_size: 4
  256.  
  257. # Second channel voltage
  258.   - platform: modbus_controller
  259.     modbus_controller_id: jsymk
  260.     id: voltage2
  261.     #name: "Voltage 2"
  262.     icon: mdi:alpha-v-box
  263.     device_class: energy
  264.     address: 0x0050
  265.     unit_of_measurement: "V"
  266.     register_type: holding
  267.     value_type: U_DWORD
  268.     accuracy_decimals: 1
  269.     filters:
  270.       - multiply: 0.0001
  271.     register_count: 1
  272.     response_size: 4
  273.  
  274. # Second channel current
  275.   - platform: modbus_controller
  276.     modbus_controller_id: jsymk
  277.     id: current2
  278.     name: "Current 2"
  279.     icon: mdi:current-ac
  280.     device_class: energy
  281.     address: 0x0051
  282.     unit_of_measurement: "A"
  283.     register_type: holding
  284.     value_type: U_DWORD
  285.     accuracy_decimals: 1
  286.     filters:
  287.       - multiply: 0.0001
  288.     register_count: 1
  289.     response_size: 4
  290.  
  291. # Second channel power
  292.   - platform: modbus_controller
  293.     modbus_controller_id: jsymk
  294.     id: power2
  295.     name: "Power 2"
  296.     icon: mdi:lightning-bolt
  297.     device_class: energy
  298.     address: 0x0052
  299.     unit_of_measurement: "W"
  300.     register_type: holding
  301.     value_type: U_DWORD
  302.     accuracy_decimals: 1
  303.     filters:
  304.       - multiply: 0.0001
  305.     register_count: 1
  306.     response_size: 4
  307.  
  308. # Second channel energy
  309.   - platform: modbus_controller
  310.     modbus_controller_id: jsymk
  311.     id: energy2
  312.     name: "Energy 2"
  313.     icon: mdi:lightning-bolt
  314.     device_class: energy
  315.     address: 0x0053
  316.     unit_of_measurement: "kWh"
  317.     state_class: total
  318.     register_type: holding
  319.     value_type: U_DWORD
  320.     accuracy_decimals: 1
  321.     filters:
  322.       - multiply: 0.0001
  323.     register_count: 1
  324.     response_size: 4
  325.  
  326. # Second channel power factor
  327.   - platform: modbus_controller
  328.     modbus_controller_id: jsymk
  329.     id: powerfactor2
  330.     name: "Power Factor 2"
  331.     icon: mdi:alpha-p-box
  332.     device_class: energy
  333.     address: 0x0054
  334.     register_type: holding
  335.     value_type: U_DWORD
  336.     accuracy_decimals: 1
  337.     filters:
  338.       - multiply: 0.001
  339.     register_count: 1
  340.     response_size: 4
  341.  
  342. # Second channel negative active energy
  343.   - platform: modbus_controller
  344.     modbus_controller_id: jsymk
  345.     id: negativeactiveenergy2
  346.     #name: "Negative Active Energy 2"
  347.     icon: mdi:lightning-bolt
  348.     device_class: energy
  349.     address: 0x0055
  350.     unit_of_measurement: "kWh"
  351.     state_class: total
  352.     register_type: holding
  353.     value_type: U_DWORD
  354.     accuracy_decimals: 1
  355.     filters:
  356.       - multiply: 0.0001
  357.     register_count: 1
  358.     response_size: 4
  359.  
  360. # Second channel current direction
  361.   - platform: modbus_controller
  362.     modbus_controller_id: jsymk
  363.     id: direction2
  364.     #name: "Current Direction 2"
  365.     icon: mdi:arrow-right
  366.     device_class: energy
  367.     address: 0x004E
  368.     register_type: holding
  369.     value_type: U_DWORD
  370.     accuracy_decimals: 0
  371.     bitmask: 0X00010000
  372.     filters:
  373.       - multiply: 1
  374.     register_count: 1
  375.     response_size: 4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement