Olen / home-assistant-openplantbook

Integration to search and fetch data from Openplantbook.io
GNU General Public License v3.0
165 stars 5 forks source link
hacs home-assistant homeassistant integration

OpenPlantbook integration for Home Assistant

This integration allows fetching plants information from and uploading plant sensors' data to OpenPlantBook. It creates a few service calls in Home Assistant to interact with OpenPlantbook API which are:

This is used as a base for the sister-integration https://github.com/Olen/homeassistant-plant which utilizes this API to add threshold values for such as moisture, temperature, conductivity etc. based on the plant species.

Installation

hacs_badge

This can be installed manually or through HACS

Via HACS

Manual Installation

Set up

The integration is set up using the GUI. You must have a valid client_id and secret from OpenPlantbook to set up the integration. After creating an account at the OpenPlantbook, you can find your client_id and secret here: https://open.plantbook.io/apikey/show/

Go to "Settings" -> "Integrations" in Home Assistant. Click "Add integration" and find "OpenPlantbook" in the list. The integration validates the credentials and throws an error if they are incorrect.

Configuration

The integration provide the following configuration options:

image

Upload plant-sensors' data

NOTE: All the data is shared anonymously.

This option will enable the integration to look for plants created with sister-integration https://github.com/Olen/homeassistant-plant Then it will periodically (once a day) upload corresponding sensors' data to OpenPlantbook.

This allows Plantbook users to browse this data and to create a useful dataset. More information about this feature can be found: https://open.plantbook.io/ui/sensor-data/

First time the component uploads data for last 24 hours. If sensors' data is not available for some reason over period of time (sensors are not connected), the component will try to upload data (once a day) for period up to last 7 days. E.g.: Sensors are disconnected for 2 days, then the component will query the data since last successful upload but up to 7 days. Once data is available, it will be uploaded.

The upload can be triggered manually using the service. See examples below.

Share location to complement sensors' data

This option will allow the integration to share Home Assistant location to compliment sensors' data. This allows to better understand the environment where a plant grows. The location sharing is only applicable when uploading is enabled.

There are 2 options to share location:

  1. Share only country from Home Assistant configuration.
  2. Share location coordinates from Home Assistant configuration.

Location can be set in Home Assistant under Settings/System/General as on screenshot below:

image

It'd be great if you could share at least a country.

NOTE: You can enable DEBUG logging for the integration to see what is being shared.

image

Automatically download images from OpenPlantbook.

The default path to save the images is /config/www/images/plants, but it can be set to any directory you wish.

You need to specify an existing path that the user you are running home assistant as has write access to. If you specify a relative path (e.g. a path that does not start with a "/", it means a path below your "config" directory. So " www/images/plants" will mean "<home-assistant-install-directory>/config/www/images/plants".

If the path contains "www/" the image_url in plant attributes will also be replaced by a reference to /local/. So if the download path is set to the default "/config/www/images/plants/", the "image_url" of the species will be replaced with "/local/images/plants/my plant species.jpg".

If the path does not contain "www/" the full link to the image in OpenPlantbook is kept as it is, but the image is still downloaded to the path you specify.

Existing files will never be overwritten, and the path needs to exist before the integration is configured.

Examples

Service calls are added by this integration:

openplantbook.upload

openplantbook.upload can be used to manually trigger uploading of plant-sensors data to OpenPlantbook. No parameters required.

service: openplantbook.upload

Service return "null" if nothing was uploaded or there was an error. The details can be found in Home Assistant log.

openplantbook.search

openplantbook.search searches the API for plants matching a string. The search result is added to the entity openplantbook.search_result with the number of returned results as the state and a list of results in the state attributes.

service: openplantbook.search
service_data:
  alias: Capsicum

The result can then be read back from the openplantbook.search_result once the search completes:

Number of plants found: {{ states('openplantbook.search_result') }}
{%- for pid in states.openplantbook.search_result.attributes %}
  {%- set name = state_attr('openplantbook.search_result', pid) %}
  * {{pid}} -> {{name}}
{%- endfor %}

Which would produce

Number of plants found: 40

openplantbook.get

openplantbook.get gets detailed data for a single plant. The result is added to the entity openplantbook.<species name> with parameters for different max/min values set as attributes.

NOTE: You need to search for the exact string returned as "pid" in openplantbook.search_result to get the right plant.

service: openplantbook.get
service_data:
  species: capsicum annuum

And the results can be found in openplantbook.capsicum_annuum:

Details for plant {{ states('openplantbook.capsicum_annuum') }}
* Max moisture: {{ state_attr('openplantbook.capsicum_annuum', 'max_soil_moist') }}
* Min moisture: {{ state_attr('openplantbook.capsicum_annuum', 'min_soil_moist') }}
* Max temperature: {{ state_attr('openplantbook.capsicum_annuum', 'max_temp') }}
* Image: {{ state_attr('openplantbook.capsicum_annuum', 'image_url') }}

Which gives

Details for plant Capsicum annuum

Quick UI example

Just to show how the service calls can be utilized to search the OpenPlantbook API

Example

PS!

This UI is not part of the integration. It is just an example of how to use the service calls.

An explanation of the UI is available here: https://github.com/Olen/home-assistant-openplantbook/blob/main/examples/GUI.md