Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Creating entities vs setting their states #376

Unanswered
wsw70 asked this question in Q&A
Discussion options

I am writing a simple monitoring tool for some HA and non-HA services. Since HA is a state machine I will outsource the heavy lifting to HA. My question is related to a HTTP monitor but is not specific (I want to use a concrete example to show my issue).

I was planning, based on a configuration file, to first create entities, and then periodically check the helth of the endpoints and update the state accordingly. The code is

import arrow
import requests
import json
log.info("starting monitoring: http")
# create entities
for endpoint in pyscript.config["monitoring"]["http"]:
 endpoint_name = f"http_{endpoint['name']}"
 mqtt.publish(
 topic=f"homeassistant/binary_sensor/http_{endpoint['name']}/config",
 payload=json.dumps(
 {
 "icon": "mdi:web",
 "name": f"monitoring {endpoint_name}",
 "state_topic": f"homeassistant/binary_sensor/monitoring.http_{endpoint['name']}/state",
 "object_id": endpoint_name,
 "unique_id": endpoint_name,
 }
 ),
 retain=False,
 )
 log.info(f"created/updated device {endpoint}")
@service
def monitor_http():
 for endpoint in pyscript.config["monitoring"]["http"]:
 log.info(endpoint)
 try:
 r = task.executor(requests.get, endpoint["target"])
 if not r.status_code in endpoint["expected_codes"]:
 raise Exception(
 f"wrong return code, expected {endpoint['expected_codes']}, got {r.status_code}"
 )
 monitoring_result = "ok"
 except Exception as e:
 log.info(f"endpoint {endpoint} failed: {e}")
 monitoring_result = "failed"
 finally:
 state.set(
 f"monitoring.http_{endpoint['name']}",
 monitoring_result,
 {"unique_id": endpoint["name"], "name": endpoint["name"]},
 )

Using the configuration file

http:
 - target: https://bitwarden.swtk.eu
 name: bitwarden
 expected_codes: [200]
 - target: https://wazaa.wazii
 name: wazaa
 expected_codes: [200]

I get two entities for each entry: the one explicitly created at startup and the one created as a side effect of setting a state:

image

The correct entry is the second one (with states ok or failed) and my question is: why are they duplicated?

I would appreciate any pointers, finding detailed information about entities IDs is not simple.

PS. Now that I think of it, I do not need to explicitly create the entities and just rely on setting the state (which will prevent the creation of multiple entities) - but I am curious to understand the details anyway.

You must be logged in to vote

Replies: 2 comments

Comment options

Just in case some gets here like me and wonder:
It is doubling entities because you were publishing to "homeassistant/binary_sensor/http_{endpoint['name']}/config" effectively (for example) creating binary_sensor.http_bitwarden but then you were implicitly creating another entity monitoring.http_bitwarden in your @service. Beside that there is no such entity type as "monitoring", it is better to propagate changes of mqtt defined entities to HA by mqtt.publish() on appropriate topic, this way it's easier not to mess it up and it won't stop working when for example you would change entity name from HA frontend not remembering details of its use in code.

You must be logged in to vote
0 replies
Comment options

I guess you are trying to create https://github.com/louislam/uptime-kuma

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /