-Teme-

poll temperature

Mar 6th, 2025
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. // Add virtual number component to Shelly pro /gen3+ devices and fetch temperature information from another Shelly device
  2. // Replace Shelly_IP with actual shelly device IP what run script and show temperature satatus
  3. //
  4. // 1. Create virtual group & number components
  5. // Write down ID from response
  6.  
  7. http://Shelly_IP/rpc/Virtual.Add?type="group"
  8. http://Shelly_IP/rpc/Virtual.Add?type="number"
  9.  
  10. // 2. Set Group name, number component type and name and assign number component to group
  11. // Use previous step group ID & number ID. 200 is default ID
  12.  
  13. http://Shelly_IP/rpc/Group.SetConfig?id=200&config={"name":"Temperature"}
  14. http://Shelly_IP/rpc/Number.SetConfig?id=200&config={"name": "Remote temp","meta": {"ui": {"view": "label","unit": "°C","step": 0.01,"icon": null}}}
  15. http://Shelly_IP/rpc/Group.Set?id=200&value=["number:200"]
  16.  
  17. // 3. Add following script to device
  18.  
  19. // Shelly mJS script to read temperature from another Shelly device
  20. // and update the state of a virtual number component
  21.  
  22. let TEMP_SENSOR_IP = "192.168.x.x"; // IP of the Shelly device providing temperature data
  23. let TEMP_SENSOR_ID = 100; // Sensor ID for temperature reading
  24. let VIRTUAL_COMP_ID = 200; // Virtual component numeric ID
  25. let POLL_INTERVAL = 10; // Polling interval in seconds
  26.  
  27. // Function to fetch temperature data from the Shelly device
  28. function getTemperatureData() {
  29. Shelly.call(
  30. "HTTP.GET",
  31. { url: "http://" + TEMP_SENSOR_IP + "/rpc/Temperature.GetStatus?id=" + TEMP_SENSOR_ID },
  32. function (response, error_code, error_message) {
  33. if (error_code === 0 && response.code === 200) {
  34. let data = JSON.parse(response.body);
  35. if (data && data.tC !== undefined) {
  36. let tempC = data.tC;
  37. print("Temperature Read (°C):", tempC);
  38. updateVirtualComponent(tempC);
  39. } else {
  40. print("Invalid temperature data received");
  41. }
  42. } else {
  43. print("Error fetching temperature data: ", error_message);
  44. }
  45. }
  46. );
  47. }
  48.  
  49. // Function to update the virtual component state
  50. function updateVirtualComponent(tempC) {
  51. Shelly.call("number.set", { id: VIRTUAL_COMP_ID, value: tempC }, function (res, error) {
  52. if (error) {
  53. print("Failed to update virtual component state. Error:", JSON.stringify(error));
  54. } else {
  55. print("Virtual component state updated successfully to:", tempC);
  56. }
  57. });
  58. }
  59.  
  60. // Set up a repeating timer to fetch and update temperature data every 10 seconds
  61. Timer.set(POLL_INTERVAL*1000, true, getTemperatureData);
  62.  
  63. // Initialize script
  64. print("Temperature monitoring started");
  65. getTemperatureData();
  66.  
Advertisement
Add Comment
Please, Sign In to add comment