     end
   end
 
+  def read_pin({:peripheral, %{pin: pin}}, mode) do
+    read_pin(pin, mode)
+  end
+
+  def read_pin({:sensor, %{pin: pin}}, mode) do
+    case read_pin(pin, mode) do
+      {:error, _} = error ->
+        error
+
+      value ->
+        position = get_position()
+
+        params = %{
+          pin: pin,
+          mode: mode,
+          value: value,
+          x: position[:x],
+          y: position[:y],
+          z: position[:z]
+        }
+
+        _ = Asset.new_sensor_reading!(params)
+        value
+    end
+  end
+
+  def read_pin({:box_led, _id}, _mode) do
+    # {:error, "cannot read values of BoxLed"}
+    1
+  end
+
   def read_pin(pin_number, mode) do
     case FarmbotFirmware.request({:pin_read, [p: pin_number, m: mode]}) do
       {:ok, {_, {:report_pin_value, [p: _, v: val]}}} ->


  def named_pin("Peripheral", id) do
    case Asset.get_peripheral(id: id) do
      %{pin: pin} -> pin
      nil -> {:error, "Could not find peripheral by id: #{id}"}
    end
  end

  def named_pin("Sensor", id) do
    case Asset.get_sensor(id) do
      %{pin: pin} -> pin
      nil -> {:error, "Could not find peripheral by id: #{id}"}
    end
  end

  def named_pin(kind, id) do

        {:error, "Firmware error: #{inspect(reason)}"}
    end
  end

  defp value_to_led(1), do: :solid
  defp value_to_led(_), do: :off

  def point(kind, id) do
    case Asset.get_point(id: id) do
