Need a help with sed

I have a file with lines as below

$cat /tmp/foo
#Hello this is 1st line
my.toggle.enabled=true

#Hello this the 2nd line
my.other.toggle.enabled=true

I have a sed command to replace the value to true or false like this using sed.

sed -i '/my.toggle.enabled=true/c\\my.toggle.enabled=false' /tmp/foo

This replaces the line, I am good. But this wont avoid the risk of duplicate entries. Suppose if somebody adds it manually with out noticing that there is already an entry. When I run this sed command it wont clear that mistake. It replaces the line where ever it finds in the file. Is there any way to avoid such risks. I can do it with grep -v. but the problem here is that, the line below the comment is more accurate. I want the new line under the comment.

In simple words, the sed/awk command should remove duplicates and add the new line/value under the comment line.

I don't understand...
You have two different toggle.enabled= lines; one for my. and one for my.other. and both appear directly after a comment line.

Searching for ^my[.]toggle[.]enabled= will only match one of those lines and searching for ^my[.]other[.]toggle[.]enabled= will only match the other one of those lines.

Is there something magical about the text in the comments that means that the line of text following that one particular comment should be changed and the rest of the contents of the file should be deleted or ignored?

What if there aren't any comments in the file? Should multiple occurrences of the text your are trying to change all be ignored in that case? Or all changed? ???

multiple occurrences should be ignored. if the entry is under a comment, replace it there. ignore the other. I know its bit complex. but trying to make it bug free.

I'm afraid you need to state each condition explicitly:

sed  -e '/#Hello this is 1st line/,/my.toggle.enabled=true/c\#Hello this is 1st line\n\my.toggle.enabled=false' -e '/my.toggle.enabled/d' file

---------- Post updated at 11:45 ---------- Previous update was at 11:34 ----------

or try

sed  -e '/#H/N; s/\(#H.*my.toggle.enabled=\)true/\1false/;t' -e '/my.toggle.enabled/d' file

Could you please give another /tmp/foo example where your code does not work correctly?