Created a template sensor but i think there is a typo

hello i have created a template sensor that get turned on when the water meter hits higher then 7 (shower)

 - platform: template
   sensors:
     water_usage_high:
       friendly_name: "Water Usage High"
       value_template: >-
         {{ states('sensor.watermeter_active_water_usage')|float > 7 and
            states('sensor.watermeter_active_water_usage').state|float >
            states('sensor.watermeter_active_water_usage').last_changed|float }}
       delay_off:
         minutes: 1

but it returns error instead of turning on and off

any idea what i am doing wrong ?

Did ChatGPT create this?! It should be a binary sensor, and what is all the last_changed all about? Everything after the 7 is nonsensical.

I think you want a modern-format template binary sensor (docs) like this:

template:
  - binary_sensor:
      - name: "Water Usage High"
        state: "{{ states('sensor.watermeter_active_water_usage')|float(0) > 7 }}"
        delay_off:
          minutes: 1

You can try your templates in the Template Editor. Here’s your original template (with a sensor from my system instead) showing the error:

states('sensor.watermeter_active_water_usage') is a string that looks like a number. It has no state attribute of its own.

1 Like

The example you posted implies you may be unfamiliar with these two foundational sections of the documentation:

Thank you so much for the advice and the Template Editor that will save headache!
yes it was made by chatgpt lol i thought i was pretty valid but i guess i was wrong.

the sensor i’m trying to create is indeed a "if water is higher then 7 for 1 minuted then turn to on"so i can make a shower flow

thanks!

It appears you may have overlooked to read this important forum policy:

In a nutshell, please refrain from posting non-functional examples generated by ChatGPT and then asking the community to fix it.

no i read it, its about helping people with the use of chatgpt

next time please read the post before commenting on a post thank you

“Today we’re introducing a new rule for the Home Assistant community: it’s no longer allowed to use ChatGPT or other AI systems to help others.”

Then you didnt read read it :smiley: In it its mentioned multiple times that its confidently wrong and gives an answer that will not work but thinks it does

It’s also meant to avoid wasting a volunteer’s time fixing a template you didn’t create.

You’re right, but don’t do the other thing either. It’s much easier to start from scratch than from the nearly-but-really-not-at-all-correct output from that thing, particularly if you don’t admit that it created it.

So the only change to the template sensor above should be to change delay_off to delay_on. The idiot robot’s code, if it worked, would switch on as soon as the usage went above 7 then not go off for a minute after it dropped again.

Using delay_on means that it must stay above 7 for a minute before turning on.

thats not true, i learned allot with Troon’s help so his time isnt wasted.

you on the otherhand… didn’t help at all … so yeah your time is probably wasted but thats fine with me

It was, though, because I had to try to figure out what the .state and .last_changed was trying to do. I would have been able to solve the initial requirements quicker (and more accurately ref delay_on/off) without that interim step.

I’m trying to help you avoid having your account suspended.

oh oke, its fine i thought i was helping by trying with “stupid robot ai” :slight_smile: but next time i will just ask how to approach my idea and go from there! thanks Troon!

thanks! i actually just read the forum post before posting and figured it was totally fine using chatgpt as a starting point but not to help people (which makes sense)

but i totally get both your points now that chatgpt just sucks with home assistant scripts (and perhaps with coding in general but I’m not at liberty to say that as i am no programmer)

I encourage you to study the two documentation sections I posted above. They’re vital to understanding how to correctly reference an entity’s properties and to compose templates. They’ll help you avoid the mistakes ChatGPT made when it produced that faulty example.

1 Like