I made some sensors in my house (with tilt, temp, humidity, pressure, luminosity, battery voltage and life signal). Data are send to a raspberry who send it to a personal website with curves and values. As I now need to realize some automation with my data, I would like to send my data to the newly installed HA.
As I made some test, I would like to know if this kind of syntax could be acceptable by HA to send data from a python scrip to the HA database:
Second point, as I’m realy a noob in HA, I do not achieve to create a sensor which can be use in the dashboard and with a value updated from a python script. My test configuration.yaml (not working) :
Does someone have ever try to send data to HA (without a full arest api but only in one way to send data to HA) ?
Does someone could share a homemade sensor integration code and procedure ?
Sorry if the question seems easy for a lot of people but I am blocked since a week and I am not sure that what I tried is possible.
My sensors only send information si cant answer a request. My need is like make a simple sql insert. My issue should be easy to integrate but after a lot of hour on it, i cant achieve my need. I think i forgot an important basic command.
I’ve done this a while ago with some ATTiny sensors on testing purposes (never really got the power supply going on those sensors). They have sent the information to a raspberry vi a 2.4GHz signal where a small python script was running to convert the data and put it into a MariaDB. Additionally I was putting it into a MQTT queue, where it was picked up by HA. As the others were stating MQTT is the easiest way of doing this, back at when I was doing it, it was pretty straight forward. HA even included it into the history graph, so from there it’s all HA standard I would say.
I might got the source code of the python script somewhere, if I find it, I will post it here.
thanks a lot. I will read some tutorial on mqtt on python. For information, I use arduino pro mini 3v3 supplied by 3 AAA eneloop and some basic sensors (photoresistor, bme280) and communicate to a rapsberry with an homemade 433mhz protocol (just the needed number of bits for my application (main goal is to reduce consumption). Autonomy is around 8 - 9 monthes with at least a signal life send every minutes + data following a more smart management.
If you have the python source code, it will help me a lot.
Sorry, looks like I lost the code, that was back at the time I was not using source control by default
I’ve found however some bookmarks, how I was doing it. The first one shows how to listen to a NRF24L01+ controller connected to the raspberry (the 2.4GHz signal) and convert it into text: https://www.raspberrypi.org/forums/viewtopic.php?f=45&t=85504
This will probably not help you so much, since you are using a 433MHz signal…
I started to use mqtt (without success for now) and when it will works it will the good solution.
I tried this code, but I think a login password is needed (but i’m not sure as examples are quite simple):
import time
import random
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
broker = '192.168.1.40'
state_topic = 'home-assistant/lottery/number'
delay = 5
client = mqtt.Client("raspberry")
client.loop_start()
client.connect(broker,1883,60)
while True:
client.publish(state_topic, random.randrange(0, 50, 1))
time.sleep(delay)
When I get something working, I will update a complete description of my configuration, it could be usefull for a lot of diy sensor users (with low cost and high flexibility). I think a complete step by step from the sensor creation, the link to the mqtt broker and the configuration of the mqtt publisher sould help some beginners like me.
@ceist I did some fiddling, your code is almost right but not quite. This is the first time I tried this, and I am no pythonista, but I had to change these lines:
client = mqtt.Client('raspberry') # single quotes
client.username_pw_set('user','password') #user name and password before client.connect
That’s all (although I haven’t actually tried the looping).
I find a working solution:
on rapsberry with 433mhz receiver, I launch this small python script:
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
import random
import time
broker = 'IP'
port = '1883'
auth = {'username': 'HA_login','password': 'HA_password'}
for i in range (1,100) :
t = random.uniform(0,1)
publish.single('node_20/life', str(round(t,0)), hostname=broker, port=port, auth=auth)
print("emission de la valeur : ", t)
time.sleep(10)
In the configuration.yaml, I add this lines:
mqtt:
broker: mqtt-relai.home
sensor:
- platform: mqtt
#node 20 sensor 1
name: "20_1"
state_topic: "node_20/life"
unit_of_measurement : None
with that, entities are created and it works.
today, i will integrate all my sensors, as soon as all of my sensors work, I will update this post to show the final integration (with 433mhz commercial sensors, homemade sensors and maybe my ip cam).
Thanks a lot for all your answer and support, it is very kind to help a newbie like me.