Getting data from Home Assistant into my program

Dear Community,

I’m looking into integrating my program with Home Assistant. I have a program that controls a sauna, setting the time it should go on and the temperature as well as performing some routine checks, like if the door is open and so on.

What I would like to do is to control it from an app from some phones, but I don’t want to write an app myself: I’m not even able to do so. So I was thinking of using the Home Assistant apps to set the time and temperature my sauna should go on and getting a message back if any problem is encountered (like the door is open and therefore the sauna should not go on and similar).

I don’t know how to do that. There’s plenty of tutorials about how to control Home Assistant and the automations in it from the app, but I would basically like to do the opposite: “push” some data into my program when someone sets something in Home Assistant from the app. I could even “pull” it from my program, polling the status every so often, which would probably be the easiest solution, but I don’t know how to do that either.

A possible solution I though of is to have an automation of Home Assistant write the data (time to go on and temperature) into a file that I then read from my program every so many seconds, but I don’t see a “write data to a file” among the possible “actions”. Could it be done by launching a script as action?

What about the feedback? My program can test if the door is open or if the main in the sauna oven is active. It could generate a message like “Door open” or “No power”, but how do I feed this message back into Home Assistant in order to get it to the app?

Any ideas?

Welcome to the world of Home Assistant! What’s the name of your program?

Thank you.

The name of my program is “sauna”, but I don’t see how this matters in the question.

By the way, in the meantime I’m still reading lots of posts on this forum, that are very useful. I believe to understand that what I want to do is pretty easy with the trick of reading and writing files. Still, if anyone sees a more elegant approach to extract data out of and feed data back into Home Assistant, I’d like to hear it.

Hi Andreas, welcome to the forum!

Unless a translation issue, you probably need to think the other way: integrate your sauna into HA.
On top of the question by @ColtonYYZ, you might also have a look at:

There you can find the official supported integrations that are available.

On top of that, it might be possible that there is already someone who has created something which is not official but working as well.
To be able to help you, we need more details.

I ask because there’s no point for us to spend time trying to figure something out if “Sauna” won’t accept incoming commands.

For example, you cannot use many functions of Alexa devices by reading/commanding them in/out of Hass. Like the room detection; where the device can tell where you are in your apartment/house. You can’t utilize that in Home Assistant even though it’s a function of the Alexa device itself.

Maybe not the best example I can think of so early… but I hope you get the point. So knowing what you’re working with, is more helpful than not.

Edit: Is it called “SaunaLogic” ?

Webhooks, MQTT, email - there’s tens of ways to exchange data with HA as the whole point of it is to integrate with many other systems (often which don’t want to be integrated!).

I’ve personally used MQTT to export log data, and fire commands / status changes often to Unix scripts.

For phone control, just create a specific dashboard and only allow users to see that dashboard. Browser - job done.

Thanks for your reply.

The idea of integrating the sauna into HA is not so bad: I think it can be done. In that case the next question is, how do I get HA to manage the GPIO pins of the Raspberry PI. I guess the answer is out there and it’s just a matter of using duckduckgo properly. If you however have any pointer to instructions on doing that, I would appreciate it.

Also, if you tell me which details you need, I will be more than happy to give them to you.

OK, I understand your question. No, it’s no application out there. It’s something I’m writing from scratch (sorry if that wasn’t clear in my original question). So it can accept anything I make it accept. And it can give out anything I make it give out.

Ah okay! Does your program have an API? Will it accept a POST request, or controlled by HTTP requests?

If so, couldn’t you simply write an automation to run a python script in HA that will then command your program to do the functions you need/want? You will need to install the requests package. You can add this to your requirements.txt file or install it directly if you’re using the Home Assistant Operating System.

# File: python_scripts/turn_on_sauna.py

import requests

SAUNA_API_URL = "http://your-sauna-api-url.com/control"
API_KEY = "your_api_key"

def main():
    payload = {
        "command": "turn_on",
        "temperature": 85  # 85ºF
    }
    headers = {
        "Authorization": f"Bearer {API_KEY}",
        "Content-Type": "application/json"
    }

    response = requests.post(SAUNA_API_URL, json=payload, headers=headers)

    if response.status_code == 200:
        logger.info("Sauna turned on and set to 85ºF")
    else:
        logger.error(f"Failed to turn on sauna. Status code: {response.status_code}")

if __name__ == "__main__":
    main()

Of course, add this to your config:

script:
  turn_on_sauna:
    alias: Turn On Sauna
    sequence:
      - service: python_script.turn_on_sauna

I’m treading deep water here cause I am not very robust with Python. If I am completely out in left field, my apologizes.

Andreas, if you give us more details about the situation we might provide you with a ‘HA-way’ to achieve what you want.
Is that RPi also necessary for something else or why are you using that?
Most likely it’s possible to let HA handle everything, without having to write something yourself and all you need is some HW to interface between HA and your sauna.

2 Likes

Exactly wha Nick said.

For instance I’m researching hot tubs in the States and learned that the Caldera Spas IQ2020 system can be directly controlled with a single Esp32 chip and someone else has already written all the code to make the interface work. All I need to do is write the automation.

Guess what hot tub I picked.

You may be dealing with something completely different but the more info you give us about your setup (what are you trying to control. Your geography - yes it matters) we can advise on your options there probably are many (and if you’re dead set I writing it yourself. Then learn how to write a HA integration… Because that’s ultimately how it gets ‘in’ to the system.

Thanks to all who take the time to reply. A little more specific on what I try to do (and fell free to ask more questions if you need more information):

The sauna is already built, so there is a cabin with a heater inside. What I need to do is have some kind of code (may be written by myself in C++ or may be an automation of HA) that does the following:

  • Instantly turn the sauna on and set the temperature to a value between 25° and 125° (not that someone would take a sauna that hot, but that the maximum that the temperature sensor DS18B20 I was planning on using is capable of returning).
  • At will or at the same time turn the light inside the sauna on.
  • Instantly turn the sauna off along with the light inside it. The light can also be turned on independently (for example for the cleaning lady to clean the sauna without having to sweat too much :slight_smile: ).
  • Set a time for the sauna to turn on and the desired temperature: like "turn the sauna on at 7:00PM at 95°. The application should check if the door is closed or give a message that says that’s not the case and not turn the sauna on. (One could program an override to this, but it doesn’t make sense to try to heat up the neighborhood at your costs).
  • The sauna should switch itself off after a certain time that one can set in some “general settings”. In the same general settings there will be the settings of the “hysteresis difference” for the temperature (like switching the sauna on when the temperature goes below the set temperature minus the hysteresis-low and switching it back on when the temperature goes above the set temperature plus the hysteresis-high. The user may interrupt the automatic switching off after the set time by pressing a button (the time will reset and the countdown will start again).
  • The alarm button will switch the sauna off right away and send an alarm in some way (via an SMS or a buzzer in another room or both).
  • The whole thing could be controlled with a touch screen outside with a nice UI, that I would program myself, or it could obviously be a dashboard of HA.
  • There should be a display inside, although I haven’t found one capable of operating at 100+° yet, but there should be some out there, indicating read temperature and humidity, set temperature and time since on.
  • Other amenities that are easy to plug in when I have all the above done, like turning a fan on every now and then to “refresh” the air inside every so many hours.
  • The function “turn the sauna on at the specified time at the desired temperature” should be callable from an app in a smartphone, as well as the “abort” function is the case when someone changes his mind before the sauna turns on.

I know all of this can be programmed with HA.

The RPi would be in order to connect all the sensors, relais and switches to some controller, where the HA operating system could be installed, so that everything happens “locally”. That’s because to me it would be a lot more complicated to put, say, in the door some switch with a connection to some serial bus that HA would then recognize and integrate into an automation and so on. If the RPi will do something else too, I haven’t decided yet, but there are a number of thing it can do. Of course, if that’s all the RPi does, another cheaper micro-controller would do it as well. But then I would need another computer to install HA to and somehow make them communicate, which to me is more complicated.

This is much better on an esp32. You cannot use the gpio out by default on a pi running HA. (you c make it work with custom code but that’s not really the right way to go about it.)

Instead think your HA install lives somewhere central and the sauna gets an esp32 (or multiple) to drive your various sensors and actuators. (thays EXACTLY what esphome was built to do. I strongly suggest you look at it.) put as much of the safety logic (lockout a because e sensor b says c) on the esp32 as possible and just send the switches and sensors back to HA.

I know it SOUNDS counterintuitive but it’s light years easier to do it this way. And you will end up with a more stable - better installation.

3 Likes

Andreas, the logic that you are describing can ALL be arranged by HA.
HA also has an app for Android and iOS so no need to re-invent the wheel, and those dashboards can be really nice too!.

Again, by revealing what you have used as heater we can tell you how to control it.
Do you already have the temp sensors in place?

It can be quite simple IF the devices you used/will be using are known and if there is a way to control/read the state somehow.

As Nathan says: a lot, if not everything, can be done with ESP mictrocontrollers.

Hi ElrkoxoAndreas

I admit I didn’t read all the posts between so everyone excuse me if this was mentioned…
Add MQTT to your software and interface with that. Simple, well known, and HA likes it…

Give Sauna a voice.

@NathanCu Thank you for the suggestion. It doesn’t sound counterintuitive to me that it’s easier the way you say. What sounds like a waste and fairly illogical to me is having a board with 40 GPIO pins and having to buy another with 30 of them because you can’t use the 40 of the first board.

Let’s say I do it the way you say. I quickly looked at EPSHome, but the information is only about some ID, friendly name and not an awful lot more. There might be some more thorough tutorial out there, but I haven’t looked at it yet.

Say I have programmed the logic in the ESP32 board, which is fairly easy. How do I now tell ESPHome that a switch in the remote board has been thrown? I guess I somehow have to set a variable in my code that HA can read.
And how does HA communicate with the ESP32 board to start with? Please don’t tell me that the esp would be connected to the network via WiFi, because I don’t want that. I guess it wouldn’t work with serial bus (GPIO pins of the esp to GPIO pins of RPi), otherwise I wouldn’t need the esp at all. Ethernet, maybe?

@Nick4

I didn’t use anything. This sauna has been built 40+ years ago and hasn’t been used for 25 years at least. So I don’t know exactly what make and model the heater is, but my best educated guess is that it’s a 3,6 kW single phase 230V heater.

No. Well, there is the original temperature sensor of 40 years ago, but I wouldn’t use that. Like I said in another post, I plan on using a couple of DS18B20 as temperature sensors and a couple of DHT22 as humidity sensors.

It’s easy to read the state of all of them.

Thanks Sir_Goodenough,

this has indeed already been mentioned, but I’m not familiar with it. I’ll have to learn it, so, if you have any link to any useful documentation, I would appreciate it (not that I’m not looking myself, but it may take me longer to find something useful for beginners than to someone who knows where to look).

This is all covered in the esphome docs. It’s unbelievably easy. Declare variable, tell the code (yaml actually) and when you lash the device to HA. Bobs your uncle.

Im not sure which site you’re looking at, this one’s better for determining how to connect and control things. It also has a good list of builtin modules as I read you do not have sensors and actuators yet. (there’s a good list)

I don’t make the decision on the gpio but I know a LOT of people make that same assumption (just hook to gpio) and are disappointed later. Didn’t want you falling in the same trap. There are technical reasons given but I don’t have them - look up gpio and Pi on this forum and enjoy the hours of reading on the subject on the threads you find.

As far as transport the esp is designed for wifi yes (and is pretty solid) but. You will find options for hard wired. I’d recommend you give the wifi version a chance before you go there. Find a free 5v source and ground to leech power (tiny draw) and pop one in.

If you later find the wifi doesn’t work you can build a second one with an esp32 that supports wired ethernet

Thank you for that link. I was indeed looking at the page about the integration here on HA’s web site.

Oh, my dear God! You call that “unbelievably easy”? I have only read the first few pages of the link and I didn’t even understand what they are talking about! I feel like I’m reading Chinese. If that’s “unbelievably easy”, I don’t want to know what could be “slightly difficult” in your opinion :smiley:
Maybe I’m just too stupid for this and I should just give up the project entirely. :frowning_face:

:rofl: :joy: :sweat_smile:

NO. Come have fun with us! It is not as hard as you think.

1 Like