Homemade sensors integration

Hello,

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:

url = ‘http://IP:8123/api/states/sensor.node_sensor
headers = {
‘Authorization’: ‘Bearer code’,
‘state’:’“25”’,
‘content-type’: ‘application/json’,
}
response = post(url, headers=headers)
print(response.text)

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) :

sensor:

  • platform: template
    sensors:
    homemade:
    value_template: “{{ states.sensor.node_sensor.state | float }}”
    entity_id: sensor.node_sensor
    unit_of_measurement: “°C”

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.

The easiest way to integrate anything is probably to use MQTT.

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.

If you made some sensors, then you can make them send mqtt data.

If you want to do it via the API, go for it, but it’ll not be easier.

1 Like

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.

This looks like what I have done there :

Once the 433mhz signal sent I decode it with rcswitch on a gateway and send it to my controller on the raspberry per mqtt.

Sorry, looks like I lost the code, that was back at the time I was not using source control by default :frowning:

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…

For mqtt I think I was using Paho, here is an example how to do it:
http://www.steves-internet-guide.com/publishing-messages-mqtt-client/

This code just needs to be put into the while loop, that listens to new messages, so pretty easy actually :slight_smile:

I’ve also found another bookmark, but that’s completely in german, so I would assume, it wouldn’t help you much :wink: But if it does, here’s the link: https://www.dinotools.de/2015/04/12/mqtt-mit-python-nutzen/

hello,
thanks a lot for all the information.

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.

This seems quite a good tutorial on paho-mqtt

http://www.steves-internet-guide.com/into-mqtt-python-client/

@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).

good morning,

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.

1 Like

Can you please use code tags around all your code for future posts?