From: David Woodhouse Date: Wed, 6 Sep 2023 16:18:53 +0000 (+0100) Subject: Make scripts for sending values to Domoticz, send on startup too X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=521663cae77bf0ee1d7f9d0027d7c6158d1bd929;p=users%2Fdwmw2%2Fesp32-pool.git Make scripts for sending values to Domoticz, send on startup too A bunch of switches are reset to a default state on startup; make sure we tell Domoticz about it. --- diff --git a/pool.yaml b/pool.yaml index d6d3ce0..16cb839 100644 --- a/pool.yaml +++ b/pool.yaml @@ -113,6 +113,34 @@ script: done = true; } + # Publish a value to Domoticz as an nvalue + - id: tell_domo_nvalue + mode: queued + parameters: + udevice: int + nvalue: int + then: + lambda: |- + id(mqtt_client).publish_json("domoticz/in", [=](JsonObject root) { + root["command"] = "udevice"; + root["idx"] = udevice; + root["nvalue"] = nvalue; + }); + + # Publish a value to Domoticz as an svalue + - id: tell_domo_svalue + mode: queued + parameters: + udevice: int + svalue: string + then: + lambda: |- + id(mqtt_client).publish_json("domoticz/in", [=](JsonObject root) { + root["command"] = "udevice"; + root["idx"] = udevice; + root["svalue"] = svalue; + }); + globals: - id: solar_in_fails # Number of minutes without a reading. type: int @@ -194,13 +222,14 @@ mqtt: password: !secret pool_mqtt_pw id: mqtt_client on_connect: - then: - - mqtt.publish_json: - topic: domoticz/in - payload: |- - root["command"] = "udevice"; - root["idx"] = 512; - root["nvalue"] = id(valve_output).state ? 1 : 0; + then: + - delay: 2s # Too soon and the first messages don't get through! + - lambda: |- + id(tell_domo_nvalue)->execute(512, id(valve_output).state); + id(tell_domo_nvalue)->execute(515, id(pool_light).state); + id(tell_domo_nvalue)->execute(516, id(patio_light).state); + id(tell_domo_nvalue)->execute(517, id(shed_fan).state); + id(tell_domo_nvalue)->execute(518, id(pool_pump).state); on_json_message: - topic: domoticz/out @@ -253,30 +282,16 @@ mqtt: auto cumulative = x["electricitymeter"]["energy"]["import"]["cumulative"]; int kwh, watts; if (cumulative) { - // { "command" : "udevice", "idx" : 29, "svalue" : $cumulative } kwh = (cumulative.as() * 1000.0f) + 0.1f; - id(mqtt_client).publish_json("domoticz/in", [=](JsonObject root) { - root["command"] = "udevice"; - root["idx"] = 29; - root["svalue"] = std::to_string(kwh); - }); + id(tell_domo_svalue)->execute(29, std::to_string(kwh)); } auto power = x["electricitymeter"]["power"]["value"]; if (power) { watts = (power.as() * 1000.0f) + 0.1f; - // { "command" : "udevice", "idx" : 25, "svalue" : $power } - id(mqtt_client).publish_json("domoticz/in", [=](JsonObject root) { - root["command"] = "udevice"; - root["idx"] = 24; - root["svalue"] = std::to_string(watts); - }); + id(tell_domo_svalue)->execute(24, std::to_string(watts)); } if (cumulative && power) { - id(mqtt_client).publish_json("domoticz/in", [=](JsonObject root) { - root["command"] = "udevice"; - root["idx"] = 514; - root["svalue"] = std::to_string(watts) + ";" + std::to_string(kwh); - }); + id(tell_domo_svalue)->execute(514, std::to_string(watts) + ";" + std::to_string(kwh)); } dallasng: @@ -381,11 +396,7 @@ sensor: #### Template sensor as their values are publish from a lambda or the BL then: lambda: |- id(solar_in_fails) = 0; - id(mqtt_client).publish_json("domoticz/in", [=](JsonObject root) { - root["command"] = "udevice"; - root["idx"] = 507; - root["svalue"] = std::to_string(x); - }); + id(tell_domo_svalue)->execute(507, std::to_string(x)); id(control_valve)->execute(); - platform: dallasng @@ -396,11 +407,7 @@ sensor: #### Template sensor as their values are publish from a lambda or the BL then: - lambda: |- id(solar_out_fails) = 0; - id(mqtt_client).publish_json("domoticz/in", [=](JsonObject root) { - root["command"] = "udevice"; - root["idx"] = 508; - root["svalue"] = std::to_string(x); - }); + id(tell_domo_svalue)->execute(508, std::to_string(x)); id(control_valve)->execute(); # - platform: dallasng @@ -439,11 +446,7 @@ sensor: #### Template sensor as their values are publish from a lambda or the BL on_value: then: lambda: |- - id(mqtt_client).publish_json("domoticz/in", [=](JsonObject root) { - root["command"] = "udevice"; - root["idx"] = 510; - root["svalue"] = std::to_string(x); - }); + id(tell_domo_svalue)->execute(510, std::to_string(x)); - platform: template name: "BLE-YC01 ORP" @@ -463,11 +466,7 @@ sensor: #### Template sensor as their values are publish from a lambda or the BL on_value: then: lambda: |- - id(mqtt_client).publish_json("domoticz/in", [=](JsonObject root) { - root["command"] = "udevice"; - root["idx"] = 511; - root["svalue"] = std::to_string(x); - }); + id(tell_domo_svalue)->execute(511, std::to_string(x)); - platform: template name: "BLE-YC01 battery" @@ -480,11 +479,7 @@ sensor: #### Template sensor as their values are publish from a lambda or the BL on_value: then: lambda: |- - id(mqtt_client).publish_json("domoticz/in", [=](JsonObject root) { - root["command"] = "udevice"; - root["idx"] = 519; - root["svalue"] = std::to_string(x); - }); + id(tell_domo_svalue)->execute(519, std::to_string(x)); - platform: template name: "BLE-YC01 CL" @@ -496,11 +491,7 @@ sensor: #### Template sensor as their values are publish from a lambda or the BL on_value: then: lambda: |- - id(mqtt_client).publish_json("domoticz/in", [=](JsonObject root) { - root["command"] = "udevice"; - root["idx"] = 509; - root["svalue"] = std::to_string(x); - }); + id(tell_domo_svalue)->execute(509, std::to_string(x)); - platform: ble_client type: rssi @@ -606,21 +597,17 @@ switch: #### To switch on and off the communication with the BLE device #### on_turn_on: then: - light.turn_on: green_led - - mqtt.publish_json: - topic: domoticz/in - payload: |- - root["command"] = "udevice"; - root["idx"] = 512; - root["nvalue"] = 1; + - script.execute: + id: tell_domo_nvalue + udevice: 512 + nvalue: 1 on_turn_off: then: - light.turn_off: green_led - - mqtt.publish_json: - topic: domoticz/in - payload: |- - root["command"] = "udevice"; - root["idx"] = 512; - root["nvalue"] = 0; + - script.execute: + id: tell_domo_nvalue + udevice: 512 + nvalue: 0 - platform: gpio id: power_1w @@ -642,20 +629,16 @@ switch: #### To switch on and off the communication with the BLE device #### inverted: true on_turn_on: then: - - mqtt.publish_json: - topic: domoticz/in - payload: |- - root["command"] = "udevice"; - root["idx"] = 516; - root["nvalue"] = 1; + - script.execute: + id: tell_domo_nvalue + udevice: 516 + nvalue: 1 on_turn_off: then: - - mqtt.publish_json: - topic: domoticz/in - payload: |- - root["command"] = "udevice"; - root["idx"] = 516; - root["nvalue"] = 0; + - script.execute: + id: tell_domo_nvalue + udevice: 516 + nvalue: 0 - platform: gpio id: pool_pump @@ -665,20 +648,16 @@ switch: #### To switch on and off the communication with the BLE device #### inverted: true on_turn_on: then: - - mqtt.publish_json: - topic: domoticz/in - payload: |- - root["command"] = "udevice"; - root["idx"] = 518; - root["nvalue"] = 1; + - script.execute: + id: tell_domo_nvalue + udevice: 518 + nvalue: 1 on_turn_off: then: - - mqtt.publish_json: - topic: domoticz/in - payload: |- - root["command"] = "udevice"; - root["idx"] = 518; - root["nvalue"] = 0; + - script.execute: + id: tell_domo_nvalue + udevice: 518 + nvalue: 0 - platform: gpio id: pool_light @@ -688,20 +667,16 @@ switch: #### To switch on and off the communication with the BLE device #### inverted: true on_turn_on: then: - - mqtt.publish_json: - topic: domoticz/in - payload: |- - root["command"] = "udevice"; - root["idx"] = 515; - root["nvalue"] = 1; + - script.execute: + id: tell_domo_nvalue + udevice: 515 + nvalue: 1 on_turn_off: then: - - mqtt.publish_json: - topic: domoticz/in - payload: |- - root["command"] = "udevice"; - root["idx"] = 515; - root["nvalue"] = 0; + - script.execute: + id: tell_domo_nvalue + udevice: 515 + nvalue: 0 - platform: gpio id: shed_fan @@ -711,17 +686,13 @@ switch: #### To switch on and off the communication with the BLE device #### inverted: true on_turn_on: then: - - mqtt.publish_json: - topic: domoticz/in - payload: |- - root["command"] = "udevice"; - root["idx"] = 517; - root["nvalue"] = 1; + - script.execute: + id: tell_domo_nvalue + udevice: 517 + nvalue: 1 on_turn_off: then: - - mqtt.publish_json: - topic: domoticz/in - payload: |- - root["command"] = "udevice"; - root["idx"] = 517; - root["nvalue"] = 0; + - script.execute: + id: tell_domo_nvalue + udevice: 517 + nvalue: 0