Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Add virtual number component to Shelly pro /gen3+ devices and fetch temperature information from another Shelly device
- // Replace Shelly_IP with actual shelly device IP what run script and show temperature satatus
- //
- // 1. Create virtual group & number components
- // Write down ID from response
- http://Shelly_IP/rpc/Virtual.Add?type="group"
- http://Shelly_IP/rpc/Virtual.Add?type="number"
- // 2. Set Group name, number component type and name and assign number component to group
- // Use previous step group ID & number ID. 200 is default ID
- http://Shelly_IP/rpc/Group.SetConfig?id=200&config={"name":"Temperature"}
- http://Shelly_IP/rpc/Number.SetConfig?id=200&config={"name": "Remote temp","meta": {"ui": {"view": "label","unit": "°C","step": 0.01,"icon": null}}}
- http://Shelly_IP/rpc/Group.Set?id=200&value=["number:200"]
- // 3. Add following script to device
- // Shelly mJS script to read temperature from another Shelly device
- // and update the state of a virtual number component
- let TEMP_SENSOR_IP = "192.168.x.x"; // IP of the Shelly device providing temperature data
- let TEMP_SENSOR_ID = 100; // Sensor ID for temperature reading
- let VIRTUAL_COMP_ID = 200; // Virtual component numeric ID
- let POLL_INTERVAL = 10; // Polling interval in seconds
- // Function to fetch temperature data from the Shelly device
- function getTemperatureData() {
- Shelly.call(
- "HTTP.GET",
- { url: "http://" + TEMP_SENSOR_IP + "/rpc/Temperature.GetStatus?id=" + TEMP_SENSOR_ID },
- function (response, error_code, error_message) {
- if (error_code === 0 && response.code === 200) {
- let data = JSON.parse(response.body);
- if (data && data.tC !== undefined) {
- let tempC = data.tC;
- print("Temperature Read (°C):", tempC);
- updateVirtualComponent(tempC);
- } else {
- print("Invalid temperature data received");
- }
- } else {
- print("Error fetching temperature data: ", error_message);
- }
- }
- );
- }
- // Function to update the virtual component state
- function updateVirtualComponent(tempC) {
- Shelly.call("number.set", { id: VIRTUAL_COMP_ID, value: tempC }, function (res, error) {
- if (error) {
- print("Failed to update virtual component state. Error:", JSON.stringify(error));
- } else {
- print("Virtual component state updated successfully to:", tempC);
- }
- });
- }
- // Set up a repeating timer to fetch and update temperature data every 10 seconds
- Timer.set(POLL_INTERVAL*1000, true, getTemperatureData);
- // Initialize script
- print("Temperature monitoring started");
- getTemperatureData();
Advertisement
Add Comment
Please, Sign In to add comment