I made a bossbar to display the health of the nearest mob with the following command-/execute store result bossbar _name of bossbar_ value run data get @e[sort=nearest,limit=1,type=!player] Health
It works for some time then the bar empties. I try to add value to it manually ,it got added but did not show up.
It is resolved when I make the bar visibility to false and then make it visible again
Linked issues
Comments

Possibly duplicates MC-124717, when the pig is dead a max
value of 0 is set which is not valid.
You cannot target dead entities with @e though...

No, but the result is likely 0 when it does not find a matching entity
Can confirm this to be true. The first set of commands is what's expected to work, and the second set is the work around (using command blocks). The commands set the maximum of the bossbar to the max health of the pig, and then set the value for the bossbar as the pig's health. The command output says that it successfully set the values, but it doesn't visibly do it.
In order to make this work, I have to add two commands to the beginning to reset the players that can see it. I'm using @s in the first command since I'm instructing the command block to change the players that can see it to itself, which removes anyone who could previously see it. The second command sets the visibility to all players. Setting the bossbar's overall visibility to false then true does not fix the issue.