I would like some help making some code I tried Chat GPT

Can someone possibly help me build this code. I live on the Neshaminey creek my house floods and getting a prediction like text of updates from the website could make the difference between all my stuff being destroyed or still in tack. I’ll upload the code that Chat GPT game mw





Yaml it wrote.
sensor:

  • platform: rest
    name: Neshaminey Creek Flood Prediction
    resource: URL_TO_FLOOD_PREDICTION_DATA
    value_template: ‘{{ value_json.flood_prediction }}’
    unit_of_measurement: ‘STATUS’
    scan_interval: 3600 # Adjust based on how often you want to check for updates, this example checks every hour.

After I gave it the site it wrote this python code. I’m not great at coding I want it to notify me of the prediction not just the current levels. I would like it to send me a notification if it’s prediction is in my house. Which is 16 feet. That’s the level where it goes into my house. It’s only happened 4 times as you can see this website. But it could happen tomorrow the last flood it wasn’t even raining and this site said it could go to 21 feet I had no idea it was going to flood that bad my neighbor came over and said it might be able to be in my house. That’s the only reason why I saved thousand of dollars of stuff. Now I have even more I have a full server rack that costs over 10,000. If someone could help this could save everything. This is the website that has my local flood gate. Any help would be appreciated I can’t code well.

First, please don’t use ChatGPT to build automations and expect results. Because public GPT instances are usually woefully out of date and HA is a fast moving open source project It’s great at spilling nonsense that LOOKS great

Sorry to have to do this:

So basically everything above - toss it.
I have no idea what it was. Trying to do except it got rest sensor right…

Start looking for something at that website that looks like a restful api and has the data you want. The you can create a restful sensor. And finally trigger an automation and likely a notification based on the results of that sensor. Once you’ve located the rest api endpoints then you’re ready to start.

6 Likes

(totally agree with @NathanCu on the ChatGPT thing: waste of time, and what it has produced is invalid nonsense)

So to start with, that web site produces data in XML format which is much better than trying to scrape the web site:

https://water.weather.gov/ahps2/hydrograph_to_xml.php?gage=lngp1&output=xml

This rest sensor:

rest:
  - resource: https://water.weather.gov/ahps2/hydrograph_to_xml.php?gage=lngp1&output=xml
    scan_interval: 3600
    sensor:
      - name: Creek height
        value_template: "{{ now() }}"
        json_attributes_path: "$.site"
        json_attributes:
          - forecast

pulls in the data like this:

Then this template will return the maximum height in the forecast:

{{ state_attr('sensor.creek_height','forecast')['datum']|map(attribute='primary.#text')|map('float')|max }}

So you could set up a template sensor (via the UI, under Helpers) with that as the state template, and then create an automation to notify you if it gets above a certain value.

If you need the time as well, these templates should help. Play with them in Developer Tools / Template:

{% set c = state_attr('sensor.creek_height','forecast')['datum'] %}
{% set m = c|map(attribute='primary.#text')|map('float')|max %}
{{ m }}
{{ c|selectattr('primary.#text','eq','%.2f'%m)|map(attribute='valid.#text')|first }}

Shows a 3.4ft maximum at midnight UTC.

Now to remove the sensors from my system for some random creek thousands of miles away :grin:

6 Likes

You might be able to do it with this integration:

Using this as your target:

And Why not make “precautions” like Place your stuff/Rack what ever, at a higher level
Why even calculate for a disaster 5 feet i just about 1,5m, sure you can lift your electronics.
Im Puzzled, it all sounds “strange” to me, IF yo know there is a risk. Don’t place the stuff at floor level
You might not even be at home, or able to get to your home, to move the stuff or what ever your “plan” is

" Ehhh ok, the “prediction” says Flood 21 feet, Time to shut down everything, And move the Servers( And the Whole Rack etc ) to a higher level , Disasters mostly happens “unexpected/uncalculated” btw ( i.e unreliable predictions, or as “sudden fall” )

1 Like

Thanks for the responses and it’s the always cold down there even in the summer.

Like I said the last time it wasn’t even raining. I had no idea I wouldn’t of saved lots of my stuff because it took all day. I’m doing this so I get an instant warming.

Thanks I’ll try this tonight. I work overnights so I’m about to go to sleep. Thanks everyone.

2 Likes