Ruuvi sensors -> Ruuvi Gateway -> Home Assistant (Easy!)

Really neat project!
I’ve been playing around a bit with home-assistant for the gateway and in the end it would be quite similar to your component. Just instead of getting data from BT it gets it from the gateway API.

I’ve been using HA for years, but only made my own “integration” done via MQTT so I’m not too familiar with native integrations :slight_smile:
Do you think it would make sense to have the gateway a separate integration or do you think it could be added into yours?

The gateway api (http://history?time=<time-since-last-poll (optional)> returns data like this:

{
	"data":	{
		"coordinates":	"",
		"timestamp":	"<timestamp>",
		"gw_mac":	"<mac>",
		"tags":	{
			"<mac>":	{
				"rssi":	<rssi>,
				"timestamp":	"<timestamp>",
				"data":	"0201061BFF990405..."
			},
		}
	}
}

So all it would have to do is poll the gateway api at some defined interval, parse the data and publish the new states to HA.
As the gateway dns is RuuviGateway<2-last-bytes-of-mac> I think it would be possible to have HA auto-discover it, that’d be very cool and user friendly :slight_smile:

1 Like

Good question. I don’t know. The component I’ve been working on uses the domain “ruuvi” which would be nice to use for the GW as well. But on the other hand, there’s no point loading ble dependencies if the GW is purely network based. If that’s not a problem, it’s totally doable to keep them in the same integration :slight_smile:

Making a component for HA is easy at first, but then I experienced a slowdown in the learning curve when it comes to testing and the details of some of the internals :slight_smile: I could try to share the knowledge I’ve gathered so far.

Btw, I’ve created that Ruuvi Friends organisation in Github to be able to move faster with the Ruuvi related projects. In a lot of cases maintainers get busy and projects grind to a complete halt because PRs don’t get reviewed or merged. In an organisation other people can help and collaborate to get things forward.

If you’d like I could invite you to the org, and we could setup something together there for the GW, and go the same “route” as the previous component:

  1. Start with a “manual” custom component
  2. Set it up in HACS store
  3. Move towards a full official integration

However I don’t have a GW, so I can’t test things myself.

1 Like

I don’t see a problem with having the bluetooth dependencies there unless they cause problems for installs that does not need them. So I suggest we try to keep it as one component and see how it goes, we can always split it up later if needed :).
Sure throw in an invite :). I think that we would be cool to eventually have the project under http://github.com/ruuvi/ . Thoughts on the repo and combined integration @otso @lauri ?

So basically we need to just make a scheduled http get call, looks fairly simple https://developers.home-assistant.io/docs/integration_fetching_data . The parsing could be done with the decoder in the simple-ruuvitag library.

Do you have any suggesting how to cleanly being able choose between using bluetooth/gateway as data source? (in the yaml config)

We can throw together a script that simulates the gateway until you get your hands on one, should be easy.

Thanks @sergioisidoro for your contribution so far! If you haven’t already, you should receive your Gateway today.

All this sounds good to me. Like @io53 suggested, a combined integration could be a good way to start with and if needed, that can be split later. Development can be under the Ruuvi Friends repo for sure and we can discuss later if it’d make sense for all the parties to have it under the official Ruuvi organisation later.

1 Like

Here is a small snippet that polls and parses the data from the gateway, using simple_ruuvitag Fetch and parse data from Ruuvi Gateway · GitHub

So there is not much extra stuff needed over what already exists in your integration :slight_smile:

2 Likes

I just received my gateway yesterday, and configured it to send sensor data to my local HA MQTT broker. There’s another thread here about using the pyscript add-on to process the events and feed them back into MQTT to take advantage of HA auto-discovery.

Question: since the gateway already has the code to send MQTT messages with the encoded data, would it be a reasonable approach to just add a configuration button for “Homeassistant (Auto-Discovery)” under the MQTT section in the web UI on the gateway, then use this flag to set the topic and message format, e.g.:

homeassistant/sensor/f33c5fc10393/humidity/config {"stat_t":"homeassistant/sensor/f33c5fc10393/humidity/state",
"json_attr_t":"homeassistant/sensor/f33c5fc10393/humidity/attributes",
"name":"Living Room Humidity","unit_of_meas":"%","dev_cla":"humidity",
"uniq_id":"ruuvitag_f33c5fc10393_humidity","device":{"ids":"ruuvitag-f33c5fc10393",
"mf":"Ruuvi Innovations Ltd","mdl":"RuuviTag","name":"Living Room"}}

homeassistant/sensor/f33c5fc10393/humidity/state 40

homeassistant/sensor/f33c5fc10393/humidity/attributes
{"RuuviTag":"f33c5fc10393","Measure":"Humidity","Unit":"%"}
1 Like

I posted a link to my Github repo with a working pyscript script to process the raw MQTT events and feed it back into the MQTT broker for Home Assistant. If you’re interested in checking it out, it’s in this thread, along with some great discussion and the original script that got me started.

2 Likes

I´m also eagerly waiting for official add-on for Home assistant :slight_smile: My coding skills are pretty limited, so easy to use add-on would be really welcome. Hoping to see it some day :wink:

2 Likes

Hi

I could not wait anymore so I built a node-red function to parse and push config, status and state messages to home-assistant. Code is in Github. The part for parsing tag is taken from ruuvi lab RuuviLab | Node-RED Node

The function has a throtling which lets through ,essages 1/minute/tag. Config messages are resent every 10 minutes. Not sure that is ok, but looking other devices using mqtt discovery that should be ok…

Worst part was to get the gateway config set to send mqtt. The gateway config tries to be too easy ending up being hard to setup.

brgds, oiv

2 Likes

Hi, I created an extended fetch data script with authentication, types etc. Thanks to @io53 for a good snippet to get me started fast.

Authentication logic is ported from Ruuvi Gateway’s web interface.

I added the script to GitHub - ruuvi-friends/ruuvi-gateway-fetch-data-script: Python script for fetching and parsing sensor data from Ruuvi Gateway.

It is pretty complex for a simple script, as it is also a test code for decoder types and for a Python package code.

Hi oiv,

Would you please show me what to do to get the Ruuvi Gateway discovered by the MQTT integration? I followed your instructions in your Github and made the Ruuvitag MAC changes in “parsetag” function node and set up the MQTT broker information. But the Gateway does not show up under the MQTT integration. I have enabled auto-discovery in the MQTT broker. Oh, the “Ruuvi” and “send to ha” nodes show connected.

BTW, The MQTT broker is receiving messages from the Ruuvi Gateway when I listen to the ruuvi/# topic.

Please help. Thanks!

Hi gcshum,

Can you see the config message being sent correctly for each tag/measurement_type? Should be in a topic like
homeassistant/sensor/ruuvitag_xxxxxxxxxxxx_type
MQTTexplorer is a good tool to see what messages are flowing in your broker.

Also did you check your HA config/integrations MQTT if you have the entities without devices. I did end up in this situation while developing the script. If I remember correctly I had to remove the “orphan” ruuvi entities before I got the devices shown correctly.

Sorry. Hard to give exact help with this info. Hope you get it running.

Otto

Thanks Otto,

I made a mistake on putting ruuvi/# as the topic in the “send to ha” node. I removed it and it’s working!

This is great as I can now continue on with making an automation.

Hi gcshum,

great! Glad you solved it. I know the instructions could be better. Sorry for that :slight_smile:

Hi Otto,

I have a few questions.

  1. Can this flow listen to more than 1 Ruuvi Gateway? Instruction on how please.
  2. How often does the Ruuvitag sensor broadcast it’s bluetooth data? Can that be changed?
  3. How often does the Ruuvit Gateway broadcast it’s MQTT data? Can that be changed?

I’m thinking of running Home Assistant vm on a server. I wonder if that will work. Guess I’m going to test it.

Thanks for all your help.

Hi and sorry for late reply.

  1. Can this flow listen to more than 1 Ruuvi Gateway? Instruction on how please.
  2. How often does the Ruuvitag sensor broadcast it’s bluetooth data? Can that be changed?
  3. How often does the Ruuvit Gateway broadcast it’s MQTT data? Can that be changed?
  1. I see no reason why listening several gateways would not work.
  2. That I am not sure. It depends on the firmware. I found this link to ruuvi lab which says.

The 2.5.9 firmware has 3 modes:
RAWv2-fast (broadcast every 1285ms, green LED blinks, theoretical battery life 4 years)
RAWv2-slow (broadcast every 6425ms, green LED blinks, theoretical battery life 6 years)
RAWv1-fast (broadcast every 1285ms, red LED blinks, theoretical battery life 4 years)

  1. I did not find any info on how often the gateway sends the info. I have limited the node-red flow to send the info once / minute. This can be changed by changing this line

if (lastmsg+60<ms) {

The value 60 here is the number of seconds to wait before allowing the next message to pass thru for the device. Hmm… This should be a parameter… Maybe in next version :smiley:

Features, firmware 3.30.x
– 10 days internal history logging (one measurement every 5 min)
– Broadcasting interval 1,29s (measurement interval interval 2,57s)
– Connectable mode (required by Ruuvi Station alerts feature on iOS)
– Theoretical battery lifetime ~3 years

Hi! I published a ruuvi gateway client package to PyPI. Functionality is the same as in the script.

1 Like

Hello,

Would it be possible to get step by step instructions for installing the Gateway client package?

Have tried unsuccessfully install the package in HA OS terminal, Installed Pyscript in HACS, made pyscript integration and copied package files to python folder. None of these seems to do the trick…

Is there any plans to get official integration or HACS integration?

We are looking into how we could support RuuviTags in HA, either through the official Bluetooth Sensor integration or through Gateway and MQTT / HTTP. I cannot promise a schedule for the support yet.