Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- binary_sensor:
- - platform: gpio
- id: ${switch_id}_button_1
- name: ${friendly_name} Left Button
- pin:
- number: 14
- inverted: true
- # on_click:
- # - switch.toggle: ${switch_id}_relay_1
- # - script.execute: nspanel_weather
- - platform: gpio
- id: ${switch_id}_button_2
- name: ${friendly_name} Right Button
- pin:
- number: 27
- inverted: true
- # on_click:
- # - switch.toggle: ${switch_id}_relay_2
- - platform: homeassistant
- id: light_under_bed
- entity_id: light.light_under_bed
- on_press:
- then:
- - lambda: 'id(nspanel1).send_json_command(0x86,"{\"id\":\"light7\",\"params\":{\"switch\":\"on\"}}");'
- on_release:
- then:
- - lambda: 'id(nspanel1).send_json_command(0x86,"{\"id\":\"light7\",\"params\":{\"switch\":\"off\"}}");'
- - platform: homeassistant
- id: cabinet_light_switch
- entity_id: light.cabinet_light_switch_switch_l1
- on_press:
- then:
- - lambda: 'id(nspanel1).send_json_command(0x86,"{\"id\":\"wled_test\",\"params\":{\"switch\":\"on\"}}");'
- on_release:
- then:
- - lambda: 'id(nspanel1).send_json_command(0x86,"{\"id\":\"wled_test\",\"params\":{\"switch\":\"off\"}}");'
- - platform: homeassistant
- id: original_wifi_plug
- entity_id: switch.original_wifi_plug
- on_press:
- then:
- - lambda: 'id(nspanel1).send_json_command(0x86,"{\"id\":\"plug_09\",\"params\":{\"switch\":\"on\"}}");'
- on_release:
- then:
- - lambda: 'id(nspanel1).send_json_command(0x86,"{\"id\":\"plug_09\",\"params\":{\"switch\":\"off\"}}");'
- output:
- - platform: ledc
- id: ${switch_id}_buzzer_out
- pin:
- number: 21
- text_sensor:
- - platform: wifi_info
- ip_address:
- name: '${friendly_name} IP Address'
- icon: mdi:ip
- - platform: template
- id: ${switch_id}_uptime
- name: "${friendly_name} час роботи"
- lambda: |-
- uint32_t uptime = (id(device_uptime).state);
- int minutes = (uptime % 3600) / 60;
- int hours = (uptime % 86400) / 3600;
- int days = uptime / 86400;
- if (days > 0) {
- return { (String(days) + " д." + String(hours) + " г." + String(minutes) + " хв.").c_str() };
- }
- if (hours > 0) {
- return { (String(hours) + " г " + String(minutes) + " хв.").c_str() };
- } else {
- return { (String(minutes) + " хв.").c_str() };
- }
- update_interval: 60s
- icon: mdi:clock
- - platform: version
- id: ${switch_id}_esp_version
- name: '${friendly_name} ESPHome Version'
- - platform: template
- name: "NSPanel Return String (Log)"
- internal: true
- id: nsp_return_message
- lambda: |-
- return {};
- update_interval: 60s
- #Verify JSON content in Log message, split and update Type/JSON text sensores
- on_value:
- then:
- - lambda: |-
- std::string message = id(nsp_return_message).state;
- std::size_t pos1 = message.find("Type");
- std::size_t pos2 = message.find(" PAYLOAD");
- std::size_t pos3 = message.find(" RAW");
- if (pos1 != std::string::npos && pos2 != std::string::npos && pos3 != std::string::npos)
- {
- id(nsp_return_type).publish_state(message.substr(pos1+5,pos2-pos1-5));
- id(nsp_return_json).publish_state(message.substr(pos2+9,pos3-pos2-9));
- }
- - platform: template
- name: "NSPanel Return Type"
- id: nsp_return_type
- lambda: |-
- return {};
- update_interval: 60s
- - platform: template
- name: "NSPanel Return JSON"
- id: nsp_return_json
- lambda: |-
- return {};
- update_interval: 60s
- # section to take JSON messages from NSPanel (GUI button presses) and update HA
- on_value:
- then:
- - logger.log: "JSON text Updated"
- - lambda: |-
- std::string message = id(nsp_return_json).state;
- std::string type = id(nsp_return_type).state;
- std::string rgb_r,rgb_g,rgb_b;
- float brightness;
- DynamicJsonDocument root(2000);
- deserializeJson(root, message);
- if (type == "0x86") {
- // Device Type Message
- // Device ID Light 7
- if (root.containsKey("id") && strcasecmp(root["id"], "light8") == 0) {
- JsonObject params = root["params"];
- if (params.containsKey("switch")) {
- if(params["switch"] == "on") {
- id(ha_service_name).publish_state("light.turn_on");
- id(ha_service_entityid).publish_state("light.light_under_bed");
- }
- if(params["switch"] == "off") {
- id(ha_service_name).publish_state("light.turn_off");
- id(ha_service_entityid).publish_state("light.light_under_bed");
- }
- id(ha_service1).execute();
- }
- else if(params.containsKey("white")) {
- JsonObject white = params["white"];
- id(ha_service_name).publish_state("light.turn_on");
- id(ha_service_entityid).publish_state("light.light_under_bed");
- id(g_brightness) = int(white["br"].as<float>() / 100 * 255);
- id(text_g_brightness).publish_state(to_string(id(g_brightness)));
- id(ha_service2).execute();
- }
- }
- if (root.containsKey("id") && strcasecmp(root["id"], "wled_test") == 0) {
- JsonObject params = root["params"];
- if (params.containsKey("bright")) {
- id(ha_service_name).publish_state("script.set_light_on_rgb");
- id(ha_service_entityid).publish_state("light.cabinet_light_switch_switch_l1");
- id(g_brightness) = int(params["bright"].as<float>() / 100 * 255);
- id(text_g_brightness).publish_state(to_string(id(g_brightness)));
- id(g_rgb_r) = params["colorR"];
- id(g_rgb_g) = params["colorG"];
- id(g_rgb_b) = params["colorB"];
- id(ha_service3).execute();
- }
- else if (params.containsKey("switch")) {
- if(params["switch"] == "on") {
- id(ha_service_name).publish_state("light.turn_on");
- id(ha_service_entityid).publish_state("light.cabinet_light_switch_switch_l1");
- id(ha_service1).execute();
- }
- else if(params["switch"] == "off") {
- id(ha_service_name).publish_state("light.turn_off");
- id(ha_service_entityid).publish_state("light.cabinet_light_switch_switch_l1");
- id(ha_service1).execute();
- }
- }
- }
- // Device ID Plug_09
- if (root.containsKey("id") && strcasecmp(root["id"], "plug_09") == 0) {
- JsonObject params = root["params"];
- if (params.containsKey("switch")) {
- if(params["switch"] == "on") {
- id(ha_service_name).publish_state("switch.turn_on");
- id(ha_service_entityid).publish_state("switch.original_wifi_plug");
- }
- if(params["switch"] == "off") {
- id(ha_service_name).publish_state("switch.turn_off");
- id(ha_service_entityid).publish_state("switch.original_wifi_plug");
- }
- id(ha_service1).execute();
- }
- }
- } else if (type == "0x84") {
- // Thermostat Messages
- if (root.containsKey("ATCEnable") && root["ATCEnable"] == 0) {
- id(ha_service_name).publish_state("climate.turn_off");
- id(ha_service_entityid).publish_state("climate.cabinet_thermostat");
- id(ha_service1).execute();
- }
- else if (root.containsKey("ATCEnable") && root["ATCEnable"] == 1) {
- id(ha_service_name).publish_state("climate.turn_on");
- id(ha_service_entityid).publish_state("climate.cabinet_thermostat");
- id(ha_service1).execute();
- }
- if (root.containsKey("ATCExpect0")) {
- id(ha_service_name).publish_state("climate.set_temperature");
- id(ha_service_entityid).publish_state("climate.cabinet_thermostat");
- id(g_temperature) = root["ATCExpect0"];
- if (id(g_temperature) != 0) { id(ha_service4).execute();}
- }
- }
- - platform: template
- name: "HA Service Name"
- id: ha_service_name
- lambda: |-
- return {};
- update_interval: 60s
- - platform: template
- name: "HA Service Entity"
- id: ha_service_entityid
- lambda: |-
- return {};
- update_interval: 60s
- - platform: template
- name: "g_brightness"
- id: "text_g_brightness"
- lambda: |-
- return {};
- update_interval: 60s
- - platform: homeassistant
- name: "HA wled_test RGB"
- id: wled_test_rgb
- entity_id: light.cabinet_light_switch_switch_l1
- attribute: rgb_color
- internal: false
- on_value:
- then:
- - script.execute: ha_wled_test_update
- - platform: homeassistant
- id: climate_state
- name: "Climate State"
- entity_id: climate.cabinet_thermostat
- internal: false
- on_value:
- then:
- - lambda: |-
- if (id(climate_state).state == "heat") {
- id(nspanel1).send_json_command(0x84,"{\"ATCEnable\":1}");
- } else {
- id(nspanel1).send_json_command(0x84,"{\"ATCEnable\":0}");
- }
- script:
- # Script to trigger basic HA Service with Service Name + Entity
- # Service + Entity_id
- - id: ha_service1
- then:
- - logger.log: "HA Service Script 1 Called"
- - homeassistant.service:
- service: !lambda return id(ha_service_name).state;
- data:
- entity_id: !lambda return id(ha_service_entityid).state;
- # Service + Entity_id + Brightness
- - id: ha_service2
- then:
- - logger.log: "HA Service Script 2 Called"
- - homeassistant.service:
- service: !lambda return id(ha_service_name).state;
- data:
- entity_id: !lambda return id(ha_service_entityid).state;
- # brightness: !lambda 'return id(g_brightness);'
- # Service + Entity_id + Brightness + RGB
- - id: ha_service3
- then:
- - logger.log: "HA Service Script 3 Called"
- - homeassistant.service:
- service: !lambda return id(ha_service_name).state;
- data:
- entity_name: !lambda 'return id(ha_service_entityid).state;'
- brightness: !lambda 'return id(g_brightness);'
- red: !lambda 'return to_string(id(g_rgb_r));'
- green: !lambda 'return to_string(id(g_rgb_g));'
- blue: !lambda 'return to_string(id(g_rgb_b));'
- # Service + Entity_id + Temp (Climate Only)
- - id: ha_service4
- then:
- - logger.log: "HA Service Script 4 Called"
- - homeassistant.service:
- service: !lambda return id(ha_service_name).state;
- data:
- entity_id: !lambda return id(ha_service_entityid).state;
- temperature: !lambda 'return to_string(id(g_temperature));'
- hvac_mode: "heat"
- - id: ha_light_under_bed_update
- then:
- - lambda: |-
- if (id(light_under_bed).state) {
- int i_brightness = int(id(light_under_bed_bright).state / 255 * 100);
- id(nspanel1).send_json_command(0x86,"{\"id\":\"light7\",\"params\":{\"switch\":\"on\",\"ltype\":\"white\",\"white\":{\"br\":" + to_string(i_brightness) + "}}}");
- } else {
- id(nspanel1).send_json_command(0x86,"{\"id\":\"light7\",\"params\":{\"switch\":\"off\"}}");
- }
- - id: ha_wled_test_update
- then:
- - lambda: |-
- if (id(cabinet_light_switch).state) {
- int i_brightness = int(id(wled_test_bright).state / 255 * 100);
- std::string s = id(wled_test_rgb).state;
- int p1 = s.find(",");
- int p2 = s.find(",",p1+1);
- std::string r = s.substr(1, p1-1).erase(0,s.find_first_not_of(" "));
- std::string g = s.substr(p1+2,p2-p1-2).erase(0,s.find_first_not_of(" "));
- std::string b = s.substr(p2+2,s.length()-2).erase(0,s.find_first_not_of(" "));
- id(nspanel1).send_json_command(0x86,"{\"id\":\"wled_test\",\"params\":{\"switch\":\"on\",\"light_type\":1,\"mode\":1,\"bright\":" + to_string(i_brightness) + ",\"colorR\":" + to_string(r) + ",\"colorG\":" + to_string(g) + ",\"colorB\":" + to_string(b) + "}}");
- } else {
- id(nspanel1).send_json_command(0x86,"{\"id\":\"wled_test\",\"params\":{\"switch\":\"off\"}}");
- }
- - id: ha_plug_09_update
- then:
- - lambda: |-
- if(id(original_wifi_plug).state) {
- id(nspanel1).send_json_command(0x86,"{\"id\":\"plug_09\",\"params\":{\"switch\":\"on\"}}");
- } else {
- id(nspanel1).send_json_command(0x86,"{\"id\":\"plug_09\",\"params\":{\"switch\":\"off\"}}");
- }
- - id: nspanel_init
- then:
- # Setup Widgets - must send all 8
- - lambda: |-
- id(nspanel1).send_json_command(0x86,"{\"HMI_resources\":[{\"index\":1,\"ctype\":\"device\",\"id\":\"light7\",\"uiid\":52}]}");
- id(nspanel1).send_json_command(0x86,"{\"relation\":[{\"ctype\":\"device\",\"id\":\"light7\",\"name\":\"UnderBAD\",\"online\":1,\"params\":{\"switch\":\"off\",\"ltype\":\"white\",\"white\":{\"br\":0,\"ct\":0}}]}");
- - lambda: |-
- id(nspanel1).send_json_command(0x86,"{\"HMI_resources\":[{\"index\":2,\"ctype\":\"device\",\"id\":\"wled_test\",\"uiid\":33}]}");
- id(nspanel1).send_json_command(0x86,"{\"relation\":[{\"ctype\":\"device\",\"id\":\"wled_test\",\"name\":\"WLED\",\"online\":1,\"params\":{\"switch\":\"on\"}]}");
- - lambda: |-
- id(nspanel1).send_json_command(0x86,"{\"HMI_resources\":[{\"index\":3,\"ctype\":\"device\",\"id\":\"plug_09\",\"uiid\":1}]}");
- id(nspanel1).send_json_command(0x86,"{\"relation\":[{\"ctype\":\"device\",\"id\":\"plug_09\",\"name\":\"PLUG09\",\"online\":1,\"params\":{\"switch\":\"on\"}]}");
- - lambda: 'id(nspanel1).send_json_command(0x86,"{\"index\":4,\"type\":\"delete\"}");'
- - lambda: 'id(nspanel1).send_json_command(0x86,"{\"index\":5,\"type\":\"delete\"}");'
- - lambda: 'id(nspanel1).send_json_command(0x86,"{\"index\":6,\"type\":\"delete\"}");'
- - lambda: 'id(nspanel1).send_json_command(0x86,"{\"index\":7,\"type\":\"delete\"}");'
- - lambda: 'id(nspanel1).send_json_command(0x86,"{\"index\":8,\"type\":\"delete\"}");'
- # Update existing Entities
- - script.execute: ha_light_under_bed_update
- - script.execute: ha_wled_test_update
- - script.execute: ha_plug_09_update
- # Setup Thermostat
- - lambda: 'id(nspanel1).send_json_command(0x84,"{\"HMI_ATCDevice\":{\"ctype\":\"device\",\"id\":\"climate\",\"outlet\":0,\"etype\":\"hot\"}}");'
- - lambda: 'id(nspanel1).send_json_command(0x84,"{\"ATCMode\":0}");'
- # Update Thermostat On/Off
- - lambda: |-
- if (id(climate_state).state == "heat") {
- id(nspanel1).send_json_command(0x84,"{\"ATCEnable\":1}");
- } else {
- id(nspanel1).send_json_command(0x84,"{\"ATCEnable\":0}");
- }
- # Update Thermostat Temperature
- - lambda: |-
- if ( (int) id(climate_temp).state > 15 && (int) id(climate_temp).state < 32) {
- id(nspanel1).send_json_command(0x84,"{\"ATCExpect0\":" + to_string((int) id(climate_temp).state) + "}");
- }
- sensor:
- - platform: homeassistant
- id: climate_temp
- name: Climate Temp
- entity_id: climate.cabinet_thermostat
- attribute: temperature
- internal: false
- on_value:
- then:
- - lambda: |-
- if ((int) id(climate_temp).state > 15 && (int) id(climate_temp).state < 32) {
- id(nspanel1).send_json_command(0x84,"{\"ATCExpect0\":" + to_string((int) id(climate_temp).state) + "}");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement