Advertisement
Guest User

Untitled

a guest
Jan 13th, 2022
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.29 KB | None | 0 0
  1. binary_sensor:
  2. - platform: gpio
  3. id: ${switch_id}_button_1
  4. name: ${friendly_name} Left Button
  5. pin:
  6. number: 14
  7. inverted: true
  8. # on_click:
  9. # - switch.toggle: ${switch_id}_relay_1
  10. # - script.execute: nspanel_weather
  11.  
  12. - platform: gpio
  13. id: ${switch_id}_button_2
  14. name: ${friendly_name} Right Button
  15. pin:
  16. number: 27
  17. inverted: true
  18. # on_click:
  19. # - switch.toggle: ${switch_id}_relay_2
  20.  
  21. - platform: homeassistant
  22. id: light_under_bed
  23. entity_id: light.light_under_bed
  24. on_press:
  25. then:
  26. - lambda: 'id(nspanel1).send_json_command(0x86,"{\"id\":\"light7\",\"params\":{\"switch\":\"on\"}}");'
  27. on_release:
  28. then:
  29. - lambda: 'id(nspanel1).send_json_command(0x86,"{\"id\":\"light7\",\"params\":{\"switch\":\"off\"}}");'
  30. - platform: homeassistant
  31. id: cabinet_light_switch
  32. entity_id: light.cabinet_light_switch_switch_l1
  33. on_press:
  34. then:
  35. - lambda: 'id(nspanel1).send_json_command(0x86,"{\"id\":\"wled_test\",\"params\":{\"switch\":\"on\"}}");'
  36. on_release:
  37. then:
  38. - lambda: 'id(nspanel1).send_json_command(0x86,"{\"id\":\"wled_test\",\"params\":{\"switch\":\"off\"}}");'
  39. - platform: homeassistant
  40. id: original_wifi_plug
  41. entity_id: switch.original_wifi_plug
  42. on_press:
  43. then:
  44. - lambda: 'id(nspanel1).send_json_command(0x86,"{\"id\":\"plug_09\",\"params\":{\"switch\":\"on\"}}");'
  45. on_release:
  46. then:
  47. - lambda: 'id(nspanel1).send_json_command(0x86,"{\"id\":\"plug_09\",\"params\":{\"switch\":\"off\"}}");'
  48. output:
  49. - platform: ledc
  50. id: ${switch_id}_buzzer_out
  51. pin:
  52. number: 21
  53.  
  54. text_sensor:
  55. - platform: wifi_info
  56. ip_address:
  57. name: '${friendly_name} IP Address'
  58. icon: mdi:ip
  59.  
  60. - platform: template
  61. id: ${switch_id}_uptime
  62. name: "${friendly_name} час роботи"
  63. lambda: |-
  64. uint32_t uptime = (id(device_uptime).state);
  65. int minutes = (uptime % 3600) / 60;
  66. int hours = (uptime % 86400) / 3600;
  67. int days = uptime / 86400;
  68. if (days > 0) {
  69. return { (String(days) + " д." + String(hours) + " г." + String(minutes) + " хв.").c_str() };
  70. }
  71. if (hours > 0) {
  72. return { (String(hours) + " г " + String(minutes) + " хв.").c_str() };
  73. } else {
  74. return { (String(minutes) + " хв.").c_str() };
  75. }
  76. update_interval: 60s
  77. icon: mdi:clock
  78.  
  79. - platform: version
  80. id: ${switch_id}_esp_version
  81. name: '${friendly_name} ESPHome Version'
  82.  
  83. - platform: template
  84. name: "NSPanel Return String (Log)"
  85. internal: true
  86. id: nsp_return_message
  87. lambda: |-
  88. return {};
  89. update_interval: 60s
  90. #Verify JSON content in Log message, split and update Type/JSON text sensores
  91. on_value:
  92. then:
  93. - lambda: |-
  94. std::string message = id(nsp_return_message).state;
  95. std::size_t pos1 = message.find("Type");
  96. std::size_t pos2 = message.find(" PAYLOAD");
  97. std::size_t pos3 = message.find(" RAW");
  98. if (pos1 != std::string::npos && pos2 != std::string::npos && pos3 != std::string::npos)
  99. {
  100. id(nsp_return_type).publish_state(message.substr(pos1+5,pos2-pos1-5));
  101. id(nsp_return_json).publish_state(message.substr(pos2+9,pos3-pos2-9));
  102. }
  103.  
  104. - platform: template
  105. name: "NSPanel Return Type"
  106. id: nsp_return_type
  107. lambda: |-
  108. return {};
  109. update_interval: 60s
  110. - platform: template
  111. name: "NSPanel Return JSON"
  112. id: nsp_return_json
  113. lambda: |-
  114. return {};
  115. update_interval: 60s
  116. # section to take JSON messages from NSPanel (GUI button presses) and update HA
  117. on_value:
  118. then:
  119. - logger.log: "JSON text Updated"
  120. - lambda: |-
  121. std::string message = id(nsp_return_json).state;
  122. std::string type = id(nsp_return_type).state;
  123. std::string rgb_r,rgb_g,rgb_b;
  124. float brightness;
  125. DynamicJsonDocument root(2000);
  126. deserializeJson(root, message);
  127. if (type == "0x86") {
  128. // Device Type Message
  129. // Device ID Light 7
  130. if (root.containsKey("id") && strcasecmp(root["id"], "light8") == 0) {
  131. JsonObject params = root["params"];
  132. if (params.containsKey("switch")) {
  133. if(params["switch"] == "on") {
  134. id(ha_service_name).publish_state("light.turn_on");
  135. id(ha_service_entityid).publish_state("light.light_under_bed");
  136. }
  137. if(params["switch"] == "off") {
  138. id(ha_service_name).publish_state("light.turn_off");
  139. id(ha_service_entityid).publish_state("light.light_under_bed");
  140. }
  141. id(ha_service1).execute();
  142. }
  143. else if(params.containsKey("white")) {
  144. JsonObject white = params["white"];
  145. id(ha_service_name).publish_state("light.turn_on");
  146. id(ha_service_entityid).publish_state("light.light_under_bed");
  147. id(g_brightness) = int(white["br"].as<float>() / 100 * 255);
  148. id(text_g_brightness).publish_state(to_string(id(g_brightness)));
  149. id(ha_service2).execute();
  150. }
  151. }
  152. if (root.containsKey("id") && strcasecmp(root["id"], "wled_test") == 0) {
  153. JsonObject params = root["params"];
  154. if (params.containsKey("bright")) {
  155. id(ha_service_name).publish_state("script.set_light_on_rgb");
  156. id(ha_service_entityid).publish_state("light.cabinet_light_switch_switch_l1");
  157. id(g_brightness) = int(params["bright"].as<float>() / 100 * 255);
  158. id(text_g_brightness).publish_state(to_string(id(g_brightness)));
  159. id(g_rgb_r) = params["colorR"];
  160. id(g_rgb_g) = params["colorG"];
  161. id(g_rgb_b) = params["colorB"];
  162. id(ha_service3).execute();
  163. }
  164. else if (params.containsKey("switch")) {
  165. if(params["switch"] == "on") {
  166. id(ha_service_name).publish_state("light.turn_on");
  167. id(ha_service_entityid).publish_state("light.cabinet_light_switch_switch_l1");
  168. id(ha_service1).execute();
  169. }
  170. else if(params["switch"] == "off") {
  171. id(ha_service_name).publish_state("light.turn_off");
  172. id(ha_service_entityid).publish_state("light.cabinet_light_switch_switch_l1");
  173. id(ha_service1).execute();
  174. }
  175. }
  176. }
  177. // Device ID Plug_09
  178. if (root.containsKey("id") && strcasecmp(root["id"], "plug_09") == 0) {
  179. JsonObject params = root["params"];
  180. if (params.containsKey("switch")) {
  181. if(params["switch"] == "on") {
  182. id(ha_service_name).publish_state("switch.turn_on");
  183. id(ha_service_entityid).publish_state("switch.original_wifi_plug");
  184. }
  185. if(params["switch"] == "off") {
  186. id(ha_service_name).publish_state("switch.turn_off");
  187. id(ha_service_entityid).publish_state("switch.original_wifi_plug");
  188. }
  189. id(ha_service1).execute();
  190. }
  191. }
  192. } else if (type == "0x84") {
  193. // Thermostat Messages
  194. if (root.containsKey("ATCEnable") && root["ATCEnable"] == 0) {
  195. id(ha_service_name).publish_state("climate.turn_off");
  196. id(ha_service_entityid).publish_state("climate.cabinet_thermostat");
  197. id(ha_service1).execute();
  198. }
  199. else if (root.containsKey("ATCEnable") && root["ATCEnable"] == 1) {
  200. id(ha_service_name).publish_state("climate.turn_on");
  201. id(ha_service_entityid).publish_state("climate.cabinet_thermostat");
  202. id(ha_service1).execute();
  203. }
  204. if (root.containsKey("ATCExpect0")) {
  205. id(ha_service_name).publish_state("climate.set_temperature");
  206. id(ha_service_entityid).publish_state("climate.cabinet_thermostat");
  207. id(g_temperature) = root["ATCExpect0"];
  208. if (id(g_temperature) != 0) { id(ha_service4).execute();}
  209. }
  210. }
  211.  
  212. - platform: template
  213. name: "HA Service Name"
  214. id: ha_service_name
  215. lambda: |-
  216. return {};
  217. update_interval: 60s
  218. - platform: template
  219. name: "HA Service Entity"
  220. id: ha_service_entityid
  221. lambda: |-
  222. return {};
  223. update_interval: 60s
  224. - platform: template
  225. name: "g_brightness"
  226. id: "text_g_brightness"
  227. lambda: |-
  228. return {};
  229. update_interval: 60s
  230. - platform: homeassistant
  231. name: "HA wled_test RGB"
  232. id: wled_test_rgb
  233. entity_id: light.cabinet_light_switch_switch_l1
  234. attribute: rgb_color
  235. internal: false
  236. on_value:
  237. then:
  238. - script.execute: ha_wled_test_update
  239. - platform: homeassistant
  240. id: climate_state
  241. name: "Climate State"
  242. entity_id: climate.cabinet_thermostat
  243. internal: false
  244. on_value:
  245. then:
  246. - lambda: |-
  247. if (id(climate_state).state == "heat") {
  248. id(nspanel1).send_json_command(0x84,"{\"ATCEnable\":1}");
  249. } else {
  250. id(nspanel1).send_json_command(0x84,"{\"ATCEnable\":0}");
  251. }
  252.  
  253. script:
  254. # Script to trigger basic HA Service with Service Name + Entity
  255. # Service + Entity_id
  256. - id: ha_service1
  257. then:
  258. - logger.log: "HA Service Script 1 Called"
  259. - homeassistant.service:
  260. service: !lambda return id(ha_service_name).state;
  261. data:
  262. entity_id: !lambda return id(ha_service_entityid).state;
  263. # Service + Entity_id + Brightness
  264. - id: ha_service2
  265. then:
  266. - logger.log: "HA Service Script 2 Called"
  267. - homeassistant.service:
  268. service: !lambda return id(ha_service_name).state;
  269. data:
  270. entity_id: !lambda return id(ha_service_entityid).state;
  271. # brightness: !lambda 'return id(g_brightness);'
  272. # Service + Entity_id + Brightness + RGB
  273. - id: ha_service3
  274. then:
  275. - logger.log: "HA Service Script 3 Called"
  276. - homeassistant.service:
  277. service: !lambda return id(ha_service_name).state;
  278. data:
  279. entity_name: !lambda 'return id(ha_service_entityid).state;'
  280. brightness: !lambda 'return id(g_brightness);'
  281. red: !lambda 'return to_string(id(g_rgb_r));'
  282. green: !lambda 'return to_string(id(g_rgb_g));'
  283. blue: !lambda 'return to_string(id(g_rgb_b));'
  284. # Service + Entity_id + Temp (Climate Only)
  285. - id: ha_service4
  286. then:
  287. - logger.log: "HA Service Script 4 Called"
  288. - homeassistant.service:
  289. service: !lambda return id(ha_service_name).state;
  290. data:
  291. entity_id: !lambda return id(ha_service_entityid).state;
  292. temperature: !lambda 'return to_string(id(g_temperature));'
  293. hvac_mode: "heat"
  294. - id: ha_light_under_bed_update
  295. then:
  296. - lambda: |-
  297. if (id(light_under_bed).state) {
  298. int i_brightness = int(id(light_under_bed_bright).state / 255 * 100);
  299. id(nspanel1).send_json_command(0x86,"{\"id\":\"light7\",\"params\":{\"switch\":\"on\",\"ltype\":\"white\",\"white\":{\"br\":" + to_string(i_brightness) + "}}}");
  300. } else {
  301. id(nspanel1).send_json_command(0x86,"{\"id\":\"light7\",\"params\":{\"switch\":\"off\"}}");
  302. }
  303.  
  304. - id: ha_wled_test_update
  305. then:
  306. - lambda: |-
  307. if (id(cabinet_light_switch).state) {
  308. int i_brightness = int(id(wled_test_bright).state / 255 * 100);
  309. std::string s = id(wled_test_rgb).state;
  310. int p1 = s.find(",");
  311. int p2 = s.find(",",p1+1);
  312. std::string r = s.substr(1, p1-1).erase(0,s.find_first_not_of(" "));
  313. std::string g = s.substr(p1+2,p2-p1-2).erase(0,s.find_first_not_of(" "));
  314. std::string b = s.substr(p2+2,s.length()-2).erase(0,s.find_first_not_of(" "));
  315. 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) + "}}");
  316. } else {
  317. id(nspanel1).send_json_command(0x86,"{\"id\":\"wled_test\",\"params\":{\"switch\":\"off\"}}");
  318. }
  319. - id: ha_plug_09_update
  320. then:
  321. - lambda: |-
  322. if(id(original_wifi_plug).state) {
  323. id(nspanel1).send_json_command(0x86,"{\"id\":\"plug_09\",\"params\":{\"switch\":\"on\"}}");
  324. } else {
  325. id(nspanel1).send_json_command(0x86,"{\"id\":\"plug_09\",\"params\":{\"switch\":\"off\"}}");
  326. }
  327. - id: nspanel_init
  328. then:
  329. # Setup Widgets - must send all 8
  330. - lambda: |-
  331. id(nspanel1).send_json_command(0x86,"{\"HMI_resources\":[{\"index\":1,\"ctype\":\"device\",\"id\":\"light7\",\"uiid\":52}]}");
  332. 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}}]}");
  333. - lambda: |-
  334. id(nspanel1).send_json_command(0x86,"{\"HMI_resources\":[{\"index\":2,\"ctype\":\"device\",\"id\":\"wled_test\",\"uiid\":33}]}");
  335. id(nspanel1).send_json_command(0x86,"{\"relation\":[{\"ctype\":\"device\",\"id\":\"wled_test\",\"name\":\"WLED\",\"online\":1,\"params\":{\"switch\":\"on\"}]}");
  336. - lambda: |-
  337. id(nspanel1).send_json_command(0x86,"{\"HMI_resources\":[{\"index\":3,\"ctype\":\"device\",\"id\":\"plug_09\",\"uiid\":1}]}");
  338. id(nspanel1).send_json_command(0x86,"{\"relation\":[{\"ctype\":\"device\",\"id\":\"plug_09\",\"name\":\"PLUG09\",\"online\":1,\"params\":{\"switch\":\"on\"}]}");
  339. - lambda: 'id(nspanel1).send_json_command(0x86,"{\"index\":4,\"type\":\"delete\"}");'
  340. - lambda: 'id(nspanel1).send_json_command(0x86,"{\"index\":5,\"type\":\"delete\"}");'
  341. - lambda: 'id(nspanel1).send_json_command(0x86,"{\"index\":6,\"type\":\"delete\"}");'
  342. - lambda: 'id(nspanel1).send_json_command(0x86,"{\"index\":7,\"type\":\"delete\"}");'
  343. - lambda: 'id(nspanel1).send_json_command(0x86,"{\"index\":8,\"type\":\"delete\"}");'
  344. # Update existing Entities
  345. - script.execute: ha_light_under_bed_update
  346. - script.execute: ha_wled_test_update
  347. - script.execute: ha_plug_09_update
  348. # Setup Thermostat
  349. - lambda: 'id(nspanel1).send_json_command(0x84,"{\"HMI_ATCDevice\":{\"ctype\":\"device\",\"id\":\"climate\",\"outlet\":0,\"etype\":\"hot\"}}");'
  350. - lambda: 'id(nspanel1).send_json_command(0x84,"{\"ATCMode\":0}");'
  351. # Update Thermostat On/Off
  352. - lambda: |-
  353. if (id(climate_state).state == "heat") {
  354. id(nspanel1).send_json_command(0x84,"{\"ATCEnable\":1}");
  355. } else {
  356. id(nspanel1).send_json_command(0x84,"{\"ATCEnable\":0}");
  357. }
  358. # Update Thermostat Temperature
  359. - lambda: |-
  360. if ( (int) id(climate_temp).state > 15 && (int) id(climate_temp).state < 32) {
  361. id(nspanel1).send_json_command(0x84,"{\"ATCExpect0\":" + to_string((int) id(climate_temp).state) + "}");
  362. }
  363. sensor:
  364.  
  365. - platform: homeassistant
  366. id: climate_temp
  367. name: Climate Temp
  368. entity_id: climate.cabinet_thermostat
  369. attribute: temperature
  370. internal: false
  371. on_value:
  372. then:
  373. - lambda: |-
  374. if ((int) id(climate_temp).state > 15 && (int) id(climate_temp).state < 32) {
  375. id(nspanel1).send_json_command(0x84,"{\"ATCExpect0\":" + to_string((int) id(climate_temp).state) + "}");
  376. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement