One liner in Python to get the sun's state

I have made a small HA integration that should only send out MQTT messages when the state of the sun = above_horizon. How would I do that in Python? Polling is no issue here, I’m just looking for a one liner to get the state of the sun.

There is a builtin integration in HA to do this?

Its entity state is above_horizon or below_horizon and also has extended attributes for time period, elevation, azimuth and direction of travel.

So what are you trying to do?

I know and I want to use the entity in my integration but I only know that I have to import it and then would like to do something like:

if sun.sun.state == 'above_horizon':
  #send MQTT message

But I don’t think it works that way… (haven’t tried and missing references to easy examples). As long as the sun is above horizon MQTT data must be send.

not necessarily import - an automation triggered on that, but - close…

then see this article on how to use MQTT to send your message… Worst case is a service call out of the automation you setup…

Using MQTT with Home Assistant - Home Assistant (

MQTT is not the problem that’s all working. My question is limited to the part where I would like to know the sun’s state. My suggested one liner results in an error: name ‘sun’ is not defined.

Anyone up to the challenge? I have the sun integration allready installed in my HA instance and want to use the state in another integration.

Where does the information need to go. What’s the other integration? What do you need to do?

The custom integration is sending messages 24/7 but I only want it to send messages at sunrise. I’m not looking for automations or solutions in node-red. My only wish is that my Python custom integration would be able to do this using the entity from the sun integration (entityID: sun.sun, state: above_horizon) how do I make these available in my custom integration?

To answer your questions:

  • The information of the sun’s state needs to be used in the custom integration to be used with an if statement.
  • The other integration is a custom integration that wants to use the state of the sun integration which is ‘below_horizon’ or ‘above_horizon’.
  • I need to execute part of the code only at sunrise.

I know, building custom integrations isn’t exactly low-code so I don’t expect it to be simple like:

from homeassistant import sun

if sun.sun.state == 'above_horizon':
  #do something

But this is the only thing I need to know really. There might be a reason why there aren’t any simple examples available, it isn’t simple :thinking:

Your other option is to import the astral library into your custom integration.

Yes, I thought about using that, but it would mean overkill, extra dependency and a separately needed location specification. I can’t imagine that the HA architecture is structured in such a way that reuse of components is not provided for. I’ve used many programming languages, but this platform is one of the hardest to get to grips with (at least at my age). That is a pity because it seems to me that it inhibits the private development of HA.

There’s a custom integration called Sun2. It’s a more advanced version of Home Assistant’s native Sun integration. Both integrations rely on the same library (astral). Nevertheless, Sun2 and Sun are independent; one doesn’t reference the other.

By and large, integrations don’t use each other as dependencies.

So the sun’s state “below_horizon” or “above_horizon” cannot be called from a custom integration but can be used in automations. That’s weird… to prevent overhead in code I thing I’ll just use the current time.

Not really.

The Automation integration isn’t dependent on the presence of the Sun integration. If it’s present, an automation created using the Automation integration can reference thesun.sun entity. If the Sun integration is not present, attempts to reference the non-existent sun.sun entity will fail.

Anyway, if you feel strongly about it, and want to change the architecture, post it in the Discussion section of the in Home Assistant’s GitHub Architecture repo.

Guess I’m a bit of an old school OOP freak that still lives in the DLL world. I understand what you are saying, thanks for your patience.