You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The current custom sensor I have has some interval 1 second, while I created another custom sensor for public ip address as below, but I need interval 200. How to achieve this ?
import requests
from typing import List
def get_public_ip() -> str:
try:
# Use an external service to fetch the public IP address
response = requests.get('https://api64.ipify.org?format=json')
response.raise_for_status() # Ensure the request was successful
ip_address = response.json()['ip']
return ip_address
except requests.RequestException:
return "No IP found" # Handle any issues with fetching the public IP
class PUBLICIP(CustomDataSource):
last_val = [math.nan] * 10
def as_string(self) -> str:
ip_address = get_public_ip()
if ip_address:
return f"{ip_address}"
return "No IP found"
def last_values(self) -> List[float]:
return self.last_val
def as_numeric(self) -> float:
# Implementation of the required 'as_numeric' abstract method
ip_address = get_public_ip()
# Convert the public IP to a numeric representation or return a default value.
try:
# Example: Convert the IP to numeric by summing its parts if it's IPv4.
ip_parts = ip_address.split(".")
return sum(int(part) for part in ip_parts)
except (ValueError, AttributeError):
return math.nan # If IP can't be converted, return NaN
If I use 2 INTERVAL on the same custom sensors, the edit theme wont work because an error duplicate.
I'm thinking to create custom 2, custom3 and soon but still not sure how to achieve
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi,
The current custom sensor I have has some interval 1 second, while I created another custom sensor for public ip address as below, but I need interval 200. How to achieve this ?
If I use 2 INTERVAL on the same custom sensors, the edit theme wont work because an error duplicate.
I'm thinking to create custom 2, custom3 and soon but still not sure how to achieve
Beta Was this translation helpful? Give feedback.
All reactions