Run this notebook

Use Livebook to open this notebook and explore new ideas.

It is easy to get started, on your machine or the cloud.

Click below to open and run it in your Livebook at .

(or change your Livebook location)

# Homex ```elixir Mix.install([ {:homex, path: __DIR__}, ], config: [ homex: [ entities: [MySwitch, MyTemperature, MyHumidity, MyLight] ] ]) ``` ## Section ```elixir defmodule MySwitch do use Homex.Entity.Switch, name: "my-switch" def handle_on(state) do IO.puts("Switch turned on") {:noreply, state} end def handle_off(state) do IO.puts("Switch turned off") {:noreply, state} end end defmodule MyTemperature do use Homex.Entity.Sensor, name: "my-temperature", unit_of_measurement: "C", device_class: "temperature" def handle_update(state) do {:reply, [state: Enum.random(-40..40//1)], state} end end defmodule MyHumidity do use Homex.Entity.Sensor, name: "my-humidiy", unit_of_measurement: "%", device_class: "humidity" def handle_update(state) do {:reply, [state: Enum.random(20..90//1)], state} end end defmodule MyLight do use Homex.Entity.Light, name: "my-light" def handle_brightness(brightness, state) do {:ok, percentage} = convert_brightness(brightness, 0) IO.puts("Light set to #{percentage}%") {:reply, [brightness: brightness], state} end end ``` ```elixir Logger.put_application_level(:emqtt, :info) ``` ```elixir {:ok, pid} = Homex.start_link() ``` ```elixir Homex.publish(MySwitch.command_topic(), MySwitch.on()) ``` ```elixir Homex.publish(MySwitch.command_topic(), MySwitch.off()) ``` ```elixir Homex.publish(MyLight.brightness_command_topic(), "138") ```
See source

Have you already installed Livebook?

If you already installed Livebook, you can configure the default Livebook location where you want to open notebooks.
Livebook up Checking status We can't reach this Livebook (but we saved your preference anyway)
Run notebook

Not yet? Install Livebook in just a minute

Livebook is open source, free, and ready to run anywhere.

Run on your machine

with Livebook Desktop

Run in the cloud

on select platforms

To run on Linux, Docker, embedded devices, or Elixir’s Mix, check our README.

PLATINUM SPONSORS
SPONSORS
Code navigation with go to definition of modules and functions Read More ×