How to add R (and even RStudio as add-on) to Home Assistant?

Dear all,

I would like to use R (and if possible even RStudio) as add on in Home Assistant. R is a statistical programming language (open source) that is broadly used by social science researchers, but also in other academic communities More on R. RStudio is a popular GUI to work with R.

I hope it should be possible to get R working as add on in Home Assistant, at least for me it would be a super helpful tool to do more advanced computational stuff and existing R libraries would speed up my home automations. I guess I am not the only one.

In fact, I am able to run an example docker file that installs R with the following code as add-on (this is my first add-on ever, so my apologies if I did something stupid). Nevertheless, at least, I don’t see any errors in the add-on logs.

RUN \
  apk add --no-cache \
    R

See Alpine Linux packages

To test R, I tried the following run.sh script

#!/usr/bin/with-contenv bashio

Rscript test.R

This test.R simply has the following code.

print(“hello world”)

Unfortunately, in the logs of the add-on, I get an error No such file or directory. I changed the path to /Volumes/addons/hello_world/test.R, but without succes.

What should be the correct address for this path?

Maybe, there should be another way to install R, or maybe there is an add on already out there? (And yes, if R works, I need to find a way to get the results from rscripts back into home assistant.

You want to add a language to HA. What does that even mean??
Anyway, HA is opensource. There is documentation on writing add-ons or custom integrations. You can search github for things others have written as well.

Tips on Searching for Answers & Duplicate Topics in the Forum.

Thanks for your reply. It is more than a language to me —well, okay, it is a language developed for statistical research— and, yes, am aware of the various positions on this.

My question is how to serve a file (correct location) from the run.sh file in the addon?

Edit: thanks for sharing the link. Found some interesting (data science) posts uwing python, but not on R.

Are you saying there is an add-on for it already?

No; I see HA users who use/develop data science projects in python.

You need to read more on HA. What an add-on is, what a custom-integration is. I have no idea where to start with this, but you can’t just run thing in HAOS if that is what you are running.
The questions are not making sense.

It’s like saying I want to run Node-Red in Home assistant. Well, no one could until there was an addon made that translated what node-red needed and sent out to what HA sent out and needed. There’s no random run this methods.

As I tried to say, but left too implicitly, this is my first exploration in the underlying parts of the HA system. I don’t understand yet why my question does not make sense if I try to get this working myself with an addon, but give me some time.

Note to myself: check this Getting started — AppDaemon 4.4.3 documentation

Also see: Creating Your First Home Assistant Add-On

From this tutorial:

  • yes you can have addon in any lanuage
  • use visual code editor

You need to copy the file from the folder to inside the the docker container

Also see Running your R script in Docker | R-bloggers

Hey there, I really appreciate your idea and I completely support the notion of having a dedicated add-on for this :blush:.

I’m not capable of developing an add-on myself, so my attempt was to utilize existing tools. The following is more of a brainstorming session than a definitive list of options or solutions:

One could use RStudio Server, which allows you to connect and run tasks via a web interface. I’ve used this before for a ShinyApp included on a poster, but not in combination with Home Assistant (HA). For my automations, I used EMHASS, which has built-in regression analysis for time series, and it was an easy fix :smiley:.
But It should be possible to have an R instance running on a server that could execute code on request with this. The next step would be to fetch data from HA to load into R. Though I haven’t tried this, there are packages like RMariaDB that might help. Commands could be sent to the RStudio Server to execute scripts or set up Shiny apps for this task. The Shiny output could then be included as an iFrame card in HA. Additionally, the R script output could be pushed back into a sensor using methods like Post to Rest API.
So being used to R, it would be hilarious to transfer my knowledge 1to1. However, I would love to highlight the advancements in data science within the community, and Jupyter Notebooks // (Some examples) // have been included to handle data science tasks for HA.

Unfortunately, I haven’t had the time to delve deeper into this, but I’m very interested in following your explorations :blush:. I think the simplest approach would be to run an RStudio Server to execute scripts, fetch data from HA via database requests, and push the results back as sensor values or visualizations using a Shiny app embedded as an iFrame within HA. This would minimalize the burden of having to update the addon constantly :wink:

Greatings

Thanks @BartJones and @Flipso !

Yes; I came to the same conclusion last night, as in: first get rstudio running on a separate server being capable of interacting with HA and if all works, see if an addon has added value (besides being a learning project for me ; ).

Shiny in Iframe, yeah! Didn’t think of this.

I am aware about other python projects for data science and advanced automations, which are super cool. I want to see what I can do with R.

Thank you for sharing your brainstorm ideas and interest in this endeavor. Keeping you posted!

Step 1.

Get access to the HA API in R. Piece of cake.

#install.packages(c("httr", "jsonlite"))

library(httr)
library(jsonlite)

token <- "XXX"
base_url <- "http://IP_HOMEASSISTANT:8123/api/"

res <- GET(base_url,
           add_headers('Authorization' = paste0("Bearer ", token),
                                                'Content-Type' = "application/json")
)
data <- fromJSON(rawToChar(res$content))

Step 2.

Open the database in R (I use MariaDB).

#check you added the user + password + rights in the add-on
#open the port in the add-on
#https://community.home-assistant.io/t/access-mariadb-from-other-computer/69869/6

#install.packages("RMariaDB")

library(RMariaDB)

username_mariadb <- "XXX"
password_mariadb <- "XXX"
host <- "XXX" # the IP address only (no http:// or other protocol)

con <- dbConnect(
  drv = RMariaDB::MariaDB(), 
  username = paste0(username_mariadb),
  password = paste0(password_mariadb), 
  host = paste0(host), 
  port = 3306
)

dbSendQuery(con, 'use homeassistant')

res <- dbSendQuery(con, 'show tables')
tables <- dbFetch(res, n = 10)
View(tables)

res <- dbSendQuery(con, 'select * from states')
states <- dbFetch(res, n = 100)
View(states)

res <- dbSendQuery(con, 'select * from states_meta')
states_meta <- dbFetch(res, n = 200)
View(states_meta)

Okay, having this I’m able to interact with the HA from R. Having this, I’m able to do analyses with my HA data in R. Step 3 will be do collect and analyse some data.

After this, step 4 will be calling an R script from HA to automate things…