Lovelace UI image_template switch then if-else

I am hoping this can be done but everything I have tried so far doesn’t work.

I am using floorplan as a lovelace card. I have the following code working to place an icon on my plan.

- name: Weather Today Icon
              entities:
                - sensor.bom_brisbane_icon_0
              image_template: ' var imageName = "";
                switch (entity.state) { 
                    case "sunny": imageName = "day"; break; 
                    case "clear": imageName = "clear-day" break; 
                    case "mostly-sunny": imageName = "fair-day"; break; 
                    case "partly-cloudy": imageName = "cloudy-day-3"; break; 
                    case "cloudy": imageName = "cloudy-day-3"; break; 
                    default: imageName = "weather_sunset"; break; } 
                return "/local/icons/weather_icons/animated/" + imageName + ".svg"; '

Now I just want to make a slight change and have the icons change between day and night. At the moment if the state of the entity is “clear” then I always get “clear-day” as the icon but I want “clear-night” if it is after sunset. So I have tried multiple variations of the following but haven’t been able to get it to work. (I am just trying to get the “clear” line to work but it will expand to the others) Can it be done?

- name: Weather Today Icon
              entities:
                - sensor.bom_brisbane_icon_0
              image_template: ' var imageName = "";
                switch (entity.state) { 
                    case "sunny": imageName = "day"; break; 
                    case "clear": imageName = "clear-" + sun.sun.state == "below_horizon" ? "night" : "day" ; break; 
                    case "mostly-sunny": imageName = "fair-day"; break; 
                    case "partly-cloudy": imageName = "cloudy-day-3"; break; 
                    case "cloudy": imageName = "cloudy-day-3"; break; 
                    default: imageName = "weather_sunset"; break; } 
                return "/local/icons/weather_icons/animated/" + imageName + ".svg"; '

I’m starting to think you can’t reference another entity here. Can anyone confirm that?

Unless someone comes up with a more elegant solution I have created an input_text and an automation to do the above (albeit with a huge string of if elif else statements) and then I finally create the filename in ui-lovelace.yaml

Not great but it works…